import type { TaskPriority, TaskPriorityWrite } from './dto/task-priority'
import type { HydraCollection } from '~/utils/api'
import { extractHydraMembers } from '~/utils/api'

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

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

    async function create(payload: TaskPriorityWrite): Promise<TaskPriority> {
        return api.post<TaskPriority>('/task_priorities', payload as Record<string, unknown>, {
            toastSuccessKey: 'taskPriorities.created',
        })
    }

    async function update(id: number, payload: Partial<TaskPriorityWrite>): Promise<TaskPriority> {
        return api.patch<TaskPriority>(`/task_priorities/${id}`, payload as Record<string, unknown>, {
            toastSuccessKey: 'taskPriorities.updated',
        })
    }

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

    return { getAll, create, update, remove }
}
