import { useQuery, useMutation } from "@tanstack/react-query";
import { toast } from "sonner"; // ✅ shadcn sonner toast
import { useTranslation } from "react-i18next";
import { useErrorHandler } from "@/shared/hooks/useErrorHandler";

import {
  CreateRecordTagPayload,
  RecordTag,
  RecordTagResponse,
  UpdateRecordTagPayload,
} from "@/user-management/dto/recordTags/recordTags.type";
import { recordTagService } from "../services/api/recordTagsService";

export const useRecordTags = ({
  unitId,
  recordTagId,
}: {
  unitId?: string | null;
  recordTagId?: string;
}) => {
  const { t } = useTranslation();
  const { handleError } = useErrorHandler(t);
  // --- Get Record Tags List by Unit ---
  const {
    data: recordTagsList,
    isLoading: isLoadingRecordTagsList,
    isError: isErrorRecordTagsList,
    refetch: refetchRecordTagsList,
  } = useQuery<RecordTagResponse>({
    queryKey: ["recordTagsList", unitId],
    queryFn: async () => {
      const { data } = await recordTagService.getRecordTagsListWithUnitById(
        unitId!
      );
      return data;
    },
    enabled: !!unitId,
    staleTime: 5 * 60 * 1000,
    retry: false,
  });

  // --- Get Record Tag by Id ---
  const {
    data: recordTag,
    isLoading: isLoadingRecordTag,
    isError: isErrorRecordTag,
    refetch: refetchRecordTag,
  } = useQuery<RecordTag>({
    queryKey: ["recordTag", recordTagId],
    queryFn: async () => {
      const { data } = await recordTagService.getRecordTags(recordTagId!);
      return data;
    },
    enabled: !!recordTagId,
    staleTime: 5 * 60 * 1000,
    retry: false,
  });

  // --- Create Record Tag ---
  const { mutate: createRecordTag, isPending: isCreatingRecordTag } =
    useMutation({
      mutationFn: async (payload: CreateRecordTagPayload) => {
        const { data } = await recordTagService.createRecordTags(payload);
        return data;
      },
      onSuccess: () => {
        toast.success("Record Tag created successfully ✅");
        refetchRecordTagsList();
      },
      onError: (error) => {
        handleError(error);
      },
    });

  // --- Update Record Tag ---
  const { mutate: updateRecordTag, isPending: isUpdatingRecordTag } =
    useMutation({
      mutationFn: async ({
        id,
        payload,
      }: {
        id: string;
        payload: UpdateRecordTagPayload;
      }) => {
        const { data } = await recordTagService.updateEmployee(id, payload);
        return data;
      },
      onSuccess: () => {
        toast.success("Record Tag updated successfully ✨");
        refetchRecordTagsList();
      },
      onError: (error) => {
        handleError(error);
      },
    });

  // --- Delete Record Tag ---
  const { mutate: deleteRecordTag, isPending: isDeletingRecordTag } =
    useMutation({
      mutationFn: async (id: string) => {
        const { data } = await recordTagService.deleteEmployee(id);
        return data;
      },
      onSuccess: () => {
        toast.success("Record Tag deleted successfully 🗑️");
        refetchRecordTagsList();
      },
      onError: (error) => {
        handleError(error);
      },
    });

  return {
    // list
    recordTagsList,
    isLoadingRecordTagsList,
    isErrorRecordTagsList,
    refetchRecordTagsList,

    // single
    recordTag,
    isLoadingRecordTag,
    isErrorRecordTag,
    refetchRecordTag,

    // mutations
    createRecordTag,
    isCreatingRecordTag,
    updateRecordTag,
    isUpdatingRecordTag,
    deleteRecordTag,
    isDeletingRecordTag,
  };
};
