import type { Bonus } from './dto/bonus'
import { extractItems } from '~/utils/api'

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

export const createBonus = async (data: {
  employeeId: number
  month: string
  amount: number
  comment?: string
}) => {
  const api = useApi()
  return api.post<Bonus>('/bonuses', {
    employee: `/api/employees/${data.employeeId}`,
    month: data.month,
    amount: data.amount,
    comment: data.comment
  }, {
    toastSuccessKey: 'success.bonus.create',
    toastErrorKey: 'errors.bonus.create'
  })
}

export const updateBonus = async (id: number, data: {
  month: string
  amount: number
  comment?: string
}) => {
  const api = useApi()
  return api.patch<Bonus>(`/bonuses/${id}`, {
    month: data.month,
    amount: data.amount,
    comment: data.comment
  }, {
    toastSuccessKey: 'success.bonus.update',
    toastErrorKey: 'errors.bonus.update'
  })
}

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