/**
 * PDF Download Button Component
 * Reusable button for downloading PDFs
 */

import { Button } from './button';
import { Download, FileText, Award, Tag } from 'lucide-react';
import { useState } from 'react';

type PDFType = 'text' | 'certificate' | 'label';

interface PDFDownloadButtonProps {
  type: PDFType;
  data: unknown;
  filename?: string;
  children?: React.ReactNode;
  variant?: 'default' | 'outline' | 'secondary' | 'ghost';
  size?: 'default' | 'sm' | 'lg';
  className?: string;
  onSuccess?: () => void;
  onError?: (error: Error) => void;
}

const iconMap = {
  text: FileText,
  certificate: Award,
  label: Tag,
};

export function PDFDownloadButton({
  type,
  data,
  filename,
  children,
  variant = 'outline',
  size = 'default',
  className = '',
  onSuccess,
  onError,
}: PDFDownloadButtonProps) {
  const [isLoading, setIsLoading] = useState(false);
  const Icon = iconMap[type];

  const handleClick = async () => {
    setIsLoading(true);
    try {
      const { generateTextPDF, generateCertificatePDF, generateLabelPDF } = await import('@/lib/pdf');

      switch (type) {
        case 'text':
          await generateTextPDF(data as any, filename);
          break;
        case 'certificate':
          await generateCertificatePDF(data as any, filename);
          break;
        case 'label':
          await generateLabelPDF(data as any, filename);
          break;
      }
      onSuccess?.();
    } catch (error) {
      console.error('PDF generation failed:', error);
      onError?.(error as Error);
    } finally {
      setIsLoading(false);
    }
  };

  return (
    <Button
      variant={variant}
      size={size}
      onClick={handleClick}
      disabled={isLoading}
      className={className}
    >
      {isLoading ? (
        <>Generating...</>
      ) : (
        <>
          <Icon className="w-4 h-4" />
          {children || <Download className="w-4 h-4" />}
        </>
      )}
    </Button>
  );
}

/**
 * Certificate PDF Download Button
 * Pre-configured for certificate generation
 */
interface CertificateDownloadButtonProps {
  data: {
    title: string;
    recipientName: string;
    description: string;
    date: string;
    certificateNumber: string;
    authorizedBy?: string;
  };
  filename?: string;
  variant?: 'default' | 'outline' | 'secondary' | 'ghost';
  size?: 'default' | 'sm' | 'lg';
  className?: string;
  onSuccess?: () => void;
  onError?: (error: Error) => void;
}

export function CertificateDownloadButton({
  data,
  filename,
  variant = 'default',
  size = 'default',
  className = '',
  onSuccess,
  onError,
}: CertificateDownloadButtonProps) {
  return (
    <PDFDownloadButton
      type="certificate"
      data={data}
      filename={filename}
      variant={variant}
      size={size}
      className={className}
      onSuccess={onSuccess}
      onError={onError}
    >
      <Award className="w-4 h-4" />
      Download Certificate
    </PDFDownloadButton>
  );
}

/**
 * Label PDF Download Button
 * Pre-configured for label generation (plants, batches, etc.)
 */
interface LabelDownloadButtonProps {
  data: {
    title: string;
    subtitle?: string;
    qrCode?: string;
    details: Array<{ label: string; value: string }>;
  };
  filename?: string;
  variant?: 'default' | 'outline' | 'secondary' | 'ghost';
  size?: 'default' | 'sm' | 'lg';
  className?: string;
  icon?: React.ReactNode;
  onSuccess?: () => void;
  onError?: (error: Error) => void;
}

export function LabelDownloadButton({
  data,
  filename,
  variant = 'outline',
  size = 'sm',
  className = '',
  icon,
  onSuccess,
  onError,
}: LabelDownloadButtonProps) {
  return (
    <PDFDownloadButton
      type="label"
      data={data}
      filename={filename}
      variant={variant}
      size={size}
      className={className}
      onSuccess={onSuccess}
      onError={onError}
    >
      {icon || <Tag className="w-3 h-3" />}
    </PDFDownloadButton>
  );
}
