import {UpdateMedicalPackageFormValues} from "@/components/forms/medical-packages/edit/UpdateMedicalPackageForm";
import {CreateMedicalPackageValues} from "@/components/forms/medical-packages/new/CreateMedicalPackageForm";
import {
  createMedicalPackage,
  deleteMedicalPackage,
  getAllMedicalPackages,
  getAllMedicalPackagesParent,
  getSingleMedicalPackage,
  updateMedicalPackage,
} from "@/service/apis/medical-package";
import {ServerResponse} from "@/types";
import {useMutation, UseMutationResult, useQuery} from "@tanstack/react-query";

export const useGetAllMedicalPackages = (lang: string) =>
  useQuery({
    queryKey: ["get-all-medical-packages", lang],
    queryFn: () => getAllMedicalPackages(lang),
    retry: false,
    refetchOnWindowFocus: false,
  });

export const useGetAllMedicalPackagesParent = (lang: string) =>
  useQuery({
    queryKey: ["get-all-medical-packages-parent", lang],
    queryFn: () => getAllMedicalPackagesParent(lang),
    retry: false,
    refetchOnWindowFocus: false,
  });
export const useGetSingleMedicalPackage = ({id,lang}:{id:string,lang:string}) =>
  useQuery({
    queryKey: ["get-single-medical-packages", id,lang],
    queryFn: () => getSingleMedicalPackage(id,lang),
    retry: false,
    refetchOnWindowFocus: false,
  });

export const useCreateMedicalPackage = (): UseMutationResult<
  ServerResponse, // ✅ data returned on success
  Error, // error type
  CreateMedicalPackageValues, // variables type,
  ServerResponse
> => {
  return useMutation<
    ServerResponse,
    Error,
    CreateMedicalPackageValues,
    ServerResponse
  >({
    mutationFn: (payload) => createMedicalPackage(payload),
  });
};
export const useDeleteMedicalPackage = () =>
  useMutation({mutationFn: deleteMedicalPackage});
export const useUpdateMedicalPackage = (): UseMutationResult<
  ServerResponse, // ✅ data returned on success
  Error, // error type
  {id: string; data: UpdateMedicalPackageFormValues}, // variables type,
  ServerResponse
> => {
  return useMutation<
    ServerResponse,
    Error,
    {id: string; data: UpdateMedicalPackageFormValues},
    ServerResponse
  >({mutationFn: (payload) => updateMedicalPackage(payload.id, payload.data)});
};
