import { Head, Link } from '@inertiajs/react';
import { ArrowUpFromLine, Plus } from 'lucide-react';
import EmptyState from '@/components/member/empty-state';
import MemberPage from '@/components/member/member-page';
import Pagination from '@/components/member/pagination';
import WithdrawalStatus from '@/components/member/withdrawal-status';
import type { WithdrawalItem } from '@/components/member/withdrawal-status';
import { formatDate, formatNaira } from '@/lib/format';
import { create, index, show } from '@/routes/withdrawals';
import type { Paginated } from '@/types';

export default function WithdrawalsIndex({
    withdrawals,
}: {
    withdrawals: Paginated<WithdrawalItem>;
}) {
    const newButton = (
        <Link
            href={create()}
            className="inline-flex items-center gap-1.5 rounded-full bg-brand px-3.5 py-1.5 text-sm font-semibold text-white hover:bg-brand-hover"
        >
            <Plus className="size-4" />
            Request withdrawal
        </Link>
    );

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

            <MemberPage
                title="Withdrawals"
                description="Take money out of your wallet as cash at the office or a bank transfer."
                actions={newButton}
            >
                {withdrawals.data.length === 0 ? (
                    <EmptyState
                        icon={ArrowUpFromLine}
                        title="No withdrawals yet"
                        description="When you request a withdrawal, the money is held in your wallet until our staff pay it out."
                        action={newButton}
                    />
                ) : (
                    <ul className="divide-y divide-surface-100 overflow-hidden rounded-landing-lg border border-surface-200 bg-white">
                        {withdrawals.data.map((withdrawal) => (
                            <li key={withdrawal.id}>
                                <Link
                                    href={show(withdrawal.id)}
                                    className="flex items-center gap-3 px-4 py-3.5 hover:bg-surface-50 sm:px-5"
                                >
                                    <span className="flex size-9 shrink-0 items-center justify-center rounded-full bg-surface-100 text-ink-500">
                                        <ArrowUpFromLine className="size-4" />
                                    </span>
                                    <span className="min-w-0 flex-1">
                                        <span className="block truncate text-sm font-semibold text-ink">
                                            {withdrawal.reason}
                                        </span>
                                        <span className="block truncate text-xs text-ink-400">
                                            {withdrawal.reference} ·{' '}
                                            {withdrawal.paymentMethodLabel} ·{' '}
                                            {formatDate(withdrawal.requestedAt)}
                                        </span>
                                    </span>
                                    <span className="flex flex-col items-end gap-1">
                                        <span className="text-sm font-semibold text-ink tabular-nums">
                                            {formatNaira(withdrawal.amount)}
                                        </span>
                                        <WithdrawalStatus
                                            status={withdrawal.status}
                                            label={withdrawal.statusLabel}
                                        />
                                    </span>
                                </Link>
                            </li>
                        ))}
                    </ul>
                )}

                <Pagination page={withdrawals} />
            </MemberPage>
        </>
    );
}

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