import { Head, Link } from '@inertiajs/react';
import { ArrowLeft, History, Package } from 'lucide-react';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import {
    Card,
    CardContent,
    CardDescription,
    CardHeader,
    CardTitle,
} from '@/components/ui/card';
import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableHeader,
    TableRow,
} from '@/components/ui/table';
import { formatDate } from '@/lib/utils';
import { dashboard } from '@/routes';
import { index as stockExitsIndex } from '@/routes/admin/stock-exits';
import { index as stockMovementsIndex } from '@/routes/admin/stock-movements';
import { show as invoiceShow } from '@/routes/admin/invoices';

type ExitLine = {
    id: number;
    product: { id: number; code: string; name: string } | null;
    quantity: number;
};

type StockExit = {
    id: number;
    code: string;
    type: string;
    type_label: string;
    exited_at: string | null;
    notes: string | null;
    created_at: string | null;
    warehouse: { id: number; code: string; name: string };
    destination_warehouse: { id: number; code: string; name: string } | null;
    creator: { id: number; name: string } | null;
    invoice: { id: number; code: string | null } | null;
    lines: ExitLine[];
    lines_count: number;
    lines_quantity: number;
};

export default function StockExitsShow({
    exit,
    canViewInvoice,
}: {
    exit: StockExit;
    canViewInvoice: boolean;
}) {
    return (
        <>
            <Head title={exit.code} />

            <div className="flex h-full flex-1 flex-col gap-6 p-4 md:p-6">
                <div className="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
                    <div className="space-y-2">
                        <div className="flex flex-wrap items-center gap-2">
                            <h1 className="font-mono text-xl font-semibold tracking-tight">
                                {exit.code}
                            </h1>
                            <Badge variant="secondary">{exit.type_label}</Badge>
                        </div>
                        <p className="text-muted-foreground text-sm">
                            {formatDate(exit.exited_at)}
                            <span className="mx-1.5">·</span>
                            {exit.warehouse.name}
                        </p>
                    </div>
                    <div className="flex flex-wrap gap-2">
                        <Button variant="outline" asChild>
                            <Link href={stockMovementsIndex.url()}>
                                <History />
                                Historique des mouvements
                            </Link>
                        </Button>
                        <Button variant="outline" asChild>
                            <Link href={stockExitsIndex.url()}>
                                <ArrowLeft />
                                Retour
                            </Link>
                        </Button>
                    </div>
                </div>

                <div className="grid gap-4 sm:grid-cols-3">
                    <Card>
                        <CardHeader className="pb-2">
                            <CardDescription>Produits</CardDescription>
                            <CardTitle className="text-2xl tabular-nums">
                                {exit.lines_count}
                            </CardTitle>
                        </CardHeader>
                    </Card>
                    <Card>
                        <CardHeader className="pb-2">
                            <CardDescription>Unités sorties</CardDescription>
                            <CardTitle className="flex items-center gap-2 text-2xl tabular-nums">
                                <Package className="size-5 text-muted-foreground" />
                                {exit.lines_quantity}
                            </CardTitle>
                        </CardHeader>
                    </Card>
                    <Card>
                        <CardHeader className="pb-2">
                            <CardDescription>Type</CardDescription>
                            <CardTitle className="text-lg">
                                {exit.type_label}
                            </CardTitle>
                        </CardHeader>
                    </Card>
                </div>

                <Card>
                    <CardHeader>
                        <CardTitle>Informations</CardTitle>
                    </CardHeader>
                    <CardContent className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
                        <div>
                            <p className="text-muted-foreground text-xs uppercase tracking-wide">
                                Entrepôt source
                            </p>
                            <p className="mt-1 text-sm font-medium">
                                {exit.warehouse.code} — {exit.warehouse.name}
                            </p>
                        </div>
                        <div>
                            <p className="text-muted-foreground text-xs uppercase tracking-wide">
                                Destination
                            </p>
                            <p className="mt-1 text-sm font-medium">
                                {exit.destination_warehouse
                                    ? `${exit.destination_warehouse.code} — ${exit.destination_warehouse.name}`
                                    : '—'}
                            </p>
                        </div>
                        <div>
                            <p className="text-muted-foreground text-xs uppercase tracking-wide">
                                Créé par
                            </p>
                            <p className="mt-1 text-sm font-medium">
                                {exit.creator?.name ?? '—'}
                            </p>
                        </div>
                        <div>
                            <p className="text-muted-foreground text-xs uppercase tracking-wide">
                                Créé le
                            </p>
                            <p className="mt-1 text-sm font-medium">
                                {exit.created_at
                                    ? formatDate(exit.created_at)
                                    : '—'}
                            </p>
                        </div>
                        {exit.invoice ? (
                            <div>
                                <p className="text-muted-foreground text-xs uppercase tracking-wide">
                                    Facture liée
                                </p>
                                {canViewInvoice ? (
                                    <Link
                                        href={invoiceShow(exit.invoice.id).url}
                                        className="mt-1 block text-sm font-medium text-primary hover:underline"
                                    >
                                        {exit.invoice.code ?? `#${exit.invoice.id}`}
                                    </Link>
                                ) : (
                                    <p className="mt-1 text-sm font-medium">
                                        {exit.invoice.code ?? `#${exit.invoice.id}`}
                                    </p>
                                )}
                            </div>
                        ) : null}
                        {exit.notes ? (
                            <div className="sm:col-span-2 lg:col-span-4">
                                <p className="text-muted-foreground text-xs uppercase tracking-wide">
                                    Notes
                                </p>
                                <p className="mt-1 text-sm">{exit.notes}</p>
                            </div>
                        ) : null}
                    </CardContent>
                </Card>

                <Card>
                    <CardHeader>
                        <CardTitle>Lignes</CardTitle>
                        <CardDescription>
                            Produits sortis de l&apos;entrepôt source.
                        </CardDescription>
                    </CardHeader>
                    <CardContent>
                        <Table>
                            <TableHeader>
                                <TableRow>
                                    <TableHead>Produit</TableHead>
                                    <TableHead className="text-right">
                                        Quantité
                                    </TableHead>
                                </TableRow>
                            </TableHeader>
                            <TableBody>
                                {exit.lines.map((line) => (
                                    <TableRow key={line.id}>
                                        <TableCell>
                                            <div className="flex flex-col gap-0.5">
                                                <span className="font-medium">
                                                    {line.product?.name ?? '—'}
                                                </span>
                                                <span className="text-muted-foreground font-mono text-xs">
                                                    {line.product?.code ?? '—'}
                                                </span>
                                            </div>
                                        </TableCell>
                                        <TableCell className="text-right tabular-nums font-medium">
                                            {line.quantity}
                                        </TableCell>
                                    </TableRow>
                                ))}
                            </TableBody>
                        </Table>
                    </CardContent>
                </Card>
            </div>
        </>
    );
}

StockExitsShow.layout = {
    breadcrumbs: [
        { title: 'Tableau de bord', href: dashboard() },
        { title: 'Sorties de stock', href: stockExitsIndex.url() },
        { title: 'Détail', href: '#' },
    ],
};
