import { Head, Link } from '@inertiajs/react';
import { Calculator, HandCoins, Plus, ShieldAlert } from 'lucide-react';
import EmptyState from '@/components/member/empty-state';
import { FinancingStatusBadge } from '@/components/member/financing-status';
import MemberPage from '@/components/member/member-page';
import { formatDate, formatNaira } from '@/lib/format';
import { calculator, create, index, show } from '@/routes/financing';
import { show as kycShow } from '@/routes/kyc';
import type { FinancingItem } from '@/types';

const primaryButton =
    'inline-flex items-center gap-2 rounded-full bg-brand px-4 py-2 text-sm font-semibold text-white hover:bg-brand-hover';
const secondaryButton =
    'inline-flex items-center gap-2 rounded-full border border-surface-200 bg-white px-4 py-2 text-sm font-semibold text-ink-700 hover:border-brand-border hover:text-brand';

export default function FinancingIndex({
    financings,
    isVerified,
    hasOpenFinancing,
}: {
    financings: FinancingItem[];
    isVerified: boolean;
    hasOpenFinancing: boolean;
}) {
    const applyButton = !isVerified ? (
        <Link href={kycShow()} className={primaryButton}>
            <ShieldAlert className="size-4" />
            Verify to apply
        </Link>
    ) : (
        !hasOpenFinancing && (
            <Link href={create()} className={primaryButton}>
                <Plus className="size-4" />
                Apply
            </Link>
        )
    );

    return (
        <>
            <Head title="Financing" />

            <MemberPage
                title="Financing"
                description="Transparent financing based on sale, lease and partnership."
                actions={
                    <>
                        <Link href={calculator()} className={secondaryButton}>
                            <Calculator className="size-4" />
                            Calculator
                        </Link>
                        {financings.length > 0 && applyButton}
                    </>
                }
            >
                {financings.length === 0 ? (
                    <EmptyState
                        icon={HandCoins}
                        title="No financing yet"
                        description="Need equipment, stock or help with an emergency? Apply and we'll review it within a few working days."
                        action={applyButton}
                    />
                ) : (
                    <ul className="divide-y divide-surface-100 overflow-hidden rounded-landing-lg border border-surface-200 bg-white">
                        {financings.map((financing) => (
                            <li key={financing.id}>
                                <Link
                                    href={show(financing.id)}
                                    className="flex flex-wrap items-center gap-3 px-5 py-4 hover:bg-surface-50"
                                >
                                    <div className="min-w-0 flex-1">
                                        <p className="text-sm font-semibold text-ink">
                                            {financing.typeLabel}
                                        </p>
                                        <p className="text-xs text-ink-400">
                                            <span className="font-mono">
                                                {financing.reference}
                                            </span>{' '}
                                            ·{' '}
                                            {formatNaira(
                                                financing.approvedAmount ??
                                                    financing.amountRequested,
                                            )}{' '}
                                            · {financing.tenorMonths} months ·
                                            applied{' '}
                                            {formatDate(financing.appliedAt)}
                                        </p>
                                    </div>
                                    <FinancingStatusBadge
                                        financing={financing}
                                    />
                                </Link>
                            </li>
                        ))}
                    </ul>
                )}
            </MemberPage>
        </>
    );
}

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