import { Head, Link, router } from '@inertiajs/react';
import type { FormEvent } from 'react';
import { useState } from 'react';

type ProductRow = {
    id: number;
    name: string;
    slug: string;
    sku: string;
    condition: string;
    status: string;
    price: string;
    discount_price: string | null;
    stock_quantity: number;
    reserved_quantity: number;
    brand: { name: string } | null;
    category: { name: string } | null;
    part_numbers: { type: string; number: string }[];
};

type Pagination<T> = {
    data: T[];
    links: { url: string | null; label: string; active: boolean }[];
};

export default function AdminProductsIndex({
    products,
    filters,
}: {
    products: Pagination<ProductRow>;
    filters: { search: string };
}) {
    const [search, setSearch] = useState(filters.search ?? '');

    function submit(event: FormEvent) {
        event.preventDefault();
        router.get('/admin/products', { search }, { preserveState: true });
    }

    return (
        <>
            <Head title="Admin Products" />
            <main className="min-h-screen bg-slate-50 px-4 py-6 text-slate-900 sm:px-6 lg:px-8">
                <div className="mx-auto max-w-7xl">
                    <div className="flex flex-col gap-4 border-b border-slate-200 pb-5 md:flex-row md:items-end md:justify-between">
                        <div>
                            <p className="text-sm font-medium text-cyan-700">
                                Catalog admin
                            </p>
                            <h1 className="text-2xl font-semibold">
                                Products
                            </h1>
                        </div>
                        <Link
                            href="/admin/products/create"
                            className="inline-flex items-center justify-center rounded-md bg-cyan-700 px-4 py-2 text-sm font-semibold text-white hover:bg-cyan-800"
                        >
                            Add product
                        </Link>
                    </div>

                    <form
                        onSubmit={submit}
                        className="mt-6 flex flex-col gap-3 sm:flex-row"
                    >
                        <input
                            value={search}
                            onChange={(event) => setSearch(event.target.value)}
                            placeholder="Search name, SKU, OEM or alternative number"
                            className="w-full rounded-md border border-slate-300 bg-white px-3 py-2 text-sm focus:border-cyan-600 focus:ring-cyan-600"
                        />
                        <button className="rounded-md border border-slate-300 bg-white px-4 py-2 text-sm font-semibold text-slate-800 hover:bg-slate-100">
                            Search
                        </button>
                    </form>

                    <div className="mt-6 overflow-hidden rounded-lg border border-slate-200 bg-white">
                        <div className="hidden overflow-x-auto lg:block">
                            <table className="min-w-full divide-y divide-slate-200 text-sm">
                                <thead className="bg-slate-100 text-left text-xs uppercase tracking-wide text-slate-600">
                                    <tr>
                                        <th className="px-4 py-3">Product</th>
                                        <th className="px-4 py-3">Brand</th>
                                        <th className="px-4 py-3">Category</th>
                                        <th className="px-4 py-3">Price</th>
                                        <th className="px-4 py-3">Stock</th>
                                        <th className="px-4 py-3">Status</th>
                                        <th className="px-4 py-3" />
                                    </tr>
                                </thead>
                                <tbody className="divide-y divide-slate-200">
                                    {products.data.map((product) => (
                                        <tr key={product.id}>
                                            <td className="px-4 py-3">
                                                <div className="font-medium">
                                                    {product.name}
                                                </div>
                                                <div className="text-xs text-slate-500">
                                                    {product.sku}
                                                </div>
                                            </td>
                                            <td className="px-4 py-3">
                                                {product.brand?.name ?? '-'}
                                            </td>
                                            <td className="px-4 py-3">
                                                {product.category?.name ?? '-'}
                                            </td>
                                            <td className="px-4 py-3">
                                                {product.discount_price ??
                                                    product.price}
                                            </td>
                                            <td className="px-4 py-3">
                                                {product.stock_quantity -
                                                    product.reserved_quantity}
                                            </td>
                                            <td className="px-4 py-3 capitalize">
                                                {product.status}
                                            </td>
                                            <td className="px-4 py-3 text-right">
                                                <Link
                                                    href={`/admin/products/${product.id}/edit`}
                                                    className="text-sm font-semibold text-cyan-700 hover:text-cyan-900"
                                                >
                                                    Edit
                                                </Link>
                                            </td>
                                        </tr>
                                    ))}
                                </tbody>
                            </table>
                        </div>

                        <div className="divide-y divide-slate-200 lg:hidden">
                            {products.data.map((product) => (
                                <article key={product.id} className="p-4">
                                    <div className="flex items-start justify-between gap-3">
                                        <div>
                                            <h2 className="font-semibold">
                                                {product.name}
                                            </h2>
                                            <p className="text-xs text-slate-500">
                                                {product.sku}
                                            </p>
                                        </div>
                                        <Link
                                            href={`/admin/products/${product.id}/edit`}
                                            className="text-sm font-semibold text-cyan-700"
                                        >
                                            Edit
                                        </Link>
                                    </div>
                                    <dl className="mt-3 grid grid-cols-2 gap-2 text-sm">
                                        <div>
                                            <dt className="text-xs text-slate-500">
                                                Brand
                                            </dt>
                                            <dd>{product.brand?.name ?? '-'}</dd>
                                        </div>
                                        <div>
                                            <dt className="text-xs text-slate-500">
                                                Available
                                            </dt>
                                            <dd>
                                                {product.stock_quantity -
                                                    product.reserved_quantity}
                                            </dd>
                                        </div>
                                        <div>
                                            <dt className="text-xs text-slate-500">
                                                Price
                                            </dt>
                                            <dd>
                                                {product.discount_price ??
                                                    product.price}
                                            </dd>
                                        </div>
                                        <div>
                                            <dt className="text-xs text-slate-500">
                                                Status
                                            </dt>
                                            <dd className="capitalize">
                                                {product.status}
                                            </dd>
                                        </div>
                                    </dl>
                                </article>
                            ))}
                        </div>
                    </div>

                    <nav className="mt-5 flex flex-wrap gap-2">
                        {products.links.map((link) => (
                            <Link
                                key={link.label}
                                href={link.url ?? '#'}
                                className={`rounded-md border px-3 py-2 text-sm ${
                                    link.active
                                        ? 'border-cyan-700 bg-cyan-700 text-white'
                                        : 'border-slate-300 bg-white text-slate-700'
                                } ${link.url ? '' : 'pointer-events-none opacity-50'}`}
                                dangerouslySetInnerHTML={{ __html: link.label }}
                            />
                        ))}
                    </nav>
                </div>
            </main>
        </>
    );
}
