import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { client } from "@/client";
import type {
  UpdateUserProfileConfig,
  UpdateUserAppearanceConfig,
  UpdateUserNotificationConfig,
  UpdateUserPrivacyConfig,
  UpdateUserAccessibilityConfig,
} from "@/client";

export function useUserConfig() {
  return useQuery({
    queryKey: ["user", "config"],
    queryFn: async () => {
      const res = await client.userConfig();
      return res.data;
    },
    retry: false,
  });
}

export function useUpdateProfile() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: async (data: UpdateUserProfileConfig) => {
      const res = await client.userUpdateProfile(data);
      return res.data;
    },
    onSuccess: () => qc.invalidateQueries({ queryKey: ["user", "config"] }),
  });
}

export function useUpdateAppearance() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: async (data: UpdateUserAppearanceConfig) => {
      const res = await client.userUpdateAppearance(data);
      return res.data;
    },
    onSuccess: () => qc.invalidateQueries({ queryKey: ["user", "config"] }),
  });
}

export function useUpdateNotification() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: async (data: UpdateUserNotificationConfig) => {
      const res = await client.userUpdateNotification(data);
      return res.data;
    },
    onSuccess: () => qc.invalidateQueries({ queryKey: ["user", "config"] }),
  });
}

export function useUpdatePrivacy() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: async (data: UpdateUserPrivacyConfig) => {
      const res = await client.userUpdatePrivacy(data);
      return res.data;
    },
    onSuccess: () => qc.invalidateQueries({ queryKey: ["user", "config"] }),
  });
}

export function useUpdateAccessibility() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: async (data: UpdateUserAccessibilityConfig) => {
      const res = await client.userUpdateAccessibility(data);
      return res.data;
    },
    onSuccess: () => qc.invalidateQueries({ queryKey: ["user", "config"] }),
  });
}