import { Head, Link } from '@inertiajs/react';
import type { LucideIcon } from 'lucide-react';
import {
    AlertTriangle,
    BookOpen,
    Building2,
    CalendarCheck,
    Coins,
    HandCoins,
    PiggyBank,
    Receipt,
    Scale,
    UsersRound,
} from 'lucide-react';
import MemberPage from '@/components/member/member-page';
import {
    balanceSheet,
    cashbook,
    generalLedger,
    incomeExpenditure,
    trialBalance,
} from '@/routes/admin/accounting';
import { index as branchesIndex } from '@/routes/admin/branches';
import { index as contributionsIndex } from '@/routes/admin/contributions';
import { index as expensesIndex } from '@/routes/admin/expenses';
import { index as groupsIndex } from '@/routes/admin/groups';
import { index as incomeIndex } from '@/routes/admin/income';
import { delinquency } from '@/routes/admin/loans';
import { index, loans, savings } from '@/routes/admin/reports';
import { index as transactionsIndex } from '@/routes/admin/transactions';
import { index as usersIndex } from '@/routes/admin/users';

type Can = {
    savings: boolean;
    loans: boolean;
    accounting: boolean;
    members: boolean;
    branches: boolean;
};

type Report = {
    title: string;
    description: string;
    href: ReturnType<typeof index>;
    icon: LucideIcon;
    allowed: keyof Can;
};

const sections: { title: string; reports: Report[] }[] = [
    {
        title: 'Savings',
        reports: [
            {
                title: 'Savings report',
                description:
                    'Deposits, savings and withdrawals by day, week or month, and by member, group or branch.',
                href: savings(),
                icon: PiggyBank,
                allowed: 'savings',
            },
            {
                title: 'Contribution register',
                description: 'Who has paid and who has not, per period.',
                href: contributionsIndex(),
                icon: CalendarCheck,
                allowed: 'savings',
            },
            {
                title: 'Group report',
                description:
                    'Each Smart Save group’s members, savings and contributions.',
                href: groupsIndex(),
                icon: UsersRound,
                allowed: 'savings',
            },
            {
                title: 'Branch report',
                description: 'Staff, members, money held and loans per branch.',
                href: branchesIndex(),
                icon: Building2,
                allowed: 'branches',
            },
        ],
    },
    {
        title: 'Loans',
        reports: [
            {
                title: 'Loan report',
                description:
                    'Applications, approved, rejected, disbursed, outstanding, overdue and repayments.',
                href: loans(),
                icon: HandCoins,
                allowed: 'loans',
            },
            {
                title: 'Delinquency and aging',
                description:
                    'Loan standing and the 30 / 60 / 90 / 90+ day aging report.',
                href: delinquency(),
                icon: AlertTriangle,
                allowed: 'loans',
            },
        ],
    },
    {
        title: 'Financial',
        reports: [
            {
                title: 'Income',
                description:
                    'Interest, fees, penalties, membership fees and other income.',
                href: incomeIndex(),
                icon: Coins,
                allowed: 'accounting',
            },
            {
                title: 'Expenses',
                description: 'Spending by category.',
                href: expensesIndex(),
                icon: Receipt,
                allowed: 'accounting',
            },
            {
                title: 'Cashbook',
                description: 'Money received and paid out.',
                href: cashbook(),
                icon: BookOpen,
                allowed: 'accounting',
            },
            {
                title: 'General ledger',
                description: 'Every entry in one account.',
                href: generalLedger(),
                icon: BookOpen,
                allowed: 'accounting',
            },
            {
                title: 'Trial balance',
                description: 'All account balances at a date.',
                href: trialBalance(),
                icon: Scale,
                allowed: 'accounting',
            },
            {
                title: 'Income and expenditure',
                description: 'Surplus or deficit for a period.',
                href: incomeExpenditure(),
                icon: Scale,
                allowed: 'accounting',
            },
            {
                title: 'Balance sheet',
                description: 'What the association owns and owes.',
                href: balanceSheet(),
                icon: Scale,
                allowed: 'accounting',
            },
            {
                title: 'Transactions',
                description: 'Every wallet entry with its reference.',
                href: transactionsIndex(),
                icon: BookOpen,
                allowed: 'accounting',
            },
            {
                title: 'Member statement',
                description: 'Open a member under Users, then “Statement”.',
                href: usersIndex(),
                icon: UsersRound,
                allowed: 'members',
            },
        ],
    },
];

export default function ReportsIndex({ can }: { can: Can }) {
    const visible = sections
        .map((section) => ({
            ...section,
            reports: section.reports.filter((report) => can[report.allowed]),
        }))
        .filter((section) => section.reports.length > 0);

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

            <MemberPage
                title="Reports"
                description="Every report in one place. Each one can be printed, and most download as Excel, PDF or CSV."
            >
                {visible.map((section) => (
                    <section key={section.title} className="grid gap-3">
                        <h2 className="font-display text-base font-semibold text-ink">
                            {section.title}
                        </h2>
                        <ul className="grid grid-cols-1 gap-3 md:grid-cols-2 xl:grid-cols-3">
                            {section.reports.map((report) => (
                                <li key={report.title}>
                                    <Link
                                        href={report.href}
                                        className="flex h-full gap-3 rounded-landing-lg border border-surface-200 bg-white p-4 shadow-landing-sm transition-colors hover:border-brand/40"
                                    >
                                        <span className="flex size-10 shrink-0 items-center justify-center rounded-landing bg-brand-faint text-brand">
                                            <report.icon className="size-5" />
                                        </span>
                                        <span className="min-w-0">
                                            <span className="block font-semibold text-ink">
                                                {report.title}
                                            </span>
                                            <span className="block text-xs text-ink-500">
                                                {report.description}
                                            </span>
                                        </span>
                                    </Link>
                                </li>
                            ))}
                        </ul>
                    </section>
                ))}
            </MemberPage>
        </>
    );
}

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