import axiosInstance from "@/lib/axios";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";

const fetchAdminSettings = async () => {
  const { data } = await axiosInstance.get(`/superadmin/settings`);

  return data;
};

const fetchAdminSingleSettings = async (key: string) => {
  const { data } = await axiosInstance.get(`/superadmin/settings/${key}`);

  return data;
};

export const useUpdateReligionSettings = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async (updatedValues: any) => {
      const response = await axiosInstance.put(
        `/superadmin/settings/religion`,
        {
          value: updatedValues,
        }
      );

      return response.data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: ["AdminReligionSettings"],
        refetchType: "active",
      });
      queryClient.invalidateQueries({
        queryKey: ["AdminSettings"],
        refetchType: "active",
      });
    },
    onError: (error) => {
      console.error("Error updating religion settings:", error);
    },
  });
};

export const useDeleteRelationshipSettings = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async (updatedValues: string[]) => {
      const response = await axiosInstance.put(
        `/superadmin/settings/relationship_type`,
        {
          key: "relationship_type",
          value: updatedValues,
        }
      );
      return response.data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: ["AdminRelationshipSettings"],
        refetchType: "active",
      });
      queryClient.invalidateQueries({
        queryKey: ["AdminSettings"],
        refetchType: "active",
      });
    },
    onError: (error) => {
      console.error("Error deleting relationship type:", error);
    },
  });
};

export const useDeleteReligionSettings = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async (
      updatedValues: Record<string, { denominations: string[] }>
    ) => {
      const response = await axiosInstance.put(
        `/superadmin/settings/religion`,
        {
          value: updatedValues,
        }
      );
      return response.data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: ["AdminReligionSettings"],
        refetchType: "active",
      });
      queryClient.invalidateQueries({
        queryKey: ["AdminSettings"],
        refetchType: "active",
      });
    },
    onError: (error) => {
      console.error("Error deleting religion:", error);
    },
  });
};

// for later use
export const useCreateRelationshipSettings = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async (newValues: string[]) => {
      const response = await axiosInstance.post(
        `/superadmin/settings/relationship_type`,
        {
          key: "relationship_type",
          value: newValues,
        }
      );
      return response.data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: ["AdminRelationshipSettings"],
        refetchType: "active",
      });
      queryClient.invalidateQueries({
        queryKey: ["AdminSettings"],
        refetchType: "active",
      });
    },
    onError: (error) => {
      console.error("Error creating relationship settings:", error);
    },
  });
};

export const useCreateReligionSettings = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async (
      newValues: Record<string, { denominations: string[] }>
    ) => {
      const response = await axiosInstance.post(`/superadmin/settings`, {
        key: "religion",
        value: newValues,
      });
      return response.data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: ["AdminReligionSettings"],
        refetchType: "active",
      });
      queryClient.invalidateQueries({
        queryKey: ["AdminSettings"],
        refetchType: "active",
      });
    },
    onError: (error) => {
      console.error("Error creating religion settings:", error);
    },
  });
};

export const useUpdateRelationshipSettings = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async (updatedValues: string[]) => {
      const response = await axiosInstance.put(
        `/superadmin/settings/relationship_type`,
        {
          value: updatedValues,
        }
      );
      return response.data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: ["AdminRelationshipSettings"],
        refetchType: "active",
      });
      queryClient.invalidateQueries({
        queryKey: ["AdminSettings"],
        refetchType: "active",
      });
    },
    onError: (error) => {
      console.error("Error updating relationship settings:", error);
    },
  });
};

export const useUpdateSettings = (path: string) => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async (updatedValues: string[]) => {
      const response = await axiosInstance.put(`/superadmin/settings/${path}`, {
        value: updatedValues,
      });
      return response.data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: ["AdminSettings"],
        refetchType: "active",
      });
    },
    onError: (error) => {
      console.error("Error updating settings:", error);
    },
  });
};

export const useGetAdminSettings = () => {
  return useQuery({
    queryKey: ["AdminSettings"],
    queryFn: fetchAdminSettings,
    staleTime: 1000 * 60 * 10,
    refetchOnWindowFocus: true,
  });
};

export const useGetAdminRelationshipSettings = (key: string) => {
  return useQuery({
    queryKey: ["AdminRelationshipSettings"],
    queryFn: () => fetchAdminSingleSettings(key),
    staleTime: 1000 * 60 * 10,
    refetchOnWindowFocus: true,
  });
};

export const useGetAdminReligionSettings = (key: string) => {
  return useQuery({
    queryKey: ["AdminReligionSettings"],
    queryFn: () => fetchAdminSingleSettings(key),
    staleTime: 1000 * 60 * 10,
    refetchOnWindowFocus: true,
  });
};
