export type ShiftAssignmentPayload = {
    employee_id: string
}

export type CreateShiftPayload = {
    date: string
    type: string
    start_time: string
    end_time: string
    assignments?: ShiftAssignmentPayload[]
}

export type ShiftResponse = {
    id: string
    date: string
    type: string
    start_time: string
    end_time: string
    assignments: {
        id: string
        name: string
    }[]
}

const buildHeaders = (orgId: string) => ({
    'X-Org-Id': orgId
})

export const useShiftApi = () => {
    const getShifts = (branchId: string, from: string, to: string, orgId: string) => {
        return $fetch<ShiftResponse[]>(`/shifts/branches/${branchId}`, {
            method: 'GET',
            headers: buildHeaders(orgId),
            query: { from, to }
        })
    }

    const createShift = (branchId: string, payload: CreateShiftPayload, orgId: string) => {
        return $fetch<ShiftResponse>(`/shifts/branches/${branchId}`, {
            method: 'POST',
            headers: buildHeaders(orgId),
            body: payload
        })
    }

    const assignEmployees = (shiftId: string, payload: ShiftAssignmentPayload[], orgId: string) => {
        return $fetch<ShiftResponse>(`/shifts/${shiftId}/assignments`, {
            method: 'POST',
            headers: buildHeaders(orgId),
            body: { assignments: payload }
        })
    }

    return {
        getShifts,
        createShift,
        assignEmployees
    }
}
