import { Head } 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 { trialBalance } from '@/routes/admin/accounting';
import type { AccountRow } from './trial-balance';

export default function IncomeExpenditure({
    from,
    to,
    income,
    expenses,
    totalIncome,
    totalExpenses,
    surplus,
}: {
    from: string;
    to: string;
    income: AccountRow[];
    expenses: AccountRow[];
    totalIncome: number;
    totalExpenses: number;
    surplus: number;
}) {
    return (
        <>
            <Head title="Income and expenditure" />

            <MemberPage
                title="Income and expenditure"
                description={`${formatDate(from)} – ${formatDate(to)}`}
            >
                <AccountingNav
                    current="income-expenditure"
                    dates={{ from, to }}
                />

                <div className="grid gap-5 lg:grid-cols-2">
                    <Statement
                        title="Income"
                        rows={income}
                        total={totalIncome}
                        empty="No income in this period."
                    />
                    <Statement
                        title="Expenditure"
                        rows={expenses}
                        total={totalExpenses}
                        empty="No expenses in this period."
                    />
                </div>

                <section
                    className={cn(
                        'flex items-center justify-between rounded-landing-lg border-2 bg-white p-5 shadow-landing-sm',
                        surplus >= 0 ? 'border-highlight/50' : 'border-red-300',
                    )}
                >
                    <span className="font-display text-base font-semibold text-ink">
                        {surplus >= 0
                            ? 'Surplus for the period'
                            : 'Deficit for the period'}
                    </span>
                    <span
                        className={cn(
                            'font-display text-xl font-semibold tabular-nums',
                            surplus >= 0
                                ? 'text-highlight-dark'
                                : 'text-red-700',
                        )}
                    >
                        {formatNaira(Math.abs(surplus))}
                    </span>
                </section>
            </MemberPage>
        </>
    );
}

export function Statement({
    title,
    rows,
    total,
    empty,
    extra,
}: {
    title: string;
    rows: AccountRow[];
    total: number;
    empty: string;
    extra?: { name: string; amount: number }[];
}) {
    return (
        <section className="rounded-landing-lg border border-surface-200 bg-white p-5 shadow-landing-sm">
            <h2 className="font-display text-base font-semibold text-ink">
                {title}
            </h2>
            {rows.length === 0 && !extra?.length ? (
                <p className="mt-2 text-sm text-ink-400">{empty}</p>
            ) : (
                <dl className="mt-3 divide-y divide-surface-100 text-sm tabular-nums">
                    {rows.map((row) => (
                        <div
                            key={row.code}
                            className="flex justify-between gap-3 py-2"
                        >
                            <dt className="text-ink-700">
                                <span className="mr-2 font-mono text-xs text-ink-400">
                                    {row.code}
                                </span>
                                {row.name}
                            </dt>
                            <dd className="text-ink">
                                {formatNaira(row.balance)}
                            </dd>
                        </div>
                    ))}
                    {extra?.map((row) => (
                        <div
                            key={row.name}
                            className="flex justify-between gap-3 py-2"
                        >
                            <dt className="text-ink-700">{row.name}</dt>
                            <dd className="text-ink">
                                {formatNaira(row.amount)}
                            </dd>
                        </div>
                    ))}
                </dl>
            )}
            <p className="mt-2 flex justify-between border-t-2 border-surface-200 pt-2 text-sm font-semibold text-ink tabular-nums">
                <span>Total {title.toLowerCase()}</span>
                <span>{formatNaira(total)}</span>
            </p>
        </section>
    );
}

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