/**
 * React Query hooks for Platform Email Addresses
 */

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import {
  getPlatformEmailAddresses,
  getPlatformEmailAddress,
  createPlatformEmailAddress,
  updatePlatformEmailAddress,
  deletePlatformEmailAddress,
  removeLocalPlatformEmailAddress,
  syncPlatformEmailAddress,
  testImapConnection,
  testSmtpConnection,
  setAsDefault,
  testMailServerConnection,
  getMailServerAccounts,
  getAvailableDomains,
  getAssignableUsers,
  importFromMailServer,
  PlatformEmailAddressListItem,
  PlatformEmailAddress,
  PlatformEmailAddressCreate,
  PlatformEmailAddressUpdate,
} from '../api/platformEmailAddresses';

export type { PlatformEmailAddressListItem, PlatformEmailAddress };

const QUERY_KEY = 'platformEmailAddresses';

/**
 * Hook to fetch all platform email addresses
 */
export const usePlatformEmailAddresses = () => {
  return useQuery({
    queryKey: [QUERY_KEY],
    queryFn: getPlatformEmailAddresses,
  });
};

/**
 * Hook to fetch a single platform email address
 */
export const usePlatformEmailAddress = (id: number) => {
  return useQuery({
    queryKey: [QUERY_KEY, id],
    queryFn: () => getPlatformEmailAddress(id),
    enabled: !!id,
  });
};

/**
 * Hook to create a new platform email address
 */
export const useCreatePlatformEmailAddress = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (data: PlatformEmailAddressCreate) => createPlatformEmailAddress(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: [QUERY_KEY] });
    },
  });
};

/**
 * Hook to update a platform email address
 */
export const useUpdatePlatformEmailAddress = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id, data }: { id: number; data: PlatformEmailAddressUpdate }) =>
      updatePlatformEmailAddress(id, data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: [QUERY_KEY] });
    },
  });
};

/**
 * Hook to delete a platform email address (also removes from mail server)
 */
export const useDeletePlatformEmailAddress = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (id: number) => deletePlatformEmailAddress(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: [QUERY_KEY] });
    },
  });
};

/**
 * Hook to remove email address from database only (keeps mail server account)
 */
export const useRemoveLocalPlatformEmailAddress = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (id: number) => removeLocalPlatformEmailAddress(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: [QUERY_KEY] });
    },
  });
};

/**
 * Hook to sync a platform email address to the mail server
 */
export const useSyncPlatformEmailAddress = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (id: number) => syncPlatformEmailAddress(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: [QUERY_KEY] });
    },
  });
};

/**
 * Hook to test IMAP connection
 */
export const useTestImapConnection = () => {
  return useMutation({
    mutationFn: (id: number) => testImapConnection(id),
  });
};

/**
 * Hook to test SMTP connection
 */
export const useTestSmtpConnection = () => {
  return useMutation({
    mutationFn: (id: number) => testSmtpConnection(id),
  });
};

/**
 * Hook to set email address as default
 */
export const useSetAsDefault = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (id: number) => setAsDefault(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: [QUERY_KEY] });
    },
  });
};

/**
 * Hook to test mail server SSH connection
 */
export const useTestMailServerConnection = () => {
  return useMutation({
    mutationFn: testMailServerConnection,
  });
};

/**
 * Hook to get mail server accounts
 */
export const useMailServerAccounts = () => {
  return useQuery({
    queryKey: [QUERY_KEY, 'mailServerAccounts'],
    queryFn: getMailServerAccounts,
  });
};

/**
 * Hook to get available email domains
 */
export const useAvailableDomains = () => {
  return useQuery({
    queryKey: [QUERY_KEY, 'availableDomains'],
    queryFn: getAvailableDomains,
  });
};

/**
 * Hook to get assignable users
 */
export const useAssignableUsers = () => {
  return useQuery({
    queryKey: [QUERY_KEY, 'assignableUsers'],
    queryFn: getAssignableUsers,
  });
};

/**
 * Hook to import email addresses from the mail server
 */
export const useImportFromMailServer = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: importFromMailServer,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: [QUERY_KEY] });
    },
  });
};
