import { Head, Link, router } from '@inertiajs/react';
import { Plus } from 'lucide-react';

import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';
import AdminLayout from '@/layouts/admin-layout';
import { type Category, type PaginatedData } from '@/types';

export default function AdminCategoriesIndex({ categories }: { categories: PaginatedData<Category> }) {
    const destroy = (id: number) => {
        if (confirm('Delete this category?')) {
            router.delete(route('admin.categories.destroy', id));
        }
    };

    return (
        <AdminLayout title="Categories">
            <Head title="Categories" />

            <div className="mb-4 flex justify-end">
                <Link href={route('admin.categories.create')}>
                    <Button className="bg-indigo-600 hover:bg-indigo-700"><Plus size={16} /> Add Category</Button>
                </Link>
            </div>

            <Card>
                <CardContent className="p-0">
                    <table className="w-full text-sm">
                        <thead className="border-b bg-slate-50 text-xs uppercase text-slate-500">
                            <tr>
                                <th className="px-4 py-3 text-left">Name</th>
                                <th className="px-4 py-3 text-left">Parent</th>
                                <th className="px-4 py-3 text-center">Order</th>
                                <th className="px-4 py-3 text-center">Status</th>
                                <th className="px-4 py-3" />
                            </tr>
                        </thead>
                        <tbody className="divide-y">
                            {categories.data.map((cat) => (
                                <tr key={cat.id} className={`hover:bg-slate-50 ${cat.deleted_at ? 'opacity-50' : ''}`}>
                                    <td className="px-4 py-3 font-medium">{cat.name}</td>
                                    <td className="px-4 py-3 text-slate-500">{cat.parent?.name ?? '—'}</td>
                                    <td className="px-4 py-3 text-center text-slate-500">{cat.sort_order}</td>
                                    <td className="px-4 py-3 text-center">
                                        <span className={`rounded-full px-2 py-0.5 text-xs font-medium ${cat.status ? 'bg-green-100 text-green-700' : 'bg-slate-100 text-slate-500'}`}>
                                            {cat.status ? 'Active' : 'Inactive'}
                                        </span>
                                    </td>
                                    <td className="px-4 py-3">
                                        {!cat.deleted_at && (
                                            <div className="flex gap-2">
                                                <Link href={route('admin.categories.edit', cat.id)} className="text-xs text-indigo-600 hover:underline">Edit</Link>
                                                <button onClick={() => destroy(cat.id)} className="text-xs text-red-500 hover:underline">Delete</button>
                                            </div>
                                        )}
                                    </td>
                                </tr>
                            ))}
                            {categories.data.length === 0 && (
                                <tr><td colSpan={5} className="px-4 py-10 text-center text-slate-400">No categories yet</td></tr>
                            )}
                        </tbody>
                    </table>
                </CardContent>
            </Card>
        </AdminLayout>
    );
}
