import type { Observation } from './dto/observation'
import { extractItems } from '~/utils/api'

export const listObservations = async (employeeId: number) => {
  const api = useApi()
  const data = await api.get<Observation[] | { 'hydra:member'?: Observation[] }>(
    '/observations',
    { employee: `/api/employees/${employeeId}` },
    { toast: false }
  )
  return extractItems<Observation>(data)
}

export const createObservation = async (data: {
  employeeId: number
  month: string
  content: string
}) => {
  const api = useApi()
  return api.post<Observation>('/observations', {
    employee: `/api/employees/${data.employeeId}`,
    month: data.month,
    content: data.content
  }, {
    toastSuccessKey: 'success.observation.create',
    toastErrorKey: 'errors.observation.create'
  })
}

export const updateObservation = async (id: number, data: {
  month: string
  content: string
}) => {
  const api = useApi()
  return api.patch<Observation>(`/observations/${id}`, {
    month: data.month,
    content: data.content
  }, {
    toastSuccessKey: 'success.observation.update',
    toastErrorKey: 'errors.observation.update'
  })
}

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