import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import type { AxiosResponse } from "axios";
import {
  createGlobalOrgConfig,
  updateGlobalOrgConfig,
  getGlobalOrgConfig,
  getListOfGlobalOrgConfig,
  createGlobalUnitConfig,
  updateGlobalUnitConfig,
  deleteGlobalUnitConfig,
  getGlobalUnitConfig,
  getListOfGlobalUnitConfig,
  GlobalOrgConfig,
  GlobalUnitConfig,
  createOrganizationConfig,
  updateOrganizationConfig,
  deleteOrganizationConfig,
  getOrganizationConfig,
  OrganizationConfigPayload,
} from "@/shared/services/organizationConfigService";

// 🔹 Global Org Config Hooks
export const useGetGlobalOrgConfig = (id: string) => {
  return useQuery<AxiosResponse>({
    queryKey: ["globalOrgConfig", id],
    queryFn: () => getGlobalOrgConfig(id),
    enabled: !!id,
  });
};

export const useGetListOfGlobalOrgConfig = (id: string) => {
  return useQuery<AxiosResponse>({
    queryKey: ["listGlobalOrgConfig", id],
    queryFn: () => getListOfGlobalOrgConfig(id),
    enabled: !!id,
  });
};

export const useCreateGlobalOrgConfig = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (data: GlobalOrgConfig) => createGlobalOrgConfig(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["listGlobalOrgConfig"] });
    },
  });
};

export const useUpdateGlobalOrgConfig = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, data }: { id: string; data: GlobalOrgConfig }) =>
      updateGlobalOrgConfig(id, data),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({
        queryKey: ["globalOrgConfig", variables.id],
      });
      queryClient.invalidateQueries({
        queryKey: ["listGlobalOrgConfig"],
      });
    },
  });
};

// 🔹 Global Unit Config Hooks
export const useGetGlobalUnitConfig = (id: string) => {
  return useQuery<AxiosResponse>({
    queryKey: ["globalUnitConfig", id],
    queryFn: () => getGlobalUnitConfig(id),
    enabled: !!id,
  });
};

export const useGetListOfGlobalUnitConfig = (id: string) => {
  return useQuery<AxiosResponse>({
    queryKey: ["listGlobalUnitConfig", id],
    queryFn: () => getListOfGlobalUnitConfig(id),
    enabled: !!id,
  });
};

export const useCreateGlobalUnitConfig = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (data: any) => createGlobalUnitConfig(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["listGlobalUnitConfig"] });
    },
  });
};

export const useUpdateGlobalUnitConfig = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, data }: { id: string; data: any }) =>
      updateGlobalUnitConfig(id, data),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({
        queryKey: ["globalUnitConfig", variables.id],
      });
      queryClient.invalidateQueries({
        queryKey: ["listGlobalUnitConfig"],
      });
    },
  });
};

export const useDeleteGlobalUnitConfig = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (id: string) => deleteGlobalUnitConfig(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["listGlobalUnitConfig"] });
      queryClient.invalidateQueries({ queryKey: ["globalUnitConfig"] });
    },
  });
};

// 🔹 Organization Configuration Hooks (/organization-configurations)
export const useGetOrganizationConfig = (organizationId: string) => {
  return useQuery<AxiosResponse>({
    queryKey: ["organizationConfig", organizationId],
    queryFn: () => getOrganizationConfig(organizationId),
    enabled: !!organizationId,
  });
};

export const useCreateOrganizationConfig = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (data: OrganizationConfigPayload) =>
      createOrganizationConfig(data),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({
        queryKey: ["organizationConfig", variables.organizationId],
      });
    },
  });
};

export const useUpdateOrganizationConfig = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({
      id,
      data,
    }: {
      id: string;
      data: OrganizationConfigPayload;
    }) => updateOrganizationConfig(id, data),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({
        queryKey: ["organizationConfig", variables.data.organizationId],
      });
    },
  });
};

export const useDeleteOrganizationConfig = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (id: string) => deleteOrganizationConfig(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["organizationConfig"] });
    },
  });
};
