// components/staff/InvoicePdfViewerModal.tsx
"use client";

import React, { useState, useEffect, useCallback } from "react";
import { X, ChevronLeft, ChevronRight, FileText, AlertCircle, CheckCircle, Download, ExternalLink } from "lucide-react";

type InvoicePdf = {
  id: string;
  numero?: string;
  organizationName?: string;
  periode?: string;
  montantHT?: number;
  montantTTC?: number;
  statut?: string;
  pdfUrl?: string;
  hasError: boolean;
  errorMessage?: string;
};

type InvoicePdfViewerModalProps = {
  isOpen: boolean;
  onClose: () => void;
  invoices: InvoicePdf[];
  isLoading: boolean;
};

const fmtEUR = new Intl.NumberFormat("fr-FR", { style: "currency", currency: "EUR" });

function getStatusBadge(statut?: string) {
  if (!statut) return null;
  
  if (statut === "payee") {
    return (
      <span className="inline-flex items-center gap-1 px-2 py-1 text-xs bg-emerald-100 text-emerald-800 rounded-full">
        <span className="w-2 h-2 rounded-full bg-emerald-500" />
        Payée
      </span>
    );
  }
  if (statut === "annulee") {
    return (
      <span className="inline-flex items-center gap-1 px-2 py-1 text-xs bg-rose-100 text-rose-800 rounded-full">
        <span className="w-2 h-2 rounded-full bg-rose-500" />
        Annulée
      </span>
    );
  }
  if (statut === "en_cours") {
    return (
      <span className="inline-flex items-center gap-1 px-2 py-1 text-xs bg-blue-100 text-blue-800 rounded-full">
        <span className="w-2 h-2 rounded-full bg-blue-500" />
        En cours
      </span>
    );
  }
  if (statut === "prete") {
    return (
      <span className="inline-flex items-center gap-1 px-2 py-1 text-xs bg-green-100 text-green-800 rounded-full">
        <span className="w-2 h-2 rounded-full bg-green-500" />
        Prête
      </span>
    );
  }
  return (
    <span className="inline-flex items-center gap-1 px-2 py-1 text-xs bg-slate-100 text-slate-800 rounded-full">
      <span className="w-2 h-2 rounded-full bg-slate-500" />
      Émise
    </span>
  );
}

