import { Head, Link, router } from '@inertiajs/react';
import { AlertCircle, Download } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { useI18n } from '@/hooks/use-i18n';
import { show as invoiceShow } from '@/routes/invoices';
import { dues as duesRoute } from '@/routes/reports';
import { exportMethod as exportDuesRoute } from '@/routes/reports/dues';

type DueInvoice = {
    id: number;
    invoice_number: string;
    due_date: string;
    total_amount: string | number;
    paid_amount: string | number;
    due_amount: string | number;
    status: string;
    student?: { student_code: string; name_bn: string; name_en: string | null; guardian_phone: string | null } | null;
};

export default function DueReport({
    invoices,
    dueType,
    totals,
}: {
    invoices: { data: DueInvoice[] };
    dueType: string;
    totals: { complete_count: number; partial_count: number; total_due: number };
}) {
    const { locale, t, localized } = useI18n();

    const tabs = [
        { key: 'all', label: locale === 'bn' ? 'সকল বকেয়া' : 'All Dues' },
        { key: 'complete', label: locale === 'bn' ? 'সম্পূর্ণ বকেয়া' : 'Complete Due' },
        { key: 'partial', label: locale === 'bn' ? 'আংশিক বকেয়া' : 'Partial Due' },
        { key: 'overdue', label: locale === 'bn' ? 'মেয়াদোত্তীর্ণ' : 'Overdue' },
    ];

    return (
        <>
            <Head title={t('nav.due_reports', 'Due Reports')} />

            <div className="flex flex-col gap-6 p-4 md:p-6">
                <div className="flex flex-wrap items-center justify-between gap-4">
                    <div>
                        <h1 className="text-2xl font-bold tracking-tight">{t('nav.due_reports', 'Due Reports')}</h1>
                        <p className="text-sm text-muted-foreground">
                            {locale === 'bn'
                                ? 'সম্পূর্ণ বকেয়া, আংশিক বকেয়া এবং মেয়াদোত্তীর্ণ ইনভয়েসের তালিকা।'
                                : 'Complete due, partial due, and overdue student invoices.'}
                        </p>
                    </div>
                    <Button variant="outline" asChild>
                        <a href={exportDuesRoute.url({ query: { due_type: dueType } })}>
                            <Download className="mr-1.5 h-4 w-4" />
                            {locale === 'bn' ? 'CSV ডাউনলোড' : 'Export CSV'}
                        </a>
                    </Button>
                </div>

                <div className="grid gap-4 sm:grid-cols-3">
                    <div className="minimal-card p-5">
                        <p className="text-xs text-muted-foreground">{locale === 'bn' ? 'মোট বকেয়া' : 'Total Due'}</p>
                        <p className="mt-1 text-2xl font-bold text-rose-600">৳{totals.total_due.toLocaleString()}</p>
                    </div>
                    <div className="minimal-card p-5">
                        <p className="text-xs text-muted-foreground">{locale === 'bn' ? 'সম্পূর্ণ বকেয়া সংখ্যা' : 'Complete Due Count'}</p>
                        <p className="mt-1 text-2xl font-bold">{totals.complete_count}</p>
                    </div>
                    <div className="minimal-card p-5">
                        <p className="text-xs text-muted-foreground">{locale === 'bn' ? 'আংশিক বকেয়া সংখ্যা' : 'Partial Due Count'}</p>
                        <p className="mt-1 text-2xl font-bold">{totals.partial_count}</p>
                    </div>
                </div>

                <div className="flex flex-wrap gap-2">
                    {tabs.map((item) => (
                        <Button
                            key={item.key}
                            variant={dueType === item.key ? 'default' : 'outline'}
                            size="sm"
                            onClick={() => router.get(duesRoute().url, { due_type: item.key }, { preserveState: true })}
                        >
                            {item.label}
                        </Button>
                    ))}
                </div>

                <div className="minimal-card overflow-x-auto">
                    <table className="w-full text-left text-sm">
                        <thead className="border-b bg-muted/40 text-xs uppercase text-muted-foreground">
                            <tr>
                                <th className="px-4 py-3">{locale === 'bn' ? 'ইনভয়েস' : 'Invoice'}</th>
                                <th className="px-4 py-3">{locale === 'bn' ? 'শিক্ষার্থী' : 'Student'}</th>
                                <th className="px-4 py-3">{locale === 'bn' ? 'অভিভাবকের ফোন' : 'Guardian Phone'}</th>
                                <th className="px-4 py-3">{locale === 'bn' ? 'মোট' : 'Total'}</th>
                                <th className="px-4 py-3">{locale === 'bn' ? 'জমা' : 'Paid'}</th>
                                <th className="px-4 py-3">{locale === 'bn' ? 'বকেয়া' : 'Due'}</th>
                            </tr>
                        </thead>
                        <tbody className="divide-y">
                            {invoices.data.length === 0 ? (
                                <tr>
                                    <td colSpan={6} className="px-4 py-8 text-center text-muted-foreground">
                                        <AlertCircle className="mx-auto mb-2 h-5 w-5 opacity-50" />
                                        {locale === 'bn' ? 'কোনো বকেয়া পাওয়া যায়নি।' : 'No due invoices found.'}
                                    </td>
                                </tr>
                            ) : (
                                invoices.data.map((inv) => (
                                    <tr key={inv.id} className="hover:bg-muted/30">
                                        <td className="px-4 py-3 font-medium">
                                            <Link href={invoiceShow(inv.id).url} className="text-primary hover:underline">
                                                {inv.invoice_number}
                                            </Link>
                                        </td>
                                        <td className="px-4 py-3">
                                            <div className="font-medium">{localized(inv.student, 'name')}</div>
                                            <div className="text-xs text-muted-foreground">{inv.student?.student_code}</div>
                                        </td>
                                        <td className="px-4 py-3 text-muted-foreground">{inv.student?.guardian_phone ?? '—'}</td>
                                        <td className="px-4 py-3">৳{Number(inv.total_amount).toLocaleString()}</td>
                                        <td className="px-4 py-3 text-emerald-600">৳{Number(inv.paid_amount).toLocaleString()}</td>
                                        <td className="px-4 py-3 font-bold text-rose-600">৳{Number(inv.due_amount).toLocaleString()}</td>
                                    </tr>
                                ))
                            )}
                        </tbody>
                    </table>
                </div>
            </div>
        </>
    );
}
