import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import * as profileApi from '../api/profile';

// Profile hooks
export const useProfile = () => {
  return useQuery({
    queryKey: ['profile'],
    queryFn: profileApi.getProfile,
  });
};

export const useUpdateProfile = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: profileApi.updateProfile,
    onSuccess: (data) => {
      queryClient.setQueryData(['profile'], data);
      queryClient.invalidateQueries({ queryKey: ['currentUser'] });
    },
  });
};

export const useUploadAvatar = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: profileApi.uploadAvatar,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['profile'] });
      queryClient.invalidateQueries({ queryKey: ['currentUser'] });
    },
  });
};

export const useDeleteAvatar = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: profileApi.deleteAvatar,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['profile'] });
      queryClient.invalidateQueries({ queryKey: ['currentUser'] });
    },
  });
};

// Email hooks
export const useSendVerificationEmail = () => {
  return useMutation({
    mutationFn: profileApi.sendVerificationEmail,
  });
};

export const useVerifyEmail = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: profileApi.verifyEmail,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['profile'] });
      queryClient.invalidateQueries({ queryKey: ['currentUser'] });
    },
  });
};

export const useRequestEmailChange = () => {
  return useMutation({
    mutationFn: profileApi.requestEmailChange,
  });
};

export const useConfirmEmailChange = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: profileApi.confirmEmailChange,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['profile'] });
      queryClient.invalidateQueries({ queryKey: ['currentUser'] });
    },
  });
};

// Password hooks
export const useChangePassword = () => {
  return useMutation({
    mutationFn: ({
      currentPassword,
      newPassword,
    }: {
      currentPassword: string;
      newPassword: string;
    }) => profileApi.changePassword(currentPassword, newPassword),
  });
};

// 2FA hooks
export const useSetupTOTP = () => {
  return useMutation({
    mutationFn: profileApi.setupTOTP,
  });
};

export const useVerifyTOTP = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: profileApi.verifyTOTP,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['profile'] });
      queryClient.invalidateQueries({ queryKey: ['currentUser'] });
    },
  });
};

export const useDisableTOTP = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: profileApi.disableTOTP,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['profile'] });
      queryClient.invalidateQueries({ queryKey: ['currentUser'] });
    },
  });
};

export const useRecoveryCodes = () => {
  return useQuery({
    queryKey: ['recoveryCodes'],
    queryFn: profileApi.getRecoveryCodes,
    enabled: false, // Only fetch on demand
  });
};

export const useRegenerateRecoveryCodes = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: profileApi.regenerateRecoveryCodes,
    onSuccess: (codes) => {
      queryClient.setQueryData(['recoveryCodes'], codes);
    },
  });
};

// Phone verification hooks
export const useSendPhoneVerification = () => {
  return useMutation({
    mutationFn: profileApi.sendPhoneVerification,
  });
};

export const useVerifyPhoneCode = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: profileApi.verifyPhoneCode,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['profile'] });
      queryClient.invalidateQueries({ queryKey: ['currentUser'] });
    },
  });
};

// Session hooks
export const useSessions = () => {
  return useQuery({
    queryKey: ['sessions'],
    queryFn: profileApi.getSessions,
  });
};

export const useRevokeSession = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: profileApi.revokeSession,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['sessions'] });
    },
  });
};

export const useRevokeOtherSessions = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: profileApi.revokeOtherSessions,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['sessions'] });
    },
  });
};

export const useLoginHistory = () => {
  return useQuery({
    queryKey: ['loginHistory'],
    queryFn: profileApi.getLoginHistory,
  });
};

// Multiple email hooks
export const useUserEmails = () => {
  return useQuery({
    queryKey: ['userEmails'],
    queryFn: profileApi.getUserEmails,
  });
};

export const useAddUserEmail = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: profileApi.addUserEmail,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['userEmails'] });
    },
  });
};

export const useDeleteUserEmail = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: profileApi.deleteUserEmail,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['userEmails'] });
    },
  });
};

export const useSendUserEmailVerification = () => {
  return useMutation({
    mutationFn: profileApi.sendUserEmailVerification,
  });
};

export const useVerifyUserEmail = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ emailId, token }: { emailId: number; token: string }) =>
      profileApi.verifyUserEmail(emailId, token),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['userEmails'] });
    },
  });
};

export const useSetPrimaryEmail = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: profileApi.setPrimaryEmail,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['userEmails'] });
      queryClient.invalidateQueries({ queryKey: ['profile'] });
      queryClient.invalidateQueries({ queryKey: ['currentUser'] });
    },
  });
};
