import type { TaskEffort, TaskEffortWrite } from './dto/task-effort'
import type { HydraCollection } from '~/utils/api'
import { extractHydraMembers } from '~/utils/api'

export function useTaskEffortService() {
    const api = useApi()

    async function getAll(): Promise<TaskEffort[]> {
        const data = await api.get<HydraCollection<TaskEffort>>('/task_efforts')
        return extractHydraMembers(data)
    }

    async function create(payload: TaskEffortWrite): Promise<TaskEffort> {
        return api.post<TaskEffort>('/task_efforts', payload as Record<string, unknown>, {
            toastSuccessKey: 'taskEfforts.created',
        })
    }

    async function update(id: number, payload: Partial<TaskEffortWrite>): Promise<TaskEffort> {
        return api.patch<TaskEffort>(`/task_efforts/${id}`, payload as Record<string, unknown>, {
            toastSuccessKey: 'taskEfforts.updated',
        })
    }

    async function remove(id: number): Promise<void> {
        await api.delete(`/task_efforts/${id}`, {}, {
            toastSuccessKey: 'taskEfforts.deleted',
        })
    }

    return { getAll, create, update, remove }
}
