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

export function useBedMutations() {
  const saveBedCellMutation = useApiMutation(
    async ({
      roomId,
      bedId,
      field,
      value,
    }: {
      roomId: number;
      bedId: number;
      field: string;
      value: unknown;
    }) => api.put(`/rooms/${roomId}/beds/${bedId}`, { [field]: value }),
    { invalidate: [['rooms']] },
  );
  const saveBedMutation = useApiMutation(
    async ({
      roomId,
      bedId,
      values,
    }: {
      roomId: number;
      bedId?: number;
      values: Record<string, unknown>;
    }) =>
      bedId
        ? api.put(`/rooms/${roomId}/beds/${bedId}`, values)
        : api.post(`/rooms/${roomId}/beds`, values),
    { invalidate: [['rooms']] },
  );
  const deleteBedMutation = useApiMutation(
    async ({ roomId, bedId }: { roomId: number; bedId: number }) =>
      api.delete(`/rooms/${roomId}/beds/${bedId}`),
    { invalidate: [['rooms']] },
  );
  const batchBedsMutation = useApiMutation(
    async ({ roomId, count }: { roomId: number; count: number }) =>
      api.post(`/rooms/${roomId}/beds/batch`, { count }),
    { invalidate: [['rooms']] },
  );

  return {
    saveBedCellMutation,
    saveBedMutation,
    deleteBedMutation,
    batchBedsMutation,
  };
}

export function useLockerMutations() {
  const saveLockerCellMutation = useApiMutation(
    async ({
      roomId,
      lockerId,
      field,
      value,
    }: {
      roomId: number;
      lockerId: number;
      field: string;
      value: unknown;
    }) => api.put(`/rooms/${roomId}/lockers/${lockerId}`, { [field]: value }),
    { invalidate: [['rooms']] },
  );
  const saveLockerMutation = useApiMutation(
    async ({
      roomId,
      lockerId,
      values,
    }: {
      roomId: number;
      lockerId?: number;
      values: Record<string, unknown>;
    }) =>
      lockerId
        ? api.put(`/rooms/${roomId}/lockers/${lockerId}`, values)
        : api.post(`/rooms/${roomId}/lockers`, values),
    { invalidate: [['rooms']] },
  );
  const deleteLockerMutation = useApiMutation(
    async ({ roomId, lockerId }: { roomId: number; lockerId: number }) =>
      api.delete(`/rooms/${roomId}/lockers/${lockerId}`),
    { invalidate: [['rooms']] },
  );
  const batchLockersMutation = useApiMutation(
    async ({ roomId, count }: { roomId: number; count: number }) =>
      api.post(`/rooms/${roomId}/lockers/batch`, { count }),
    { invalidate: [['rooms']] },
  );

  return {
    saveLockerCellMutation,
    saveLockerMutation,
    deleteLockerMutation,
    batchLockersMutation,
  };
}

export function useRoomItemMutations() {
  return { ...useBedMutations(), ...useLockerMutations() };
}

export function useRoomMutations(editing: any) {
  const saveMutation = useApiMutation(
    async (payload: Record<string, unknown>) =>
      editing ? api.put(`/rooms/${editing.id}`, payload) : api.post('/rooms', payload),
    { invalidate: [['rooms']] },
  );
  const saveRoomCellMutation = useApiMutation(
    async ({ record, field, value }: { record: any; field: string; value: unknown }) =>
      api.put(`/rooms/${record.id}`, { [field]: value }),
    { invalidate: [['rooms']] },
  );
  const archiveMutation = useApiMutation(
    async (id: number) => api.delete(`/rooms/${id}`),
    { invalidate: [['rooms']] },
  );
  const restoreMutation = useApiMutation(
    async (id: number) => api.put(`/rooms/${id}/restore`),
    { invalidate: [['rooms']] },
  );
  const purgeMutation = useApiMutation(
    async (id: number) => api.delete(`/rooms/${id}/permanent`),
    { invalidate: [['rooms']] },
  );
  const batchDeleteMutation = useApiMutation(
    async (ids: number[]) => api.post('/rooms/batch-delete', { ids }),
    { invalidate: [['rooms']] },
  );
  const batchRestoreMutation = useApiMutation(
    async (ids: number[]) =>
      api.put<{ message?: string; restored: number; skipped: number }>('/rooms/batch-restore', {
        ids,
      }),
    { invalidate: [['rooms']] },
  );
  const batchPurgeMutation = useApiMutation(
    async (ids: number[]) => api.post('/rooms/batch-permanent-delete', { ids }),
    { invalidate: [['rooms']] },
  );
  const importMutation = useApiMutation(
    async (formData: FormData) =>
      api.post<{ message?: string }>('/rooms/import', formData),
    { invalidate: [['rooms']] },
  );

  return {
    saveMutation,
    saveRoomCellMutation,
    archiveMutation,
    restoreMutation,
    purgeMutation,
    batchDeleteMutation,
    batchRestoreMutation,
    batchPurgeMutation,
    importMutation,
  };
}
