import { Head } from '@inertiajs/react';

import { ConfigurableTable } from '@/components/ui/configurable-table';
import { AdminLayout } from '@/layouts/admin-layout';

type DashboardProps = {
    stats: {
        categories: number;
        products: number;
        published_products: number;
        media_items: number;
    };
    latestProducts: Array<{
        id: number;
        name: string;
        status: string;
        category: string | null;
        created_at: string | null;
    }>;
};

export default function Dashboard({ stats, latestProducts }: DashboardProps) {
    return (
        <AdminLayout>
            <Head title="Dashboard" />

            <div className="space-y-6">
                <div>
                    <h1 className="text-2xl font-semibold text-[#0a1530] dark:text-[#efe8ff]">
                        Catalog Dashboard
                    </h1>
                    <p className="mt-1 text-sm text-slate-600 dark:text-[#c7bde8]">
                        Phase 1 administration foundation for categories,
                        products, and media.
                    </p>
                </div>

                <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
                    <StatCard label="Categories" value={stats.categories} />
                    <StatCard label="Products" value={stats.products} />
                    <StatCard
                        label="Published Products"
                        value={stats.published_products}
                    />
                    <StatCard label="Media Items" value={stats.media_items} />
                </div>

                <div className="space-y-3 md:hidden">
                    {latestProducts.map((product) => (
                        <article
                            key={product.id}
                            className="rounded-xl border border-slate-200 bg-white p-4 dark:border-[#3c2b68] dark:bg-[#211740]"
                        >
                            <p className="font-semibold text-[#0a1530] dark:text-[#efe8ff]">
                                {product.name}
                            </p>
                            <div className="mt-2 grid grid-cols-2 gap-2 text-xs text-slate-700 dark:text-[#c7bde8]">
                                <p>
                                    Category:{' '}
                                    {product.category || 'Uncategorized'}
                                </p>
                                <p>Status: {product.status}</p>
                                <p>Created: {product.created_at || '-'}</p>
                            </div>
                        </article>
                    ))}
                </div>

                <ConfigurableTable
                    tableId="admin-dashboard"
                    className="hidden overflow-hidden rounded-xl border border-slate-200 md:block dark:border-[#3c2b68]"
                >
                    <table className="min-w-full divide-y divide-slate-200 text-sm dark:divide-[#3c2b68]">
                        <thead className="bg-slate-50 dark:bg-[#2a1d50]/70">
                            <tr>
                                <th className="px-4 py-3 text-left font-medium text-slate-700 dark:text-[#c7bde8]">
                                    Product
                                </th>
                                <th className="px-4 py-3 text-left font-medium text-slate-700 dark:text-[#c7bde8]">
                                    Category
                                </th>
                                <th className="px-4 py-3 text-left font-medium text-slate-700 dark:text-[#c7bde8]">
                                    Status
                                </th>
                                <th className="px-4 py-3 text-left font-medium text-slate-700 dark:text-[#c7bde8]">
                                    Created
                                </th>
                            </tr>
                        </thead>
                        <tbody className="divide-y divide-slate-100 bg-white dark:divide-[#3c2b68] dark:bg-[#211740]">
                            {latestProducts.map((product) => (
                                <tr key={product.id}>
                                    <td className="px-4 py-3 font-medium text-[#0a1530] dark:text-[#efe8ff]">
                                        {product.name}
                                    </td>
                                    <td className="px-4 py-3 text-slate-700 dark:text-[#c7bde8]">
                                        {product.category || 'Uncategorized'}
                                    </td>
                                    <td className="px-4 py-3 text-slate-700 dark:text-[#c7bde8]">
                                        {product.status}
                                    </td>
                                    <td className="px-4 py-3 text-slate-700 dark:text-[#c7bde8]">
                                        {product.created_at || '-'}
                                    </td>
                                </tr>
                            ))}
                        </tbody>
                    </table>
                </ConfigurableTable>
            </div>
        </AdminLayout>
    );
}

function StatCard({ label, value }: { label: string; value: number }) {
    return (
        <div className="rounded-xl border border-slate-200 bg-white p-4 dark:border-[#3c2b68] dark:bg-[#211740]">
            <p className="text-xs tracking-wide text-slate-500 uppercase dark:text-[#bcaee6]">
                {label}
            </p>
            <p className="mt-2 text-2xl font-semibold text-[#0a1530] dark:text-[#efe8ff]">
                {value}
            </p>
        </div>
    );
}
