import type { TaskTag, TaskTagWrite } from './dto/task-tag'
import type { HydraCollection } from '~/utils/api'
import { extractHydraMembers } from '~/utils/api'

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

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

    async function create(payload: TaskTagWrite): Promise<TaskTag> {
        return api.post<TaskTag>('/task_tags', payload as Record<string, unknown>, {
            toastSuccessKey: 'taskTags.created',
        })
    }

    async function update(id: number, payload: Partial<TaskTagWrite>): Promise<TaskTag> {
        return api.patch<TaskTag>(`/task_tags/${id}`, payload as Record<string, unknown>, {
            toastSuccessKey: 'taskTags.updated',
        })
    }

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

    return { getAll, create, update, remove }
}
