import { Link, router } from '@inertiajs/react';
import { Printer } from 'lucide-react';
import ExportMenu from '@/components/export-menu';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { cn } from '@/lib/utils';
import {
    balanceSheet,
    cashbook,
    generalLedger,
    incomeExpenditure,
    trialBalance,
} from '@/routes/admin/accounting';

const reports = [
    { key: 'trial-balance', title: 'Trial balance', href: trialBalance() },
    {
        key: 'income-expenditure',
        title: 'Income & expenditure',
        href: incomeExpenditure(),
    },
    { key: 'balance-sheet', title: 'Balance sheet', href: balanceSheet() },
    { key: 'general-ledger', title: 'General ledger', href: generalLedger() },
    { key: 'cashbook', title: 'Cashbook', href: cashbook() },
] as const;

export type ReportKey = (typeof reports)[number]['key'];

/**
 * Tabs between the accounting reports, plus the date controls for the
 * current one (a period, or a single "as at" date).
 */
export default function AccountingNav({
    current,
    dates,
    extra,
}: {
    current: ReportKey;
    dates: Record<string, string>;
    extra?: React.ReactNode;
}) {
    const apply = (name: string, value: string) => {
        const query = new URLSearchParams(window.location.search);

        if (value) {
            query.set(name, value);
        } else {
            query.delete(name);
        }

        router.get(
            `${window.location.pathname}?${query}`,
            {},
            { preserveScroll: true, preserveState: true, replace: true },
        );
    };

    const labels: Record<string, string> = {
        from: 'From',
        to: 'To',
        as_at: 'As at',
    };

    return (
        <div className="grid gap-3 print:hidden">
            <nav className="flex flex-wrap gap-1 rounded-full bg-surface-100 p-1 text-sm font-semibold sm:w-fit">
                {reports.map((report) => (
                    <Link
                        key={report.key}
                        href={report.href}
                        className={cn(
                            'rounded-full px-3 py-1.5',
                            report.key === current
                                ? 'bg-white text-ink shadow-landing-sm'
                                : 'text-ink-500 hover:text-ink',
                        )}
                    >
                        {report.title}
                    </Link>
                ))}
            </nav>
            <div className="flex flex-wrap items-end gap-3">
                {Object.entries(dates).map(([name, value]) => (
                    <div key={name} className="grid gap-1">
                        <Label htmlFor={name}>{labels[name] ?? name}</Label>
                        <Input
                            id={name}
                            type="date"
                            value={value}
                            onChange={(event) =>
                                apply(name, event.target.value)
                            }
                            className="h-9"
                        />
                    </div>
                ))}
                {extra}
                {['trial-balance', 'general-ledger', 'cashbook'].includes(
                    current,
                ) && <ExportMenu />}
                <button
                    type="button"
                    onClick={() => window.print()}
                    className="inline-flex h-9 items-center gap-1.5 rounded-md border border-input bg-white px-3 text-sm font-semibold text-ink-700 hover:bg-surface-50"
                >
                    <Printer className="size-4" />
                    Print
                </button>
            </div>
        </div>
    );
}
