import { Head, Link } from '@inertiajs/react';
import { ArrowDownToLine, Banknote, PiggyBank, Plus } from 'lucide-react';
import DataTable from '@/components/data-table';
import type { DataTableColumn } from '@/components/data-table';
import MemberPage from '@/components/member/member-page';
import StatusBadge from '@/components/member/status-badge';
import { formatDateTime, formatNaira } from '@/lib/format';
import { create, index } from '@/routes/admin/deposits';
import { show as userShow } from '@/routes/admin/users';
import { show as receiptShow } from '@/routes/receipts';
import type { Paginated } from '@/types';

type Deposit = {
    id: number;
    reference: string;
    amount: number;
    createdAt: string | null;
    member: { id: number; name: string; memberId: string };
    paymentMethod: string | null;
    recordedBy: string | null;
    intoPlan: boolean;
    note: string | null;
};

export default function DepositsIndex({
    deposits,
    todayTotal,
}: {
    deposits: Paginated<Deposit>;
    todayTotal: number;
}) {
    const columns: DataTableColumn<Deposit>[] = [
        {
            key: 'member',
            header: 'Member',
            cell: (deposit) => (
                <Link
                    href={userShow(deposit.member.id)}
                    className="group block min-w-0"
                >
                    <span className="block truncate font-semibold text-ink group-hover:text-brand">
                        {deposit.member.name}
                    </span>
                    <span className="block truncate font-mono text-xs text-ink-400">
                        {deposit.member.memberId}
                    </span>
                </Link>
            ),
        },
        {
            key: 'amount',
            header: 'Amount',
            sortable: true,
            align: 'right',
            className: 'font-semibold whitespace-nowrap text-ink tabular-nums',
            cell: (deposit) => formatNaira(deposit.amount),
        },
        {
            key: 'paymentMethod',
            header: 'Method',
            hideOnMobile: true,
            cell: (deposit) => (
                <span className="flex flex-wrap items-center gap-1">
                    <StatusBadge tone="neutral">
                        {deposit.paymentMethod ?? '—'}
                    </StatusBadge>
                    {deposit.intoPlan && (
                        <StatusBadge tone="success">
                            <PiggyBank className="mr-1 size-3" />
                            Savings
                        </StatusBadge>
                    )}
                </span>
            ),
        },
        {
            key: 'reference',
            header: 'Reference',
            hideOnMobile: true,
            cell: (deposit) => (
                <span className="block">
                    <Link
                        href={receiptShow(deposit.id)}
                        className="block font-mono text-xs text-ink-700 hover:text-brand"
                    >
                        {deposit.reference}
                    </Link>
                    {deposit.note && (
                        <span className="block max-w-56 truncate text-xs text-ink-400">
                            {deposit.note}
                        </span>
                    )}
                </span>
            ),
        },
        {
            key: 'recordedBy',
            header: 'Recorded by',
            hideOnMobile: true,
            className: 'text-xs text-ink-500',
            cell: (deposit) => deposit.recordedBy ?? '—',
        },
        {
            key: 'createdAt',
            header: 'Date',
            sortable: true,
            className: 'text-xs whitespace-nowrap text-ink-500',
            cell: (deposit) => formatDateTime(deposit.createdAt),
        },
    ];

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

            <MemberPage
                title="Deposits"
                description="Money members hand in at the office: cash, POS or a confirmed bank transfer."
                actions={
                    <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" />
                        Record deposit
                    </Link>
                }
            >
                <section className="flex items-center gap-3 rounded-landing-lg border border-surface-200 bg-white p-4 shadow-landing-sm sm:max-w-sm">
                    <span className="flex size-10 items-center justify-center rounded-landing bg-highlight-faint text-highlight-dark">
                        <Banknote className="size-5" />
                    </span>
                    <div>
                        <p className="text-xs font-semibold tracking-widest text-ink-400 uppercase">
                            Recorded today
                        </p>
                        <p className="font-display text-xl font-semibold text-ink tabular-nums">
                            {formatNaira(todayTotal)}
                        </p>
                    </div>
                </section>

                <DataTable
                    title="Recorded deposits"
                    description={`${deposits.total} in total`}
                    columns={columns}
                    page={deposits}
                    getRowId={(deposit) => deposit.id}
                    searchable
                    searchPlaceholder="Search member, ADS-ID or reference"
                    defaultSort={{ key: 'createdAt', direction: 'desc' }}
                    empty={{
                        icon: ArrowDownToLine,
                        title: 'No deposits recorded yet',
                        description:
                            'Use “Record deposit” when a member pays in at the office.',
                    }}
                />
            </MemberPage>
        </>
    );
}

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