import { useApiMutation } from '../../hooks/useApiMutation';
import api from '../../api';

export function useOccupancyMutations() {
  const invalidateOccupancies: Array<readonly unknown[]> = [['occupancies']];
  const checkInMutation = useApiMutation(
    async (payload: unknown) => api.post('/occupancies/check-in', payload),
    { invalidate: invalidateOccupancies },
  );
  const checkOutMutation = useApiMutation(
    async ({ id, payload }: { id: number; payload: Record<string, unknown> }) =>
      api.put(`/occupancies/${id}/check-out`, payload),
    { invalidate: invalidateOccupancies },
  );
  const transferMutation = useApiMutation(
    async ({ id, payload }: { id: number; payload: unknown }) =>
      api.put(`/occupancies/${id}/transfer`, payload),
    { invalidate: invalidateOccupancies },
  );
  const batchCheckOutMutation = useApiMutation(
    async (payload: Record<string, unknown>) => api.post('/occupancies/batch-check-out', payload),
    { invalidate: invalidateOccupancies },
  );
  const batchDeleteMutation = useApiMutation(
    async (ids: number[]) => api.post('/occupancies/batch-delete', { ids }),
    { invalidate: invalidateOccupancies },
  );
  const batchRestoreMutation = useApiMutation(
    async (ids: number[]) =>
      api.put<{ restored: number; skipped: number }>('/occupancies/batch-restore', { ids }),
    { invalidate: invalidateOccupancies },
  );
  const archiveMutation = useApiMutation(
    async (id: number) => api.delete(`/occupancies/${id}`),
    { invalidate: invalidateOccupancies },
  );
  const purgeMutation = useApiMutation(
    async (id: number) => api.delete(`/occupancies/${id}/permanent`),
    { invalidate: invalidateOccupancies },
  );
  const batchPurgeMutation = useApiMutation(
    async (ids: number[]) => api.post('/occupancies/batch-permanent-delete', { ids }),
    { invalidate: invalidateOccupancies },
  );
  const importMutation = useApiMutation(
    async ({ formData, params }: { formData: FormData; params: string }) =>
      api.post(`/occupancies/import?${params}`, formData, {
        headers: { 'Content-Type': 'multipart/form-data' },
      }),
    { invalidate: invalidateOccupancies },
  );

  return {
    checkInMutation,
    checkOutMutation,
    transferMutation,
    batchCheckOutMutation,
    batchDeleteMutation,
    batchRestoreMutation,
    archiveMutation,
    purgeMutation,
    batchPurgeMutation,
    importMutation,
  };
}
