import { Head, Link } from '@inertiajs/react';
import { AlertTriangle, Eye } from 'lucide-react';
import Heading from '@/components/heading';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableHeader,
    TableRow,
} from '@/components/ui/table';
import { dashboard } from '@/routes';
import { index as stockAlertsIndex } from '@/routes/admin/stock-alerts';
import { show as warehouseShow } from '@/routes/admin/warehouses';

type StockAlert = {
    id: number;
    quantity: number;
    min_stock: number;
    product: { id: number; code: string; name: string };
    warehouse: { id: number; code: string; name: string };
};

export default function StockAlertsIndex({
    alerts,
}: {
    alerts: StockAlert[];
}) {
    return (
        <>
            <Head title="Alertes de stock" />

            <div className="flex h-full flex-1 flex-col gap-6 p-4 md:p-6">
                <Heading
                    title="Alertes de stock"
                    description="Produits dont la quantité disponible a atteint ou passé le seuil minimum configuré."
                />

                {alerts.length === 0 ? (
                    <div className="flex flex-col items-center justify-center gap-2 rounded-xl border border-dashed py-16 text-center">
                        <AlertTriangle className="size-8 text-muted-foreground" />
                        <p className="text-muted-foreground text-sm">
                            Aucune alerte pour le moment. Tous les stocks sont
                            au-dessus de leur seuil minimum.
                        </p>
                    </div>
                ) : (
                    <div className="overflow-hidden rounded-xl border">
                        <Table>
                            <TableHeader className="bg-muted/40">
                                <TableRow>
                                    <TableHead>Produit</TableHead>
                                    <TableHead>Entrepôt</TableHead>
                                    <TableHead className="text-right">
                                        Quantité
                                    </TableHead>
                                    <TableHead className="text-right">
                                        Seuil mini
                                    </TableHead>
                                    <TableHead className="text-right">
                                        Actions
                                    </TableHead>
                                </TableRow>
                            </TableHeader>
                            <TableBody>
                                {alerts.map((alert) => (
                                    <TableRow key={alert.id}>
                                        <TableCell>
                                            <div className="flex flex-col gap-0.5">
                                                <span className="font-medium">
                                                    {alert.product.name}
                                                </span>
                                                <span className="text-muted-foreground font-mono text-xs">
                                                    {alert.product.code}
                                                </span>
                                            </div>
                                        </TableCell>
                                        <TableCell>
                                            <div className="flex flex-col gap-0.5">
                                                <span className="text-sm font-medium">
                                                    {alert.warehouse.name}
                                                </span>
                                                <span className="text-muted-foreground font-mono text-xs">
                                                    {alert.warehouse.code}
                                                </span>
                                            </div>
                                        </TableCell>
                                        <TableCell className="text-right">
                                            <Badge
                                                variant={
                                                    alert.quantity === 0
                                                        ? 'destructive'
                                                        : 'secondary'
                                                }
                                                className="tabular-nums"
                                            >
                                                {alert.quantity}
                                            </Badge>
                                        </TableCell>
                                        <TableCell className="text-right tabular-nums text-muted-foreground">
                                            {alert.min_stock}
                                        </TableCell>
                                        <TableCell className="text-right">
                                            <Button
                                                variant="outline"
                                                size="icon"
                                                asChild
                                            >
                                                <Link
                                                    href={warehouseShow.url(
                                                        alert.warehouse.id,
                                                    )}
                                                    aria-label={`Voir ${alert.warehouse.name}`}
                                                >
                                                    <Eye />
                                                </Link>
                                            </Button>
                                        </TableCell>
                                    </TableRow>
                                ))}
                            </TableBody>
                        </Table>
                    </div>
                )}
            </div>
        </>
    );
}

StockAlertsIndex.layout = {
    breadcrumbs: [
        { title: 'Tableau de bord', href: dashboard() },
        { title: 'Alertes de stock', href: stockAlertsIndex.url() },
    ],
};
