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

type AttributeGroupIndexProps = {
    groups: PaginatedData<AttributeGroupRow>;
    filters: {
        search: string;
    };
};

export default function AttributeGroupIndex({
    groups,
    filters,
}: AttributeGroupIndexProps) {
    const [search, setSearch] = useState(filters.search || '');

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

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

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

        router.delete(`/admin/attribute-groups/${id}`);
    };

    return (
        <AdminLayout>
            <Head title="Attribute Groups" />

            <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]">
                            Attribute Groups
                        </h1>
                        <p className="mt-1 text-sm text-slate-600 dark:text-[#c7bde8]">
                            Organize technical attributes into reusable groups.
                        </p>
                    </div>
                    <Link
                        href="/admin/attribute-groups/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 Group
                    </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>

                <div className="space-y-3 md:hidden">
                    {groups.data.map((group) => (
                        <article
                            key={group.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]">
                                {group.name}
                            </p>
                            <p className="text-xs text-slate-500 dark:text-[#bcaee6]">
                                {group.slug}
                            </p>
                            <div className="mt-3 grid grid-cols-2 gap-2 text-xs text-slate-700 dark:text-[#c7bde8]">
                                <p>Active: {group.is_active ? 'Yes' : 'No'}</p>
                                <p>Sort: {group.sort_order}</p>
                                <p>Attributes: {group.attributes_count}</p>
                                <p>Updated: {group.updated_at || '-'}</p>
                            </div>
                            <div className="mt-4 flex gap-2">
                                <SystemRecordActions
                                    activeUrl={`/admin/attribute-groups/${group.id}/active`}
                                    isActive={group.is_active}
                                    isSystem={group.is_system}
                                >
                                    <Link
                                        href={`/admin/attribute-groups/${group.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={() => deleteGroup(group.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>

                <ConfigurableTable
                    tableId="admin-attribute-groups"
                    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]">
                                    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]">
                                    Attributes
                                </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]">
                            {groups.data.map((group) => (
                                <tr key={group.id}>
                                    <td className="px-4 py-3">
                                        <p className="font-medium text-[#0a1530] dark:text-[#efe8ff]">
                                            {group.name}
                                        </p>
                                        <p className="text-xs text-slate-500 dark:text-[#bcaee6]">
                                            {group.slug}
                                        </p>
                                    </td>
                                    <td className="px-4 py-3 text-slate-700 dark:text-[#c7bde8]">
                                        {group.is_active ? 'Yes' : 'No'}
                                    </td>
                                    <td className="px-4 py-3 text-slate-700 dark:text-[#c7bde8]">
                                        {group.sort_order}
                                    </td>
                                    <td className="px-4 py-3 text-slate-700 dark:text-[#c7bde8]">
                                        {group.attributes_count}
                                    </td>
                                    <td className="px-4 py-3">
                                        <div className="flex flex-wrap gap-2">
                                            <SystemRecordActions
                                                activeUrl={`/admin/attribute-groups/${group.id}/active`}
                                                isActive={group.is_active}
                                                isSystem={group.is_system}
                                            >
                                                <Link
                                                    href={`/admin/attribute-groups/${group.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={() =>
                                                        deleteGroup(group.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="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 {groups.from || 0} to {groups.to || 0} of{' '}
                        {groups.total} groups
                    </p>
                    <PaginationLinks links={groups.links} />
                </div>
            </div>
        </AdminLayout>
    );
}
