import { Head, Link } from '@inertiajs/react';
import AccountingNav from '@/components/accounting-nav';
import MemberPage from '@/components/member/member-page';
import { formatDate, formatNaira } from '@/lib/format';
import { cn } from '@/lib/utils';
import { generalLedger, trialBalance } from '@/routes/admin/accounting';

export type AccountRow = {
    code: string;
    name: string;
    type: string;
    debit: number;
    credit: number;
    balance: number;
    debitBalance: number;
    creditBalance: number;
};

export default function TrialBalance({
    asAt,
    accounts,
    totals,
}: {
    asAt: string;
    accounts: AccountRow[];
    totals: { debit: number; credit: number };
}) {
    const balanced = totals.debit === totals.credit;

    return (
        <>
            <Head title="Trial balance" />

            <MemberPage
                title="Trial balance"
                description={`Every account's balance as at ${formatDate(asAt)}.`}
            >
                <AccountingNav
                    current="trial-balance"
                    dates={{ as_at: asAt }}
                />

                <section className="overflow-x-auto rounded-landing-lg border border-surface-200 bg-white shadow-landing-sm">
                    <table className="w-full min-w-[560px] text-sm">
                        <thead>
                            <tr className="border-b border-surface-100 text-left text-xs text-ink-400">
                                <th className="px-4 py-3 font-semibold">
                                    Code
                                </th>
                                <th className="px-4 py-3 font-semibold">
                                    Account
                                </th>
                                <th className="px-4 py-3 text-right font-semibold">
                                    Debit
                                </th>
                                <th className="px-4 py-3 text-right font-semibold">
                                    Credit
                                </th>
                            </tr>
                        </thead>
                        <tbody className="divide-y divide-surface-100 tabular-nums">
                            {accounts.map((account) => (
                                <tr key={account.code}>
                                    <td className="px-4 py-2 font-mono text-xs text-ink-500">
                                        {account.code}
                                    </td>
                                    <td className="px-4 py-2">
                                        <Link
                                            href={generalLedger({
                                                query: {
                                                    account: account.code,
                                                    to: asAt,
                                                },
                                            })}
                                            className="font-semibold text-ink hover:text-brand"
                                        >
                                            {account.name}
                                        </Link>
                                        <span className="ml-2 text-xs text-ink-400 capitalize">
                                            {account.type}
                                        </span>
                                    </td>
                                    <td className="px-4 py-2 text-right">
                                        {account.debitBalance
                                            ? formatNaira(account.debitBalance)
                                            : ''}
                                    </td>
                                    <td className="px-4 py-2 text-right">
                                        {account.creditBalance
                                            ? formatNaira(account.creditBalance)
                                            : ''}
                                    </td>
                                </tr>
                            ))}
                        </tbody>
                        <tfoot>
                            <tr
                                className={cn(
                                    'border-t-2 font-semibold tabular-nums',
                                    balanced
                                        ? 'border-highlight/40 text-ink'
                                        : 'border-red-300 text-red-700',
                                )}
                            >
                                <td className="px-4 py-3" colSpan={2}>
                                    {balanced
                                        ? 'Total — balanced'
                                        : 'Total — does not balance'}
                                </td>
                                <td className="px-4 py-3 text-right">
                                    {formatNaira(totals.debit)}
                                </td>
                                <td className="px-4 py-3 text-right">
                                    {formatNaira(totals.credit)}
                                </td>
                            </tr>
                        </tfoot>
                    </table>
                </section>
            </MemberPage>
        </>
    );
}

TrialBalance.layout = {
    breadcrumbs: [{ title: 'Accounting', href: trialBalance() }],
};
