import type { Employee } from './dto/employee'
import { extractItems } from '~/utils/api'

export const listEmployees = async () => {
  const api = useApi()
  const data = await api.get<Employee[] | { 'hydra:member'?: Employee[] }>(
    '/employees',
    {},
    { toast: false }
  )
  return extractItems<Employee>(data)
}

export const listScopedEmployees = async () => {
  const api = useApi()
  const data = await api.get<Employee[] | { 'hydra:member'?: Employee[] }>(
    '/employees/scoped',
    {},
    { toast: false }
  )
  return extractItems<Employee>(data)
}

export const getEmployee = async (id: number) => {
  const api = useApi()
  return api.get<Employee>(`/employees/${id}`, {}, { toast: false })
}

export const createEmployee = async (payload: {
  firstName: string
  lastName: string
  siteId?: number | null
  contractId: number
  contractNature?: 'CDI' | 'CDD' | 'INTERIM'
  contractStartDate?: string
  contractEndDate?: string | null
  isDriverInput?: boolean
  workDaysHoursInput?: Record<number, number> | null
  interimAgencyId?: number | null
}) => {
  const api = useApi()
  return api.post<Employee>('/employees', {
    firstName: payload.firstName,
    lastName: payload.lastName,
    site: payload.siteId ? `/api/sites/${payload.siteId}` : null,
    contract: `/api/contracts/${payload.contractId}`,
    contractNature: payload.contractNature,
    contractStartDate: payload.contractStartDate,
    contractEndDate: payload.contractEndDate ?? null,
    isDriverInput: payload.isDriverInput ?? false,
    workDaysHoursInput: payload.workDaysHoursInput ?? null,
    interimAgencyId: payload.interimAgencyId ?? null
  }, {
    toastSuccessKey: 'success.employee.create',
    toastErrorKey: 'errors.employee.create'
  })
}

export const updateEmployee = async (
  id: number,
  payload: {
    firstName: string
    lastName: string
    siteId?: number | null
    contractId?: number
    contractNature?: 'CDI' | 'CDD' | 'INTERIM'
    contractStartDate?: string
    contractEndDate?: string | null
    contractPaidLeaveSettled?: boolean
    contractComment?: string | null
    displayOrder?: number
    isDriverInput?: boolean
    workDaysHoursInput?: Record<number, number> | null
    interimAgencyId?: number | null
  }
) => {
  const api = useApi()
  const body: Record<string, unknown> = {
    firstName: payload.firstName,
    lastName: payload.lastName,
    site: payload.siteId ? `/api/sites/${payload.siteId}` : null,
    displayOrder: payload.displayOrder
  }

  if (payload.contractId !== undefined) {
    body.contract = `/api/contracts/${payload.contractId}`
  }
  if (payload.contractNature !== undefined) {
    body.contractNature = payload.contractNature
  }
  if (payload.contractStartDate !== undefined) {
    body.contractStartDate = payload.contractStartDate
  }
  if (payload.contractEndDate !== undefined) {
    body.contractEndDate = payload.contractEndDate ?? null
  }
  if (payload.contractPaidLeaveSettled !== undefined) {
    body.contractPaidLeaveSettled = payload.contractPaidLeaveSettled
  }
  if (payload.contractComment !== undefined) {
    body.contractComment = payload.contractComment ?? null
  }
  if (payload.isDriverInput !== undefined) {
    body.isDriverInput = payload.isDriverInput
  }
  if (payload.workDaysHoursInput !== undefined) {
    body.workDaysHoursInput = payload.workDaysHoursInput
  }
  if (payload.interimAgencyId !== undefined) {
    body.interimAgencyId = payload.interimAgencyId
  }

  return api.patch<Employee>(`/employees/${id}`, body, {
    toastSuccessKey: 'success.employee.update',
    toastErrorKey: 'errors.employee.update'
  })
}

export const updateEmployeeOrder = async (
  id: number,
  displayOrder: number
) => {
  const api = useApi()
  return api.patch<Employee>(`/employees/${id}`, {
    displayOrder
  }, {
    toast: false
  })
}

export const deleteEmployee = async (id: number) => {
  const api = useApi()
  return api.delete(`/employees/${id}`, {}, {
    toastSuccessKey: 'success.employee.delete',
    toastErrorKey: 'errors.employee.delete'
  })
}
