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 {
    Users,
    UserRound,
    Package,
    ShoppingCart,
    MapPin,
    Activity,
    Building2,
} from "lucide-react";

type Timeline = {
    label: string;
    month: string;
    users: number;
    customers: number;
    products: number;
    orders: number;
    visits: number;
};

type Company = {
    id: number;
    nama: string;
    kode_perusahaan: string;
    users_count: number;
    customers_count: number;
    products_count: number;
    orders_count: number;
    visits_count: number;
};

export default function Usage({
    months,
    timeline,
    companies,
    summary,
}: {
    months: number;
    timeline: Timeline[];
    companies: Company[];
    summary: {
        users: number;
        customers: number;
        products: number;
        orders: number;
        visits: number;
    };
}) {
    return (
        <AppLayout
            breadcrumbs={[
                { title: "Penggunaan Sistem", href: "/platform/reports/usage" },
            ]}
        >
            <Head title="Penggunaan Sistem" />
            <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-indigo-500 animate-pulse" />
                            <span className="text-xs font-bold uppercase tracking-wider text-indigo-600 dark:text-indigo-400">
                                System Utilization & Metrics
                            </span>
                        </div>
                        <h1 className="text-2xl font-extrabold tracking-tight text-slate-900 dark:text-slate-50 sm:text-3xl mt-1">
                            Penggunaan Sistem
                        </h1>
                        <p className="text-xs text-slate-500 dark:text-slate-400 mt-1">
                            Pantau aktivitas platform, adopsi fitur, dan volume
                            transaksi berdasarkan periode[cite: 17].
                        </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/usage",
                                        { months: n },
                                        { preserveState: true },
                                    )
                                }
                            >
                                {n} Bulan
                            </Button>
                        ))}
                    </div>
                </div>

                {/* Metrics Grid using StatChip (5 Columns Layout) */}
                <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-5">
                    <StatChip
                        icon={Users}
                        label="Total User"
                        value={summary.users.toLocaleString("id-ID")}
                        tone="blue"
                        variant="compact"
                        description="Pengguna terdaftar"
                    />
                    <StatChip
                        icon={UserRound}
                        label="Total Customer"
                        value={summary.customers.toLocaleString("id-ID")}
                        tone="emerald"
                        variant="compact"
                        description="Basis data pelanggan"
                    />
                    <StatChip
                        icon={Package}
                        label="Total Produk"
                        value={summary.products.toLocaleString("id-ID")}
                        tone="violet"
                        variant="compact"
                        description="Katalog produk aktif"
                    />
                    <StatChip
                        icon={ShoppingCart}
                        label="Total Order"
                        value={summary.orders.toLocaleString("id-ID")}
                        tone="orange"
                        variant="compact"
                        description="Transaksi keseluruhan"
                    />
                    <StatChip
                        icon={MapPin}
                        label="Total Visit"
                        value={summary.visits.toLocaleString("id-ID")}
                        tone="blue"
                        variant="compact"
                        description="Aktivitas kunjungan"
                    />
                </div>

                {/* Timeline Table Card */}
                <Card className="rounded-2xl border-slate-200/80 bg-white shadow-2xs dark:border-slate-800 dark:bg-slate-900 overflow-hidden">
                    <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 flex items-center gap-2">
                            <Activity className="size-4 text-indigo-600 dark:text-indigo-400" />
                            Aktivitas Bulanan
                        </CardTitle>
                    </CardHeader>
                    <CardContent className="p-0">
                        <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">
                                            User Baru
                                        </th>
                                        <th className="px-5 py-3 text-center font-semibold">
                                            Customer Baru
                                        </th>
                                        <th className="px-5 py-3 text-center font-semibold">
                                            Produk Baru
                                        </th>
                                        <th className="px-5 py-3 text-center font-semibold">
                                            Order
                                        </th>
                                        <th className="px-5 py-3 text-center font-semibold">
                                            Visit
                                        </th>
                                    </tr>
                                </thead>
                                <tbody className="divide-y divide-slate-100 dark:divide-slate-800">
                                    {timeline.length === 0 ? (
                                        <tr>
                                            <td
                                                colSpan={6}
                                                className="px-4 py-12 text-center text-slate-400"
                                            >
                                                Belum ada data aktivitas
                                                bulanan.
                                            </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.users.toLocaleString(
                                                        "id-ID",
                                                    )}
                                                </td>
                                                <td className="px-5 py-4 text-center font-semibold text-slate-700 dark:text-slate-300">
                                                    {m.customers.toLocaleString(
                                                        "id-ID",
                                                    )}
                                                </td>
                                                <td className="px-5 py-4 text-center font-semibold text-slate-700 dark:text-slate-300">
                                                    {m.products.toLocaleString(
                                                        "id-ID",
                                                    )}
                                                </td>
                                                <td className="px-5 py-4 text-center font-semibold text-slate-700 dark:text-slate-300">
                                                    {m.orders.toLocaleString(
                                                        "id-ID",
                                                    )}
                                                </td>
                                                <td className="px-5 py-4 text-center font-semibold text-slate-700 dark:text-slate-300">
                                                    {m.visits.toLocaleString(
                                                        "id-ID",
                                                    )}
                                                </td>
                                            </tr>
                                        ))
                                    )}
                                </tbody>
                            </table>
                        </div>
                    </CardContent>
                </Card>

                {/* Companies Usage Table Card */}
                <Card className="rounded-2xl border-slate-200/80 bg-white shadow-2xs dark:border-slate-800 dark:bg-slate-900 overflow-hidden">
                    <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 flex items-center gap-2">
                            <Building2 className="size-4 text-emerald-600 dark:text-emerald-400" />
                            Penggunaan per Perusahaan
                        </CardTitle>
                    </CardHeader>
                    <CardContent className="p-0">
                        <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">
                                            Perusahaan
                                        </th>
                                        <th className="px-5 py-3 text-center font-semibold">
                                            User
                                        </th>
                                        <th className="px-5 py-3 text-center font-semibold">
                                            Customer
                                        </th>
                                        <th className="px-5 py-3 text-center font-semibold">
                                            Produk
                                        </th>
                                        <th className="px-5 py-3 text-center font-semibold">
                                            Order
                                        </th>
                                        <th className="px-5 py-3 text-center font-semibold">
                                            Visit
                                        </th>
                                    </tr>
                                </thead>
                                <tbody className="divide-y divide-slate-100 dark:divide-slate-800">
                                    {companies.length === 0 ? (
                                        <tr>
                                            <td
                                                colSpan={6}
                                                className="px-4 py-12 text-center text-slate-400"
                                            >
                                                Belum ada data perusahaan.
                                            </td>
                                        </tr>
                                    ) : (
                                        companies.map((c) => (
                                            <tr
                                                key={c.id}
                                                className="transition hover:bg-slate-50/50 dark:hover:bg-slate-800/50"
                                            >
                                                <td className="px-5 py-4">
                                                    <div className="font-bold text-slate-900 dark:text-slate-100 text-sm">
                                                        {c.nama}
                                                    </div>
                                                    <div className="text-[11px] font-mono text-slate-400 mt-0.5">
                                                        {c.kode_perusahaan}
                                                    </div>
                                                </td>
                                                <td className="px-5 py-4 text-center font-semibold text-slate-700 dark:text-slate-300">
                                                    {c.users_count.toLocaleString(
                                                        "id-ID",
                                                    )}
                                                </td>
                                                <td className="px-5 py-4 text-center font-semibold text-slate-700 dark:text-slate-300">
                                                    {c.customers_count.toLocaleString(
                                                        "id-ID",
                                                    )}
                                                </td>
                                                <td className="px-5 py-4 text-center font-semibold text-slate-700 dark:text-slate-300">
                                                    {c.products_count.toLocaleString(
                                                        "id-ID",
                                                    )}
                                                </td>
                                                <td className="px-5 py-4 text-center font-semibold text-slate-700 dark:text-slate-300">
                                                    {c.orders_count.toLocaleString(
                                                        "id-ID",
                                                    )}
                                                </td>
                                                <td className="px-5 py-4 text-center font-semibold text-slate-700 dark:text-slate-300">
                                                    {c.visits_count.toLocaleString(
                                                        "id-ID",
                                                    )}
                                                </td>
                                            </tr>
                                        ))
                                    )}
                                </tbody>
                            </table>
                        </div>
                    </CardContent>
                </Card>
            </div>
        </AppLayout>
    );
}
