import {
  AssignmentApiResponse,
  AssignmentStep,
} from "@/record-management/dto/AssignementStep";
import { getMovementSlip } from "@/record-management/services/api/movementService";
import { getRemarks } from "@/record-management/services/api/remarkService";

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

export const useRecordAssignments = (
  recordId: string,
  options?: { enabled?: boolean },
) => {
  return useQuery({
    queryKey: ["assignments", recordId],
    queryFn: () => getMovementSlip(recordId),
    enabled: !!recordId && options?.enabled !== false,
    staleTime: 0,
    gcTime: 0,
    refetchOnMount: "always",
    refetchOnWindowFocus: true,
    refetchOnReconnect: true,
  });
};

export const useRemarks = (
  recordId: string,
  options?: { enabled?: boolean },
) => {
  return useQuery({
    queryKey: ["remarks", recordId],
    queryFn: () => getRemarks(recordId),
    enabled: !!recordId && options?.enabled !== false,
    staleTime: 0,
    gcTime: 0,
  });
};

export const transformAssignmentsToSteps = (
  assignments: AssignmentApiResponse[]
): AssignmentStep[] => {
  return assignments.map((item) => {
    return {
      assignedDate: item.createdAt,
      completedDate: null, // no completedDate yet
      status:
        item.status === "re_assigned"
          ? "Reassigned"
          : item.status === "draft"
          ? "Pending"
          : "Accepted",
    };
  });
};
