import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import emailTemplateService from '@services/emailTemplateService';
import { useNotification } from './reduxHooks';

/**
 * Custom hooks for email template management
 */

/**
 * Hook for fetching all email templates
 */
export const useEmailTemplates = () => {
  return useQuery({
    queryKey: ['email-templates'],
    queryFn: emailTemplateService.getAllTemplates
  });
};

/**
 * Hook for fetching templates by type
 * @param {string} type - Template type
 */
export const useEmailTemplatesByType = (type) => {
  return useQuery({
    queryKey: ['email-templates', type],
    queryFn: () => emailTemplateService.getTemplatesByType(type),
    enabled: !!type
  });
};

/**
 * Hook for fetching a single template by ID
 * @param {string} id - Template ID
 */
export const useEmailTemplate = (id) => {
  return useQuery({
    queryKey: ['email-template', id],
    queryFn: () => emailTemplateService.getTemplateById(id),
    enabled: !!id
  });
};

/**
 * Hook for fetching the default template for a type
 * @param {string} type - Template type
 */
export const useDefaultEmailTemplate = (type) => {
  return useQuery({
    queryKey: ['default-email-template', type],
    queryFn: () => emailTemplateService.getDefaultTemplate(type),
    enabled: !!type
  });
};

/**
 * Hook for creating a new email template
 */
export const useCreateEmailTemplate = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();

  return useMutation({
    mutationFn: (templateData) => emailTemplateService.createTemplate(templateData),
    onSuccess: (data) => {
      queryClient.invalidateQueries({ queryKey: ['email-templates'] });
      queryClient.invalidateQueries({ queryKey: ['email-templates', data.type] });
      notification.showNotification('Email template created successfully', 'success');
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to create email template',
        'error'
      );
    }
  });
};

/**
 * Hook for updating an email template
 */
export const useUpdateEmailTemplate = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();

  return useMutation({
    mutationFn: ({ id, templateData }) => emailTemplateService.updateTemplate(id, templateData),
    onSuccess: (data) => {
      queryClient.invalidateQueries({ queryKey: ['email-templates'] });
      queryClient.invalidateQueries({ queryKey: ['email-templates', data.type] });
      queryClient.invalidateQueries({ queryKey: ['email-template', data.id] });
      notification.showNotification('Email template updated successfully', 'success');
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to update email template',
        'error'
      );
    }
  });
};

/**
 * Hook for deleting an email template
 */
export const useDeleteEmailTemplate = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();

  return useMutation({
    mutationFn: (id) => emailTemplateService.deleteTemplate(id),
    onSuccess: (_, id) => {
      queryClient.invalidateQueries({ queryKey: ['email-templates'] });
      notification.showNotification('Email template deleted successfully', 'success');
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to delete email template',
        'error'
      );
    }
  });
};

/**
 * Hook for setting a template as default
 */
export const useSetDefaultEmailTemplate = () => {
  const queryClient = useQueryClient();
  const notification = useNotification();

  return useMutation({
    mutationFn: (id) => emailTemplateService.setAsDefault(id),
    onSuccess: (data) => {
      queryClient.invalidateQueries({ queryKey: ['email-templates'] });
      queryClient.invalidateQueries({ queryKey: ['email-templates', data.type] });
      queryClient.invalidateQueries({ queryKey: ['default-email-template', data.type] });
      notification.showNotification('Default template set successfully', 'success');
    },
    onError: (error) => {
      notification.showNotification(
        error.message || 'Failed to set default template',
        'error'
      );
    }
  });
};