import { Head, Link, router, usePage } from '@inertiajs/react';
import { useState } from 'react';
import { PenTool, Plus } from 'lucide-react';

type Dash = { id: number; name: string; slug: string; status: string; assignments: number };

const STATUS: Record<string, string> = {
    draft: 'bg-muted text-muted-foreground',
    published: 'bg-emerald-100 text-emerald-700 dark:bg-emerald-500/15 dark:text-emerald-300',
    archived: 'bg-amber-100 text-amber-700 dark:bg-amber-500/15 dark:text-amber-300',
};

export default function StudioIndex() {
    const { dashboards } = usePage().props as unknown as { dashboards: Dash[] };
    const [name, setName] = useState('');

    return (
        <>
            <Head title="Dashboard Studio" />
            <div className="mx-auto w-full max-w-6xl px-6 py-8">
                <header className="mb-6 flex items-center justify-between">
                    <div>
                        <h1 className="flex items-center gap-2 text-2xl font-semibold tracking-tight text-foreground">
                            <PenTool className="size-5 text-primary" /> Dashboard Studio
                        </h1>
                        <p className="mt-1 text-sm text-muted-foreground">Build dashboards and publish them to organizations.</p>
                    </div>
                </header>

                <div className="mb-6 flex gap-2">
                    <input value={name} onChange={(e) => setName(e.target.value)} placeholder="New dashboard name"
                        className="flex-1 rounded-md border border-border bg-background px-3 py-2 text-sm" />
                    <button
                        className="inline-flex items-center gap-1.5 rounded-md bg-primary px-4 py-2 text-sm font-medium text-primary-foreground hover:opacity-90 disabled:opacity-50"
                        disabled={!name.trim()}
                        onClick={() => router.post('/admin/studio', { name }, { onSuccess: () => setName('') })}>
                        <Plus className="size-4" /> Create
                    </button>
                </div>

                <div className="overflow-hidden rounded-xl border border-border">
                    <table className="w-full text-sm">
                        <thead className="bg-muted/50 text-left text-xs text-muted-foreground">
                            <tr>
                                <th className="px-4 py-2.5 font-medium">Name</th>
                                <th className="px-4 py-2.5 font-medium">Status</th>
                                <th className="px-4 py-2.5 font-medium">Assigned orgs</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody className="divide-y divide-border">
                            {dashboards.map((d) => (
                                <tr key={d.id} className="hover:bg-muted/30">
                                    <td className="px-4 py-3 font-medium text-foreground">{d.name}</td>
                                    <td className="px-4 py-3">
                                        <span className={`rounded-full px-2 py-0.5 text-[11px] font-medium ${STATUS[d.status]}`}>{d.status}</span>
                                    </td>
                                    <td className="px-4 py-3 text-muted-foreground">{d.assignments}</td>
                                    <td className="px-4 py-3 text-right">
                                        <Link href={`/admin/studio/${d.id}`} className="font-medium text-primary hover:underline">Edit</Link>
                                    </td>
                                </tr>
                            ))}
                            {dashboards.length === 0 && (
                                <tr><td colSpan={4} className="px-4 py-8 text-center text-muted-foreground">No dashboards yet. Create one above.</td></tr>
                            )}
                        </tbody>
                    </table>
                </div>
            </div>
        </>
    );
}
