/**
 * React Query hooks for ticket email addresses
 */

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import {
  getTicketEmailAddresses,
  getTicketEmailAddress,
  createTicketEmailAddress,
  updateTicketEmailAddress,
  deleteTicketEmailAddress,
  testImapConnection,
  testSmtpConnection,
  fetchEmailsNow,
  setAsDefault,
  TicketEmailAddress,
  TicketEmailAddressListItem,
  TicketEmailAddressCreate,
} from '../api/ticketEmailAddresses';

const QUERY_KEY = 'ticketEmailAddresses';

/**
 * Hook to fetch all ticket email addresses
 */
export const useTicketEmailAddresses = () => {
  return useQuery<TicketEmailAddressListItem[]>({
    queryKey: [QUERY_KEY],
    queryFn: getTicketEmailAddresses,
  });
};

/**
 * Hook to fetch a specific ticket email address
 */
export const useTicketEmailAddress = (id: number) => {
  return useQuery<TicketEmailAddress>({
    queryKey: [QUERY_KEY, id],
    queryFn: () => getTicketEmailAddress(id),
    enabled: !!id,
  });
};

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

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

/**
 * Hook to update an existing ticket email address
 */
export const useUpdateTicketEmailAddress = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id, data }: { id: number; data: Partial<TicketEmailAddressCreate> }) =>
      updateTicketEmailAddress(id, data),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({ queryKey: [QUERY_KEY] });
      queryClient.invalidateQueries({ queryKey: [QUERY_KEY, variables.id] });
    },
  });
};

/**
 * Hook to delete a ticket email address
 */
export const useDeleteTicketEmailAddress = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (id: number) => deleteTicketEmailAddress(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 manually fetch emails
 */
export const useFetchEmailsNow = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (id: number) => fetchEmailsNow(id),
    onSuccess: () => {
      // Refresh the email addresses list to update the last_check_at timestamp
      queryClient.invalidateQueries({ queryKey: [QUERY_KEY] });
      // Also invalidate tickets query to show any new tickets
      queryClient.invalidateQueries({ queryKey: ['tickets'] });
    },
  });
};

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

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

export type {
  TicketEmailAddress,
  TicketEmailAddressListItem,
  TicketEmailAddressCreate,
};
