import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import * as api from "../service/Service";
import { useSharedStore } from "@/share/store/sharedStore";
import { UpdateAddressType, UpdateProfileRequest } from "../types/ProfileTypes";

export const useGetProfile = () => {
  const { isLogin } = useSharedStore();
  return useQuery({
    queryKey: ["profile"],
    queryFn: api.getProfile,
    enabled: isLogin,
  });
};

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

  return useMutation({
    mutationFn: (profileData: UpdateProfileRequest) =>
      api.updateProfile(profileData),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["profile"] });
    },
  });
};

export const useGetAddressByLocation = (lat?: number, lon?: number) => {
  return useQuery({
    queryKey: ["address-by-location", lat, lon],
    queryFn: () => api.getAddressByLocation(lat!, lon!),
    enabled: lat !== undefined && lon !== undefined,
  });
};

export const useUpdateAddress = () => {
  return useMutation({
    mutationFn: (addressData: UpdateAddressType) =>
      api.updateAddress(addressData),
  });
};

export const useGetWithList = () => {
  return useQuery({
    queryKey: ["with-list"],
    queryFn: api.getWithList,
  });
};

export const useGetComments = () => {
  return useQuery({
    queryKey: ["comments"],
    queryFn: api.getComments,
  });
};

export const useChangeEmail = () => {
  return useMutation({
    mutationFn: (email: string) => api.changeEmail(email),
  });
};
