import { Head, Link, usePage } from '@inertiajs/react';
import { LayoutDashboard, Building2, ArrowRight, PenTool } from 'lucide-react';

type BuiltIn = {
    key: string; name: string; description: string; href: string;
    stats: { label: string; value: string }[];
};
type Dash = { id: number; name: string; slug: string; description: string | null; widgets: number; updated_at: string | null };

export default function Dashboards() {
    const { builtIn, dashboards, canCreate } = usePage().props as unknown as {
        builtIn: BuiltIn[]; dashboards: Dash[]; canCreate: boolean;
    };

    return (
        <>
            <Head title="Dashboards" />
            <div className="mx-auto w-full max-w-6xl px-6 py-8">
                <header className="mb-6 flex items-start justify-between">
                    <div>
                        <h1 className="flex items-center gap-2 text-2xl font-semibold tracking-tight text-foreground">
                            <LayoutDashboard className="size-5 text-primary" /> Dashboards
                        </h1>
                        <p className="mt-1 text-sm text-muted-foreground">Reports and dashboards available to you.</p>
                    </div>
                    {canCreate && (
                        <Link href="/admin/studio"
                            className="inline-flex items-center gap-1.5 rounded-md border border-border px-3 py-1.5 text-sm font-medium hover:bg-muted">
                            <PenTool className="size-3.5" /> Dashboard Studio
                        </Link>
                    )}
                </header>

                {builtIn.length > 0 && (
                    <section className="mb-8">
                        <h2 className="mb-3 text-xs font-semibold uppercase tracking-wide text-muted-foreground">Reports</h2>
                        <div className="grid gap-4 sm:grid-cols-2">
                            {builtIn.map((b) => (
                                <Link key={b.key} href={b.href}
                                    className="group rounded-xl border border-border bg-card p-5 shadow-sm transition hover:border-primary/40 hover:shadow-md">
                                    <div className="mb-3 flex items-start justify-between">
                                        <div className="flex items-center gap-2">
                                            <span className="flex size-9 items-center justify-center rounded-lg bg-primary/10">
                                                <Building2 className="size-5 text-primary" />
                                            </span>
                                            <div>
                                                <p className="font-semibold text-foreground">{b.name}</p>
                                                <p className="text-[11px] text-muted-foreground">Live from Rentec &amp; Salesforce</p>
                                            </div>
                                        </div>
                                        <ArrowRight className="size-4 text-muted-foreground transition group-hover:translate-x-0.5 group-hover:text-primary" />
                                    </div>
                                    <p className="mb-4 text-sm text-muted-foreground">{b.description}</p>
                                    <div className="grid grid-cols-4 gap-2 border-t border-border pt-3">
                                        {b.stats.map((s) => (
                                            <div key={s.label} className="text-center">
                                                <p className="text-lg font-semibold text-foreground">{s.value}</p>
                                                <p className="text-[10px] text-muted-foreground">{s.label}</p>
                                            </div>
                                        ))}
                                    </div>
                                </Link>
                            ))}
                        </div>
                    </section>
                )}

                <section>
                    <h2 className="mb-3 text-xs font-semibold uppercase tracking-wide text-muted-foreground">
                        Custom dashboards
                    </h2>
                    <div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
                        {dashboards.map((d) => (
                            <Link key={d.id} href={`/dashboards/${d.slug}`}
                                className="rounded-xl border border-border bg-card p-4 transition hover:border-primary/40 hover:shadow-sm">
                                <p className="font-medium text-foreground">{d.name}</p>
                                {d.description && <p className="mt-0.5 text-xs text-muted-foreground">{d.description}</p>}
                                <p className="mt-2 text-[11px] text-muted-foreground">
                                    {d.widgets} widgets{d.updated_at ? ` · updated ${d.updated_at}` : ''}
                                </p>
                            </Link>
                        ))}
                        {dashboards.length === 0 && (
                            <p className="col-span-3 rounded-xl border border-dashed border-border p-8 text-center text-sm text-muted-foreground">
                                No custom dashboards yet.{canCreate && ' Build one in the Dashboard Studio.'}
                            </p>
                        )}
                    </div>
                </section>
            </div>
        </>
    );
}
