import { Button } from '@/components/ui/button';
import {
    DropdownMenu,
    DropdownMenuContent,
    DropdownMenuItem,
    DropdownMenuLabel,
    DropdownMenuSeparator,
    DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { ChevronDown, Download, FileDown, FileSpreadsheet, type LucideIcon } from 'lucide-react';
import { toast } from 'sonner';
import { paramsExport, type FormatExport, type VueExport } from './export-params';

type Traduire = (cle: string, fr: string, vars?: Record<string, string | number>) => string;

interface ExportMenuProps {
    /** Période, onglet, type et recherche affichés : le fichier contient cette vue. */
    vue: VueExport;
    /** Nombre de pièces de la vue — ce que contiendra le fichier. */
    nombre: number;
    tr: Traduire;
}

/**
 * « Exporter » : la liste affichée en PDF ou en Excel, pour la comptabilité.
 * Un simple lien de téléchargement, pas une visite Inertia : la réponse est un
 * fichier (route `factures.export`, FactureExportController).
 */
export function ExportMenu({ vue, nombre, tr }: ExportMenuProps) {
    const formats: { format: FormatExport; icone: LucideIcon; titre: string; description: string }[] = [
        {
            format: 'pdf',
            icone: FileDown,
            titre: tr('liste.export_pdf', 'PDF'),
            description: tr('liste.export_pdf_aide', 'Tableau A4 paysage, prêt à imprimer'),
        },
        {
            format: 'xlsx',
            icone: FileSpreadsheet,
            titre: tr('liste.export_excel', 'Excel'),
            description: tr('liste.export_excel_aide', 'Classeur .xlsx, montants et dates exploitables'),
        },
    ];

    // Une année en PDF prend quelques secondes : on dit que c'est parti.
    const annoncer = () => toast.info(tr('liste.export_en_cours', 'Préparation du fichier — il arrive dans vos téléchargements.'));

    return (
        <DropdownMenu modal={false}>
            <DropdownMenuTrigger asChild>
                <Button type="button" variant="outline" className="bg-white" data-tour="factures-export">
                    <Download className="size-4" />
                    {tr('liste.exporter', 'Exporter')}
                    <ChevronDown className="size-4 opacity-60" />
                </Button>
            </DropdownMenuTrigger>
            <DropdownMenuContent align="end" className="w-72">
                <DropdownMenuLabel className="text-muted-foreground text-xs font-normal">
                    {nombre > 1
                        ? tr('liste.export_nb', ':n pièces de la vue affichée', { n: nombre })
                        : tr('liste.export_nb_un', ':n pièce de la vue affichée', { n: nombre })}
                </DropdownMenuLabel>
                <DropdownMenuSeparator />
                {formats.map(({ format, icone: Icone, titre, description }) => (
                    <DropdownMenuItem key={format} asChild className="items-start gap-3 py-2">
                        <a href={route('factures.export', paramsExport(format, vue))} download onClick={annoncer}>
                            <Icone className="text-primary mt-0.5 size-4" aria-hidden />
                            <span className="grid min-w-0 gap-0.5">
                                <span className="text-sm font-medium">{titre}</span>
                                <span className="text-muted-foreground text-xs leading-snug">{description}</span>
                            </span>
                        </a>
                    </DropdownMenuItem>
                ))}
            </DropdownMenuContent>
        </DropdownMenu>
    );
}
