import { Head } from '@inertiajs/react';
import MemberPage from '@/components/member/member-page';
import StatusBadge from '@/components/member/status-badge';
import { formatDate, formatDateTime, formatNaira } from '@/lib/format';
import { index } from '@/routes/investments';
import type { InvestmentItem, InvestmentProductItem } from '@/types';

export default function InvestmentShow({
    investment,
    product,
}: {
    investment: InvestmentItem;
    product: InvestmentProductItem;
}) {
    const paidOut = investment.status === 'paid_out';
    const result =
        investment.payoutAmount === null
            ? null
            : investment.payoutAmount - investment.amount;

    const rows: [string, string][] = [
        ['Reference', investment.reference],
        ['Contract', investment.contractLabel],
        ['Started', formatDate(investment.startDate)],
        ['Matures', formatDate(investment.maturityDate)],
        ['Your profit share', `${product.memberProfitShare}%`],
        ...(paidOut
            ? ([['Paid out', formatDateTime(investment.paidOutAt)]] as [
                  string,
                  string,
              ][])
            : []),
    ];

    return (
        <>
            <Head title={investment.reference} />

            <MemberPage
                title={investment.productName}
                description={investment.reference}
                actions={
                    <StatusBadge
                        tone={
                            paidOut
                                ? 'success'
                                : investment.hasMatured
                                  ? 'pending'
                                  : 'info'
                        }
                    >
                        {!paidOut && investment.hasMatured
                            ? 'Awaiting payout'
                            : investment.statusLabel}
                    </StatusBadge>
                }
            >
                <div className="grid gap-4 lg:grid-cols-3">
                    <section className="rounded-landing-lg bg-brand-dark p-6 text-white lg:col-span-2">
                        <p className="text-sm text-white/70">
                            {paidOut ? 'Paid to your wallet' : 'Invested'}
                        </p>
                        <p className="mt-2 font-display text-4xl font-semibold tabular-nums">
                            {formatNaira(
                                investment.payoutAmount ?? investment.amount,
                            )}
                        </p>
                        <p className="mt-1 text-sm text-white/70">
                            {result === null
                                ? investment.hasMatured
                                    ? 'Matured — the office will post the final value to your wallet.'
                                    : `Matures on ${formatDate(investment.maturityDate)}.`
                                : `${result >= 0 ? 'Profit' : 'Loss'} of ${formatNaira(Math.abs(result))} on ${formatNaira(investment.amount)}.`}
                        </p>
                    </section>

                    <section className="rounded-landing-lg border border-surface-200 bg-white p-6">
                        <dl className="grid gap-3 text-sm">
                            {rows.map(([term, detail]) => (
                                <div
                                    key={term}
                                    className="flex justify-between gap-4"
                                >
                                    <dt className="text-ink-400">{term}</dt>
                                    <dd className="text-right font-medium text-ink">
                                        {detail}
                                    </dd>
                                </div>
                            ))}
                        </dl>
                    </section>
                </div>

                <section className="rounded-landing-lg border border-surface-200 bg-white p-6 text-sm text-ink-700">
                    <p>{product.description}</p>
                    <p className="mt-2 text-xs text-ink-500">
                        {product.projectionNote}
                    </p>
                </section>
            </MemberPage>
        </>
    );
}

InvestmentShow.layout = {
    breadcrumbs: [{ title: 'Investments', href: index() }],
};
