import {
  useMutation,
  //useMutation,
  useQuery,
} from "@tanstack/react-query";

import { AssignIncomingRecordPayload } from "@/record-management/dto/userRecords/usersDto";
import {
  getRecordIncoming,
  assignIncomingRecordApi,
  getExternalRecordIncoming,
  assignDraftIncomingRecordApi,
} from "@/record-management/services/api/incomingRecordService";
import { OutgoingRecordQueryParams } from "@/record-management/services/api/recordOfficerService";
import { StartAssignRecord } from "../recordOfficer/incomingRecords/AssigneIncomingRecords";
import { MeDto } from "@/shared/dto/user/meDto";
import { useUserDetail } from "./useUserDetail";
import { useAuthUser } from "@/shared/hooks/useAuthUser";

export interface AssignIncomingRecordResponse {
  recordId: string;
  recordBoxes: string[];
}

export const useCreateIncomingRecords = (
  params?: OutgoingRecordQueryParams
) => {
  // Use React Query for data fetching with caching
  const { userDetails } = useAuthUser();
  const { permissionKeys } = useUserDetail(userDetails as MeDto);
  const RO = permissionKeys.includes("can:dispatchRecords");
  const {
    data: queryData,
    isLoading,
    isError,
    refetch: refetch1,
  } = useQuery({
    queryKey: ["record-incoming", params],
    staleTime: 1000 * 60 * 5,
    queryFn: () => getRecordIncoming(RO ? params : {}),
    enabled: Boolean(RO),
  });
  const {
    data: externalRecordIncoming,
    isLoading: isExternalRecordIncoming,
    isError: isExternalRecordIncomingError,
    refetch,
  } = useQuery({
    queryKey: ["external-record-incoming", params],
    staleTime: 1000 * 60 * 5,
    queryFn: () => getExternalRecordIncoming(RO ? params : {}),
    enabled: Boolean(RO),
  });
  // Import or define the actual function to assign an incoming record

  const {
    mutate: assignIncomingRecordMutation,
    isPending,
    isSuccess,
    data: mutationData,
    error,
  } = useMutation({
    mutationFn: (body: AssignIncomingRecordPayload): Promise<any> =>
      assignIncomingRecordApi(body),
  });

  const {
    mutateAsync: startAssignIncomingRecordMutation,
    data: startMutationData,
    isPending: isStartingPending,
    isSuccess: isStartAssignSuccess,
    error: isStartError,
  } = useMutation<AssignIncomingRecordResponse, Error, string>({
    mutationFn: (id: string) => assignDraftIncomingRecordApi(id),
  });

  const externalRecordsAfter = queryData?.items ?? [];
  const externalRecordsAfterCount = queryData?.count ?? [];
  const externalRecords = externalRecordIncoming?.items ?? [];
  const externalRecordsCount = externalRecordIncoming?.count ?? 0;
  return {
    refetch,
    assignIncomingRecord: assignIncomingRecordMutation,
    isPending,
    isSuccess,
    error,
    mutationData,
    externalRecordsAfter,
    externalRecordsAfterCount,
    isLoading,
    isError,

    // isAssignError: isError,
    externalRecords,
    isExternalRecordIncoming,
    isExternalRecordIncomingError,
    externalRecordsCount,
    startAssignIncomingRecordMutation,
    startMutationData,
    refetch1,
  };
};
