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 Brand, type PaginatedData } from '@/types';

export default function AdminBrandsIndex({ brands }: { brands: PaginatedData<Brand> }) {
    const destroy = (id: number) => {
        if (confirm('Delete this brand?')) router.delete(route('admin.brands.destroy', id));
    };

    return (
        <AdminLayout title="Brands">
            <Head title="Brands" />
            <div className="mb-4 flex justify-end">
                <Link href={route('admin.brands.create')}>
                    <Button className="bg-indigo-600 hover:bg-indigo-700"><Plus size={16} /> Add Brand</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 w-16">Logo</th>
                                <th className="px-4 py-3 text-left">Name</th>
                                <th className="px-4 py-3 text-left">Slug</th>
                                <th className="px-4 py-3 text-center">Status</th>
                                <th className="px-4 py-3" />
                            </tr>
                        </thead>
                        <tbody className="divide-y">
                            {brands.data.map((brand) => (
                                <tr key={brand.id} className="hover:bg-slate-50">
                                    <td className="px-4 py-3">
                                        {brand.logo ? <img src={`/storage/${brand.logo}`} className="h-10 w-10 rounded object-contain" alt="" /> : <div className="h-10 w-10 rounded bg-slate-100" />}
                                    </td>
                                    <td className="px-4 py-3 font-medium">{brand.name}</td>
                                    <td className="px-4 py-3 font-mono text-xs text-slate-500">{brand.slug}</td>
                                    <td className="px-4 py-3 text-center">
                                        <span className={`rounded-full px-2 py-0.5 text-xs font-medium ${brand.status ? 'bg-green-100 text-green-700' : 'bg-slate-100 text-slate-500'}`}>
                                            {brand.status ? 'Active' : 'Inactive'}
                                        </span>
                                    </td>
                                    <td className="px-4 py-3">
                                        <div className="flex gap-2">
                                            <Link href={route('admin.brands.edit', brand.id)} className="text-xs text-indigo-600 hover:underline">Edit</Link>
                                            <button onClick={() => destroy(brand.id)} className="text-xs text-red-500 hover:underline">Delete</button>
                                        </div>
                                    </td>
                                </tr>
                            ))}
                            {brands.data.length === 0 && <tr><td colSpan={5} className="px-4 py-10 text-center text-slate-400">No brands yet</td></tr>}
                        </tbody>
                    </table>
                </CardContent>
            </Card>
        </AdminLayout>
    );
}