export default function InvoicePdfViewerModal({
  isOpen,
  onClose,
  invoices,
  isLoading
}: InvoicePdfViewerModalProps) {
  const [currentIndex, setCurrentIndex] = useState(0);
  const [pdfError, setPdfError] = useState(false);

  const currentInvoice = invoices[currentIndex];
  const hasInvoices = invoices.length > 0;

  // Reset index when invoices change
  useEffect(() => {
    setCurrentIndex(0);
    setPdfError(false);
  }, [invoices]);

  // Keyboard navigation
  useEffect(() => {
    if (!isOpen) return;

    const handleKeyDown = (e: KeyboardEvent) => {
      if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
        e.preventDefault();
        goToPrevious();
      } else if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
        e.preventDefault();
        goToNext();
      } else if (e.key === 'Escape') {
        e.preventDefault();
        onClose();
      }
    };

    document.addEventListener('keydown', handleKeyDown);
    return () => document.removeEventListener('keydown', handleKeyDown);
  }, [isOpen, currentIndex, invoices.length]);

  const goToPrevious = useCallback(() => {
    if (invoices.length === 0) return;
    setCurrentIndex(prev => prev > 0 ? prev - 1 : invoices.length - 1);
    setPdfError(false);
  }, [invoices.length]);

  const goToNext = useCallback(() => {
    if (invoices.length === 0) return;
    setCurrentIndex(prev => prev < invoices.length - 1 ? prev + 1 : 0);
    setPdfError(false);
  }, [invoices.length]);

  const goToIndex = useCallback((index: number) => {
    setCurrentIndex(index);
    setPdfError(false);
  }, []);

  const handlePdfError = useCallback(() => {
    setPdfError(true);
  }, []);

  const openPdfInNewTab = useCallback(() => {
    if (currentInvoice?.pdfUrl) {
      window.open(currentInvoice.pdfUrl, '_blank');
    }
  }, [currentInvoice?.pdfUrl]);

  const downloadPdf = useCallback(() => {
    if (currentInvoice?.pdfUrl) {
      const link = document.createElement('a');
      link.href = currentInvoice.pdfUrl;
      link.download = `facture_${currentInvoice.numero || currentInvoice.id}.pdf`;
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  }, [currentInvoice]);

  if (!isOpen) return null;

  return (
    <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
      <div className="bg-white rounded-lg w-full h-full max-w-7xl max-h-[95vh] flex flex-col">
        {/* Header */}
        <div className="flex items-center justify-between p-4 border-b bg-gray-50">
          <div className="flex items-center gap-3">
            <FileText className="size-5 text-blue-600" />
            <div>
              <h2 className="text-lg font-semibold">Visualisation des PDFs de factures</h2>
              {hasInvoices && (
                <p className="text-sm text-gray-600">
                  {currentIndex + 1} sur {invoices.length} factures
                </p>
              )}
            </div>
          </div>
          
          <div className="flex items-center gap-2">
            {currentInvoice && !currentInvoice.hasError && currentInvoice.pdfUrl && (
              <>
                <button
                  onClick={downloadPdf}
                  className="p-2 hover:bg-gray-200 rounded-lg transition-colors"
                  title="Télécharger le PDF"
                >
                  <Download className="size-4" />
                </button>
                <button
                  onClick={openPdfInNewTab}
                  className="p-2 hover:bg-gray-200 rounded-lg transition-colors"
                  title="Ouvrir dans un nouvel onglet"
                >
                  <ExternalLink className="size-4" />
                </button>
              </>
            )}
            <button
              onClick={onClose}
              className="p-2 hover:bg-gray-200 rounded-lg transition-colors"
            >
              <X className="size-5" />
            </button>
          </div>
        </div>

        <div className="flex flex-1 overflow-hidden">
          {/* Sidebar with invoice list */}
          <div className="w-80 border-r bg-gray-50 flex flex-col">
            <div className="p-3 border-b bg-white">
              <h3 className="font-medium text-sm text-gray-700">Liste des factures</h3>
            </div>
            
            <div className="flex-1 overflow-y-auto">
              {isLoading ? (
                <div className="p-4 text-center text-gray-500">
                  <div className="animate-spin rounded-full h-6 w-6 border-b-2 border-blue-600 mx-auto mb-2"></div>
                  Chargement des PDFs...
                </div>
              ) : invoices.length === 0 ? (
                <div className="p-4 text-center text-gray-500">
                  <AlertCircle className="size-8 mx-auto mb-2 text-gray-400" />
                  <p className="text-sm">Aucune facture sélectionnée</p>
                </div>
              ) : (
                <div className="space-y-1 p-2">
                  {invoices.map((invoice, index) => (
                    <button
                      key={invoice.id}
                      onClick={() => goToIndex(index)}
                      className={`w-full text-left p-3 rounded-lg transition-colors ${
                        index === currentIndex
                          ? 'bg-blue-100 border border-blue-200'
                          : 'hover:bg-gray-100'
                      }`}
                    >
                      <div className="flex items-start gap-2">
                        <div className="flex-shrink-0 mt-0.5">
                          {invoice.hasError ? (
                            <AlertCircle className="size-4 text-red-500" />
                          ) : invoice.pdfUrl ? (
                            <CheckCircle className="size-4 text-green-500" />
                          ) : (
                            <div className="size-4 rounded-full border-2 border-gray-300"></div>
                          )}
                        </div>
                        <div className="flex-1 min-w-0">
                          <div className="font-medium text-sm truncate">
                            {invoice.numero || `Facture ${index + 1}`}
                          </div>
                          <div className="text-xs text-gray-500 truncate">
                            {invoice.organizationName || 'Client non disponible'}
                          </div>
                          {invoice.periode && (
                            <div className="text-xs text-gray-500 truncate">
                              {invoice.periode}
                            </div>
                          )}
                          <div className="mt-1 flex items-center gap-2">
                            {getStatusBadge(invoice.statut)}
                          </div>
                          {invoice.montantTTC !== undefined && (
                            <div className="text-xs font-medium text-blue-600 mt-1">
                              {fmtEUR.format(invoice.montantTTC)}
                            </div>
                          )}
                          {invoice.hasError && (
                            <div className="text-xs text-red-500 truncate mt-1">
                              {invoice.errorMessage || 'Erreur PDF'}
                            </div>
                          )}
                        </div>
                      </div>
                    </button>
                  ))}
                </div>
              )}
            </div>
          </div>

          {/* Main content - PDF viewer or error message */}
          <div className="flex-1 flex flex-col">
            {/* Invoice summary */}
            {currentInvoice && (
              <div className="p-4 border-b bg-gray-50">
                <div className="grid grid-cols-2 md:grid-cols-4 gap-4">
                  <div>
                    <div className="text-xs text-gray-500 mb-1">Numéro</div>
                    <div className="font-medium">{currentInvoice.numero || '—'}</div>
                  </div>
                  <div>
                    <div className="text-xs text-gray-500 mb-1">Client</div>
                    <div className="font-medium truncate">{currentInvoice.organizationName || '—'}</div>
                  </div>
                  <div>
                    <div className="text-xs text-gray-500 mb-1">Période</div>
                    <div className="font-medium">{currentInvoice.periode || '—'}</div>
                  </div>
                  <div>
                    <div className="text-xs text-gray-500 mb-1">Statut</div>
                    <div>{getStatusBadge(currentInvoice.statut)}</div>
                  </div>
                </div>
                <div className="grid grid-cols-2 gap-4 mt-3">
                  <div>
                    <div className="text-xs text-gray-500 mb-1">Montant HT</div>
                    <div className="font-medium text-blue-600">
                      {currentInvoice.montantHT !== undefined ? fmtEUR.format(currentInvoice.montantHT) : '—'}
                    </div>
                  </div>
                  <div>
                    <div className="text-xs text-gray-500 mb-1">Montant TTC</div>
                    <div className="font-medium text-blue-600">
                      {currentInvoice.montantTTC !== undefined ? fmtEUR.format(currentInvoice.montantTTC) : '—'}
                    </div>
                  </div>
                </div>
              </div>
            )}

            {/* PDF viewer */}
            <div className="flex-1 bg-gray-100 relative overflow-hidden">
              {!hasInvoices ? (
                <div className="flex items-center justify-center h-full">
                  <div className="text-center text-gray-500">
                    <FileText className="size-12 mx-auto mb-3 text-gray-400" />
                    <p>Aucune facture à afficher</p>
                  </div>
                </div>
              ) : currentInvoice?.hasError ? (
                <div className="flex items-center justify-center h-full">
                  <div className="text-center text-red-500">
                    <AlertCircle className="size-12 mx-auto mb-3" />
                    <p className="font-medium">Erreur lors du chargement du PDF</p>
                    <p className="text-sm text-gray-600 mt-2">
                      {currentInvoice.errorMessage || 'Le PDF n\'est pas disponible'}
                    </p>
                  </div>
                </div>
              ) : !currentInvoice?.pdfUrl ? (
                <div className="flex items-center justify-center h-full">
                  <div className="text-center text-gray-500">
                    <FileText className="size-12 mx-auto mb-3 text-gray-400" />
                    <p>PDF non disponible pour cette facture</p>
                  </div>
                </div>
              ) : pdfError ? (
                <div className="flex items-center justify-center h-full">
                  <div className="text-center text-red-500">
                    <AlertCircle className="size-12 mx-auto mb-3" />
                    <p className="font-medium">Impossible de charger le PDF</p>
                    <button
                      onClick={() => setPdfError(false)}
                      className="mt-4 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors"
                    >
                      Réessayer
                    </button>
                    <button
                      onClick={openPdfInNewTab}
                      className="mt-2 ml-2 px-4 py-2 bg-gray-600 text-white rounded-lg hover:bg-gray-700 transition-colors"
                    >
                      Ouvrir dans un nouvel onglet
                    </button>
                  </div>
                </div>
              ) : (
                <iframe
                  src={currentInvoice.pdfUrl}
                  className="w-full h-full border-0"
                  title={`PDF Facture ${currentInvoice.numero || currentInvoice.id}`}
                  onError={handlePdfError}
                />
              )}

              {/* Navigation arrows */}
              {hasInvoices && invoices.length > 1 && (
                <>
                  <button
                    onClick={goToPrevious}
                    className="absolute left-4 top-1/2 -translate-y-1/2 p-3 bg-white rounded-full shadow-lg hover:bg-gray-50 transition-colors"
                    title="Facture précédente (←)"
                  >
                    <ChevronLeft className="size-6" />
                  </button>
                  <button
                    onClick={goToNext}
                    className="absolute right-4 top-1/2 -translate-y-1/2 p-3 bg-white rounded-full shadow-lg hover:bg-gray-50 transition-colors"
                    title="Facture suivante (→)"
                  >
                    <ChevronRight className="size-6" />
                  </button>
                </>
              )}
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}
