import { Head } from '@inertiajs/react';
import { Printer } from 'lucide-react';
import AppLogoIcon from '@/components/app-logo-icon';
import OrganisationContact from '@/components/organisation-contact';
import { formatDateTime, formatNaira } from '@/lib/format';
import { cn } from '@/lib/utils';
import { index as transactionsIndex } from '@/routes/transactions';
import type { Organisation, RepaymentSplit, TransactionItem } from '@/types';

type Receipt = TransactionItem & {
    organisation: Organisation;
    member: { name: string; memberId: string; accountNumber: string };
    paymentMethod: string;
    authorizedBy: string;
    note: string | null;
    split: RepaymentSplit | null;
};

export default function ReceiptShow({ receipt }: { receipt: Receipt }) {
    const isCredit = receipt.direction === 'credit';
    const rows: [string, string][] = [
        ['Member', receipt.member.name],
        ['Member ID', receipt.member.memberId],
        ['Wallet account', receipt.member.accountNumber],
        ['Transaction', receipt.typeLabel],
        ['Description', receipt.description],
        ['Reference', receipt.reference],
        ['Date', formatDateTime(receipt.createdAt)],
        ['Payment method', receipt.paymentMethod],
        ['Status', receipt.statusLabel],
        ...(receipt.split
            ? ([
                  ['Principal', formatNaira(receipt.split.principal)],
                  ['Interest', formatNaira(receipt.split.interest)],
                  ['Penalty', formatNaira(receipt.split.penalty)],
              ] as [string, string][])
            : []),
        ...(receipt.note
            ? ([['Note', receipt.note]] as [string, string][])
            : []),
        [
            'New balance',
            receipt.balanceAfter === null
                ? '—'
                : formatNaira(receipt.balanceAfter),
        ],
        ['Authorized by', receipt.authorizedBy],
    ];

    return (
        <>
            <Head title={`Receipt ${receipt.receiptNumber}`} />

            <div className="flex flex-1 flex-col items-center gap-4 p-4 sm:p-6">
                <div className="print-hidden flex w-full max-w-lg justify-end">
                    <button
                        type="button"
                        onClick={() => window.print()}
                        className="inline-flex items-center gap-2 rounded-full bg-brand px-4 py-2 text-sm font-semibold text-white hover:bg-brand-hover"
                    >
                        <Printer className="size-4" />
                        Print / save PDF
                    </button>
                </div>

                <article className="w-full max-w-lg rounded-landing-lg border border-surface-200 bg-white p-6 shadow-landing-sm sm:p-8 print:border-0 print:shadow-none">
                    <header className="flex items-start justify-between gap-4 border-b border-dashed border-surface-200 pb-5">
                        <div className="flex items-center gap-3">
                            <span className="flex size-10 items-center justify-center rounded-landing bg-brand text-white">
                                <AppLogoIcon className="size-5" />
                            </span>
                            <div>
                                <p className="font-display text-lg font-semibold text-ink">
                                    {receipt.organisation.name}
                                </p>
                                <p className="text-xs text-ink-400">
                                    Savings &amp; finance cooperative
                                </p>
                                <OrganisationContact
                                    organisation={receipt.organisation}
                                />
                            </div>
                        </div>
                        <div className="text-right">
                            <p className="text-[11px] font-semibold tracking-widest text-ink-400 uppercase">
                                Receipt
                            </p>
                            <p className="font-mono text-sm font-semibold text-ink">
                                {receipt.receiptNumber}
                            </p>
                        </div>
                    </header>

                    <div className="py-6 text-center">
                        <p className="text-sm text-ink-500">
                            {isCredit ? 'Amount received' : 'Amount paid out'}
                        </p>
                        <p
                            className={cn(
                                'mt-1 font-display text-4xl font-semibold tabular-nums',
                                isCredit ? 'text-highlight-dark' : 'text-ink',
                            )}
                        >
                            {formatNaira(receipt.amount)}
                        </p>
                    </div>

                    <dl className="divide-y divide-surface-100 border-y border-surface-100 text-sm">
                        {rows.map(([term, detail]) => (
                            <div
                                key={term}
                                className="flex justify-between gap-4 py-2.5"
                            >
                                <dt className="shrink-0 text-ink-400">
                                    {term}
                                </dt>
                                <dd className="text-right font-medium wrap-break-word text-ink">
                                    {detail}
                                </dd>
                            </div>
                        ))}
                    </dl>

                    <footer className="mt-6 flex items-end justify-between gap-6 text-xs text-ink-400">
                        <p>
                            Keep this receipt for your records. Questions? Quote{' '}
                            {receipt.receiptNumber}.
                        </p>
                        <div className="w-40 shrink-0 text-center">
                            <div className="h-10 border-b border-ink-300" />
                            <p className="mt-1">Signature &amp; stamp</p>
                        </div>
                    </footer>
                </article>
            </div>
        </>
    );
}

ReceiptShow.layout = {
    breadcrumbs: [{ title: 'Transactions', href: transactionsIndex() }],
};
