import { Head, Link } from '@inertiajs/react';
import { Button } from '@/components/ui/button';
import type { Category, PaginatedData } from '@/types';

type Props = { categories: PaginatedData<Category> };

export default function CategoriesIndex({ categories }: Props) {
    return (
        <>
            <Head title="Categories" />
            <div className="space-y-4 p-4">
                <div className="flex items-center justify-between">
                    <h1 className="text-2xl font-bold">Categories</h1>
                    <Button asChild>
                        <Link href="/admin/categories/create">
                            Add Category
                        </Link>
                    </Button>
                </div>
                <div className="overflow-hidden rounded-lg border">
                    <table className="w-full text-sm">
                        <thead className="bg-muted/50">
                            <tr>
                                <th className="p-3 text-left font-medium">
                                    Name
                                </th>
                                <th className="p-3 text-left font-medium">
                                    System
                                </th>
                                <th className="p-3 text-left font-medium">
                                    Status
                                </th>
                                <th className="p-3 text-left font-medium">
                                    Actions
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            {categories.data.map((cat) => (
                                <tr key={cat.id} className="border-t">
                                    <td className="p-3">{cat.name}</td>
                                    <td className="p-3">{cat.system?.name}</td>
                                    <td className="p-3">
                                        <span
                                            className={`inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium ${cat.is_active ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'}`}
                                        >
                                            {cat.is_active
                                                ? 'Active'
                                                : 'Inactive'}
                                        </span>
                                    </td>
                                    <td className="p-3">
                                        <Button
                                            variant="outline"
                                            size="sm"
                                            asChild
                                        >
                                            <Link
                                                href={`/admin/categories/${cat.id}/edit`}
                                            >
                                                Edit
                                            </Link>
                                        </Button>
                                    </td>
                                </tr>
                            ))}
                        </tbody>
                    </table>
                </div>
            </div>
        </>
    );
}
