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 DriveTrainRow = {
    id: number;
    name: string;
    slug: string;
    is_active: boolean;
    is_system: boolean;
    sort_order: number;
    variants_count: number;
    updated_at: string | null;
};

type DriveTrainIndexProps = {
    driveTrains: PaginatedData<DriveTrainRow>;
    filters: {
        search: string;
    };
};

export default function DriveTrainIndex({
    driveTrains,
    filters,
}: DriveTrainIndexProps) {
    const [search, setSearch] = useState(filters.search || '');

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

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

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

        router.delete(`/admin/drive-trains/${id}`);
    };

    return (
        <AdminLayout>
            <Head title="Drive Trains" />

            <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]">
                            Drive Trains
                        </h1>
                        <p className="mt-1 text-sm text-slate-600 dark:text-[#c7bde8]">
                            Manage standardized drive train layouts for vehicle
                            variant.
                        </p>
                    </div>
                    <Link
                        href="/admin/drive-trains/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 Drive Train
                    </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 or 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-drive-trains"
                    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]">
                                    Name
                                </th>
                                <th className="px-4 py-3 text-left font-medium text-slate-700 dark:text-[#c7bde8]">
                                    Vehicles
                                </th>
                                <th className="px-4 py-3 text-left font-medium text-slate-700 dark:text-[#c7bde8]">
                                    Active
                                </th>
                                <th className="px-4 py-3 text-left font-medium text-slate-700 dark:text-[#c7bde8]">
                                    Sort
                                </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]">
                            {driveTrains.data.map((driveTrain) => (
                                <tr key={driveTrain.id}>
                                    <td className="px-4 py-3">
                                        <p className="font-medium text-[#0a1530] dark:text-[#efe8ff]">
                                            {driveTrain.name}
                                        </p>
                                        <p className="text-xs text-slate-500 dark:text-[#bcaee6]">
                                            {driveTrain.slug}
                                        </p>
                                    </td>
                                    <td className="px-4 py-3 text-slate-700 dark:text-[#c7bde8]">
                                        {driveTrain.variants_count}
                                    </td>
                                    <td className="px-4 py-3 text-slate-700 dark:text-[#c7bde8]">
                                        {driveTrain.is_active ? 'Yes' : 'No'}
                                    </td>
                                    <td className="px-4 py-3 text-slate-700 dark:text-[#c7bde8]">
                                        {driveTrain.sort_order}
                                    </td>
                                    <td className="px-4 py-3">
                                        <div className="flex gap-2">
                                            <SystemRecordActions
                                                activeUrl={`/admin/drive-trains/${driveTrain.id}/active`}
                                                isActive={driveTrain.is_active}
                                                isSystem={driveTrain.is_system}
                                            >
                                                <Link
                                                    href={`/admin/drive-trains/${driveTrain.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={() =>
                                                        deleteDriveTrain(
                                                            driveTrain.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">
                    {driveTrains.data.map((driveTrain) => (
                        <article
                            key={driveTrain.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]">
                                {driveTrain.name}
                            </p>
                            <p className="text-xs text-slate-500 dark:text-[#bcaee6]">
                                {driveTrain.slug}
                            </p>
                            <p className="mt-2 text-xs text-slate-700 dark:text-[#c7bde8]">
                                Vehicles: {driveTrain.variants_count}
                            </p>
                            <p className="mt-1 text-xs text-slate-700 dark:text-[#c7bde8]">
                                Active: {driveTrain.is_active ? 'Yes' : 'No'}
                            </p>
                            <div className="mt-3 flex gap-2">
                                <SystemRecordActions
                                    activeUrl={`/admin/drive-trains/${driveTrain.id}/active`}
                                    isActive={driveTrain.is_active}
                                    isSystem={driveTrain.is_system}
                                >
                                    <Link
                                        href={`/admin/drive-trains/${driveTrain.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={() =>
                                            deleteDriveTrain(driveTrain.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 {driveTrains.from || 0} to {driveTrains.to || 0}{' '}
                        of {driveTrains.total} drive trains
                    </p>
                    <PaginationLinks links={driveTrains.links} />
                </div>
            </div>
        </AdminLayout>
    );
}
