import { ArrowLeft, FileSpreadsheet, FileText, Filter, Printer } from 'lucide-react';
import Link from '@/Components/Link';
import ModulBilgi from '@/Components/ModulBilgi';

interface Props {
    /** Filtre çekmecesini açar */
    onFiltre: () => void;
    aktifFiltreSayisi?: number;
    /** Verilirse PDF butonu çıkar — sorgu dizesi dahil tam URL */
    pdfUrl?: string | null;
    /** Verilirse Excel (CSV) butonu çıkar */
    excelUrl?: string | null;
    /** Tarayıcı yazdırma penceresini açar */
    yazdirilabilir?: boolean;
    /** Modülün rapor listesine dönüş bağlantısı */
    geriUrl?: string | null;
    /** modulBilgi.ts anahtarı — verilirse filtrenin yanında "?" düğmesi çıkar */
    bilgiKonu?: string;
}

/**
 * Rapor sayfalarının üst aksiyon çubuğu (AppLayout `actions` yuvası için).
 *
 * Tüm raporlarda aynı sırada aynı butonlar bulunur. Tablet ve mobilde
 * yalnızca ikon görünür — sistemdeki diğer ekranlarla aynı kural.
 */
export default function RaporAksiyonlar({
    onFiltre, aktifFiltreSayisi = 0, pdfUrl, excelUrl, yazdirilabilir = true, geriUrl, bilgiKonu,
}: Props) {
    const indir = (url: string) => { window.location.href = url; };

    return (
        <>
            {geriUrl && (
                <Link href={geriUrl} title="Rapor listesine dön"
                    className="inline-flex h-10 w-10 items-center justify-center rounded-lg border border-slate-300 bg-white text-slate-700 shadow-sm transition hover:border-blue-400 hover:bg-blue-50 hover:text-blue-700">
                    <ArrowLeft className="h-4 w-4" />
                </Link>
            )}

            <button type="button" onClick={onFiltre}
                title={`Filtrele${aktifFiltreSayisi > 0 ? ` (${aktifFiltreSayisi} aktif)` : ''}`}
                className="relative inline-flex h-10 w-10 items-center justify-center rounded-lg border border-slate-300 bg-white text-slate-700 shadow-sm transition hover:border-blue-400 hover:bg-blue-50 hover:text-blue-700">
                <Filter className="h-4 w-4" />
                {aktifFiltreSayisi > 0 && (
                    <span className="absolute -right-1.5 -top-1.5 flex h-5 min-w-[20px] items-center justify-center rounded-full bg-blue-600 px-1.5 text-[11px] font-bold text-white ring-2 ring-white">
                        {aktifFiltreSayisi}
                    </span>
                )}
            </button>

            {bilgiKonu && <ModulBilgi konu={bilgiKonu} />}

            <div className="flex-1" />

            {yazdirilabilir && (
                <button type="button" onClick={() => window.print()} title="Yazdır"
                    className="inline-flex h-10 items-center justify-center gap-1.5 rounded-lg border border-slate-300 bg-white px-2.5 text-sm font-semibold text-slate-700 shadow-sm transition hover:bg-slate-50 lg:px-4">
                    <Printer className="h-4 w-4" />
                    <span className="hidden lg:inline">Yazdır</span>
                </button>
            )}

            {excelUrl && (
                <button type="button" onClick={() => indir(excelUrl)} title="Excel (CSV) indir"
                    className="inline-flex h-10 items-center justify-center gap-1.5 rounded-lg border border-emerald-300 bg-white px-2.5 text-sm font-semibold text-emerald-700 shadow-sm transition hover:bg-emerald-50 lg:px-4">
                    <FileSpreadsheet className="h-4 w-4" />
                    <span className="hidden lg:inline">Excel</span>
                </button>
            )}

            {pdfUrl && (
                <button type="button" onClick={() => indir(pdfUrl)} title="PDF indir"
                    className="inline-flex h-10 items-center justify-center gap-1.5 rounded-lg border border-rose-300 bg-white px-2.5 text-sm font-semibold text-rose-700 shadow-sm transition hover:bg-rose-50 lg:px-4">
                    <FileText className="h-4 w-4" />
                    <span className="hidden lg:inline">PDF</span>
                </button>
            )}
        </>
    );
}

/**
 * Sorgu dizesi üretir — boş/null değerleri atar.
 * Rapor indirme bağlantıları ile ekrandaki filtre birebir aynı olsun diye.
 */
export function raporSorgu(params: Record<string, any>): string {
    const sp = new URLSearchParams();
    for (const [k, v] of Object.entries(params)) {
        if (v === undefined || v === null || v === '' || v === false) continue;
        sp.append(k, String(v));
    }
    const s = sp.toString();
    return s ? `?${s}` : '';
}
