/**
 * Export/Import React Query Hooks
 */

import { useMutation } from '@tanstack/react-query';
import { exportVariations, importVariations, downloadFile } from '../services/export';
import type { ExportRequest } from '../types/export';
import toast from 'react-hot-toast';
import { useTranslation } from 'react-i18next';

/**
 * Export variations hook
 */
export function useExportVariations() {
  const { t } = useTranslation();
  
  return useMutation({
    mutationFn: (request: ExportRequest) => exportVariations(request),
    onSuccess: (data) => {
      // Trigger download
      downloadFile(data.filename, data.content, data.content_type);
      
      // Show warning if limited (FREE tier) - formatted by frontend with i18n
      if (data.limited) {
        toast(
          t('export.limit_warning', {
            exported: data.exported,
            total: data.total,
          }),
          {
            duration: 8000,
            icon: '⚠️',
            style: {
              background: '#FEF3C7',
              color: '#92400E',
              border: '1px solid #FCD34D',
            },
          }
        );
      } else {
        toast.success(
          t('export.success', { count: data.count, filename: data.filename })
        );
      }
    },
    onError: (error: any) => {
      toast.error(error.message || t('export.error'));
    },
  });
}

/**
 * Import variations hook
 */
export function useImportVariations() {
  const { t } = useTranslation();
  
  return useMutation({
    mutationFn: (file: File) => importVariations(file),
    onSuccess: (data) => {
      toast.success(data.message);
      
      if (data.data.errors.length > 0) {
        console.warn('[Import] Errors:', data.data.errors);
      }
    },
    onError: (error: any) => {
      toast.error(error.message || t('import.error'));
    },
  });
}
