import { Head, router } from "@inertiajs/react";
import AppLayout from "@/layouts/app-layout";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { StatChip } from "@/components/crm/global-card";
import {
    Banknote,
    Receipt,
    TrendingUp,
    Calendar,
    ArrowUpRight,
    DollarSign,
} from "lucide-react";

type Timeline = {
    label: string;
    month: string;
    invoices: number;
    revenue: number;
};
type Plan = { plan: string; invoices: number; revenue: number };

const money = (v: number) =>
    new Intl.NumberFormat("id-ID", {
        style: "currency",
        currency: "IDR",
        maximumFractionDigits: 0,
    }).format(v);

export default function Revenue({
    months,
    timeline,
    byPlan,
    summary,
}: {
    months: number;
    timeline: Timeline[];
    byPlan: Plan[];
    summary: {
        revenue: number;
        invoices: number;
        average: number;
        thisMonth: number;
    };
}) {
    return (
        <AppLayout
            breadcrumbs={[
                { title: "Pendapatan", href: "/platform/reports/revenue" },
            ]}
        >
            <Head title="Pendapatan Platform" />
            <div className="crm-page space-y-6 pb-10">
                {/* Header Section */}
                <div className="flex flex-col gap-4 md:flex-row md:items-center md:justify-between">
                    <div>
                        <div className="flex items-center gap-2">
                            <span className="flex h-2 w-2 rounded-full bg-emerald-500 animate-pulse" />
                            <span className="text-xs font-bold uppercase tracking-wider text-emerald-600 dark:text-emerald-400">
                                Financial Analytics & Billing
                            </span>
                        </div>
                        <h1 className="text-2xl font-extrabold tracking-tight text-slate-900 dark:text-slate-50 sm:text-3xl mt-1">
                            Pendapatan Platform
                        </h1>
                        <p className="text-xs text-slate-500 dark:text-slate-400 mt-1">
                            Analisis komprehensif pendapatan subscription tenant
                            yang telah dibayar[cite: 21].
                        </p>
                    </div>

                    {/* Range Filter Buttons */}
                    <div className="flex items-center gap-1.5 bg-slate-100 dark:bg-slate-800 p-1 rounded-xl self-start md:self-auto border border-slate-200/80 dark:border-slate-700">
                        {[3, 6, 12].map((n) => (
                            <Button
                                key={n}
                                variant={months === n ? "default" : "ghost"}
                                size="sm"
                                className={`rounded-lg text-xs font-semibold h-8 px-3 transition ${
                                    months === n
                                        ? "bg-white dark:bg-slate-900 text-slate-900 dark:text-slate-100 shadow-2xs font-bold"
                                        : "text-slate-600 dark:text-slate-400 hover:text-slate-900 dark:hover:text-slate-200"
                                }`}
                                onClick={() =>
                                    router.get(
                                        "/platform/reports/revenue",
                                        { months: n },
                                        { preserveState: true },
                                    )
                                }
                            >
                                {n} Bulan
                            </Button>
                        ))}
                    </div>
                </div>

                {/* Metrics Grid using StatChip */}
                <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
                    <StatChip
                        icon={Banknote}
                        label="Total Pendapatan"
                        value={money(summary.revenue)}
                        tone="emerald"
                        variant="compact"
                        description="Akumulasi periode terpilih"
                    />
                    <StatChip
                        icon={Receipt}
                        label="Invoice Lunas"
                        value={summary.invoices.toLocaleString("id-ID")}
                        tone="blue"
                        variant="compact"
                        description="Total transaksi sukses"
                    />
                    <StatChip
                        icon={TrendingUp}
                        label="Rata-rata Invoice"
                        value={money(summary.average)}
                        tone="violet"
                        variant="compact"
                        description="Nilai rata-rata per transaksi"
                    />
                    <StatChip
                        icon={DollarSign}
                        label="Bulan Ini"
                        value={money(summary.thisMonth)}
                        tone="orange"
                        variant="compact"
                        description="Perolehan bulan berjalan"
                    />
                </div>

                {/* Data Tables Grid */}
                <div className="grid gap-6 lg:grid-cols-2">
                    {/* Timeline Table */}
                    <Card className="rounded-2xl border-slate-200/80 bg-white shadow-2xs dark:border-slate-800 dark:bg-slate-900 overflow-hidden flex flex-col">
                        <CardHeader className="border-b border-slate-100 dark:border-slate-800 pb-4">
                            <CardTitle className="text-base font-bold text-slate-900 dark:text-slate-100">
                                Pendapatan Bulanan
                            </CardTitle>
                        </CardHeader>
                        <CardContent className="p-0 flex-1">
                            <div className="overflow-x-auto">
                                <table className="w-full text-xs">
                                    <thead className="border-b border-slate-100 bg-slate-50/50 text-slate-500 dark:border-slate-800 dark:bg-slate-800/50 dark:text-slate-400">
                                        <tr>
                                            <th className="px-5 py-3 text-left font-semibold">
                                                Periode
                                            </th>
                                            <th className="px-5 py-3 text-center font-semibold">
                                                Invoice Lunas
                                            </th>
                                            <th className="px-5 py-3 text-right font-semibold">
                                                Pendapatan
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody className="divide-y divide-slate-100 dark:divide-slate-800">
                                        {timeline.length === 0 ? (
                                            <tr>
                                                <td
                                                    colSpan={3}
                                                    className="px-4 py-12 text-center text-slate-400"
                                                >
                                                    Belum ada data timeline.
                                                </td>
                                            </tr>
                                        ) : (
                                            timeline.map((m) => (
                                                <tr
                                                    key={m.month}
                                                    className="transition hover:bg-slate-50/50 dark:hover:bg-slate-800/50"
                                                >
                                                    <td className="px-5 py-4 font-bold text-slate-900 dark:text-slate-100">
                                                        {m.label}
                                                    </td>
                                                    <td className="px-5 py-4 text-center font-semibold text-slate-700 dark:text-slate-300">
                                                        {m.invoices.toLocaleString(
                                                            "id-ID",
                                                        )}
                                                    </td>
                                                    <td className="px-5 py-4 text-right font-bold text-emerald-600 dark:text-emerald-400 font-mono">
                                                        {money(m.revenue)}
                                                    </td>
                                                </tr>
                                            ))
                                        )}
                                    </tbody>
                                </table>
                            </div>
                        </CardContent>
                    </Card>

                    {/* By Plan Table */}
                    <Card className="rounded-2xl border-slate-200/80 bg-white shadow-2xs dark:border-slate-800 dark:bg-slate-900 overflow-hidden flex flex-col">
                        <CardHeader className="border-b border-slate-100 dark:border-slate-800 pb-4">
                            <CardTitle className="text-base font-bold text-slate-900 dark:text-slate-100">
                                Pendapatan berdasarkan Paket
                            </CardTitle>
                        </CardHeader>
                        <CardContent className="p-0 flex-1">
                            <div className="overflow-x-auto">
                                <table className="w-full text-xs">
                                    <thead className="border-b border-slate-100 bg-slate-50/50 text-slate-500 dark:border-slate-800 dark:bg-slate-800/50 dark:text-slate-400">
                                        <tr>
                                            <th className="px-5 py-3 text-left font-semibold">
                                                Paket
                                            </th>
                                            <th className="px-5 py-3 text-center font-semibold">
                                                Invoice
                                            </th>
                                            <th className="px-5 py-3 text-right font-semibold">
                                                Pendapatan
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody className="divide-y divide-slate-100 dark:divide-slate-800">
                                        {byPlan.length === 0 ? (
                                            <tr>
                                                <td
                                                    colSpan={3}
                                                    className="px-4 py-12 text-center text-slate-400"
                                                >
                                                    Belum ada data paket.
                                                </td>
                                            </tr>
                                        ) : (
                                            byPlan.map((p) => (
                                                <tr
                                                    key={p.plan}
                                                    className="transition hover:bg-slate-50/50 dark:hover:bg-slate-800/50"
                                                >
                                                    <td className="px-5 py-4 font-bold text-slate-900 dark:text-slate-100">
                                                        {p.plan}
                                                    </td>
                                                    <td className="px-5 py-4 text-center font-semibold text-slate-700 dark:text-slate-300">
                                                        {p.invoices.toLocaleString(
                                                            "id-ID",
                                                        )}
                                                    </td>
                                                    <td className="px-5 py-4 text-right font-bold text-indigo-600 dark:text-indigo-400 font-mono">
                                                        {money(p.revenue)}
                                                    </td>
                                                </tr>
                                            ))
                                        )}
                                    </tbody>
                                </table>
                            </div>
                        </CardContent>
                    </Card>
                </div>
            </div>
        </AppLayout>
    );
}
