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

import { SystemRecordActions } from '@/components/admin/system-record-actions';
import { ConfigurableTable } from '@/components/ui/configurable-table';
import { PaginationLinks } from '@/components/ui/pagination-links';
import { AdminLayout } from '@/layouts/admin-layout';
import type { PaginatedData } from '@/types';

type VehicleModelRow = {
    id: number;
    name: string;
    slug: string;
    make: string | null;
    generation: string | null;
    is_active: boolean;
    is_system: boolean;
    sort_order: number;
    variants_count: number;
    updated_at: string | null;
};

type VehicleModelIndexProps = {
    models: PaginatedData<VehicleModelRow>;
    filters: {
        search: string;
    };
};

export default function VehicleModelIndex({
    models,
    filters,
}: VehicleModelIndexProps) {
    const [search, setSearch] = useState(filters.search || '');

    const submitSearch = (event: React.FormEvent<HTMLFormElement>) => {
        event.preventDefault();

        router.get('/admin/vehicle-models', search ? { search } : {}, {
            preserveState: true,
            replace: true,
            preserveScroll: true,
        });
    };

    const deleteModel = (id: number) => {
        if (!window.confirm('Delete this model?')) {
            return;
        }

        router.delete(`/admin/vehicle-models/${id}`);
    };

    return (
        <AdminLayout>
            <Head title="Vehicle Models" />

            <div className="space-y-5">
                <div className="flex flex-wrap items-center justify-between gap-3">
                    <div>
                        <h1 className="text-2xl font-semibold text-[#0a1530] dark:text-[#efe8ff]">
                            Vehicle Models
                        </h1>
                        <p className="mt-1 text-sm text-slate-600 dark:text-[#c7bde8]">
                            Manage models inside vehicle generations.
                        </p>
                    </div>
                    <Link
                        href="/admin/vehicle-models/create"
                        className="rounded-lg bg-[#0b7285] px-4 py-2 text-sm font-semibold text-white hover:bg-[#095f6f] dark:bg-[#7c3aed] dark:hover:bg-[#8b5cf6]"
                    >
                        Add Model
                    </Link>
                </div>

                <form
                    onSubmit={submitSearch}
                    className="flex max-w-md flex-col gap-2 sm:flex-row"
                >
                    <input
                        value={search}
                        onChange={(event) => setSearch(event.target.value)}
                        placeholder="Search by name, slug"
                        className="w-full rounded-lg border border-slate-300 bg-white px-3 py-2 text-sm text-[#0a1530] outline-none focus:border-[#0b7285] dark:border-[#4a3a78] dark:bg-[#160f2d] dark:text-[#efe8ff] dark:focus:border-[#a78bfa]"
                    />
                    <button
                        type="submit"
                        className="rounded-lg border border-slate-300 px-3 py-2 text-sm font-medium text-slate-700 hover:bg-slate-100 dark:border-[#4a3a78] dark:text-[#e5dcff] dark:hover:bg-[#2a1d50]"
                    >
                        Search
                    </button>
                </form>

                <ConfigurableTable
                    tableId="admin-vehicle-models"
                    className="hidden overflow-x-auto 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]">
                                    Model
                                </th>
                                <th className="px-4 py-3 text-left font-medium text-slate-700 dark:text-[#c7bde8]">
                                    Generation
                                </th>
                                <th className="px-4 py-3 text-left font-medium text-slate-700 dark:text-[#c7bde8]">
                                    Stats
                                </th>
                                <th className="px-4 py-3 text-left font-medium text-slate-700 dark:text-[#c7bde8]">
                                    Actions
                                </th>
                            </tr>
                        </thead>
                        <tbody className="divide-y divide-slate-100 bg-white dark:divide-[#3c2b68] dark:bg-[#211740]">
                            {models.data.map((model) => (
                                <tr key={model.id}>
                                    <td className="px-4 py-3">
                                        <p className="font-medium text-[#0a1530] dark:text-[#efe8ff]">
                                            {model.name}
                                        </p>
                                        <p className="text-xs text-slate-500 dark:text-[#bcaee6]">
                                            {model.slug}
                                        </p>
                                    </td>
                                    <td className="px-4 py-3 text-slate-700 dark:text-[#c7bde8]">
                                        {model.make || '-'} /{' '}
                                        {model.generation || '-'}
                                    </td>
                                    <td className="px-4 py-3 text-slate-700 dark:text-[#c7bde8]">
                                        {model.variants_count} vehicles
                                    </td>
                                    <td className="px-4 py-3">
                                        <div className="flex gap-2">
                                            <SystemRecordActions
                                                activeUrl={`/admin/vehicle-models/${model.id}/active`}
                                                isActive={model.is_active}
                                                isSystem={model.is_system}
                                            >
                                                <Link
                                                    href={`/admin/vehicle-models/${model.id}/edit`}
                                                    className="rounded-md border border-slate-300 px-2.5 py-1 text-xs font-medium text-slate-700 hover:bg-slate-100 dark:border-[#4a3a78] dark:text-[#e5dcff] dark:hover:bg-[#2a1d50]"
                                                >
                                                    Edit
                                                </Link>
                                                <button
                                                    type="button"
                                                    onClick={() =>
                                                        deleteModel(model.id)
                                                    }
                                                    className="rounded-md border border-rose-200 px-2.5 py-1 text-xs font-medium text-rose-700 hover:bg-rose-50"
                                                >
                                                    Delete
                                                </button>
                                            </SystemRecordActions>
                                        </div>
                                    </td>
                                </tr>
                            ))}
                        </tbody>
                    </table>
                </ConfigurableTable>

                <div className="space-y-3 md:hidden">
                    {models.data.map((model) => (
                        <article
                            key={model.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]">
                                {model.name}
                            </p>
                            <p className="text-xs text-slate-500 dark:text-[#bcaee6]">
                                {model.make || '-'} / {model.generation || '-'}{' '}
                                • {model.slug}
                            </p>
                            <p className="mt-2 text-xs text-slate-700 dark:text-[#c7bde8]">
                                {model.variants_count} vehicles
                            </p>
                            <div className="mt-3 flex gap-2">
                                <SystemRecordActions
                                    activeUrl={`/admin/vehicle-models/${model.id}/active`}
                                    isActive={model.is_active}
                                    isSystem={model.is_system}
                                >
                                    <Link
                                        href={`/admin/vehicle-models/${model.id}/edit`}
                                        className="rounded-md border border-slate-300 px-2.5 py-1 text-xs font-medium text-slate-700 hover:bg-slate-100 dark:border-[#4a3a78] dark:text-[#e5dcff] dark:hover:bg-[#2a1d50]"
                                    >
                                        Edit
                                    </Link>
                                    <button
                                        type="button"
                                        onClick={() => deleteModel(model.id)}
                                        className="rounded-md border border-rose-200 px-2.5 py-1 text-xs font-medium text-rose-700 hover:bg-rose-50"
                                    >
                                        Delete
                                    </button>
                                </SystemRecordActions>
                            </div>
                        </article>
                    ))}
                </div>

                <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
                    <p className="text-sm text-slate-600 dark:text-[#c7bde8]">
                        Showing {models.from || 0} to {models.to || 0} of{' '}
                        {models.total} models
                    </p>
                    <PaginationLinks links={models.links} />
                </div>
            </div>
        </AdminLayout>
    );
}
