import { Head, Link, router } from '@inertiajs/react';
import { Pencil, Plus, Trash2 } from 'lucide-react';
import { useState } from 'react';
import Heading from '@/components/heading';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';
import {
    Dialog,
    DialogClose,
    DialogContent,
    DialogDescription,
    DialogFooter,
    DialogTitle,
} from '@/components/ui/dialog';
import { dashboard } from '@/routes/admin';
import { create, destroy, edit, index } from '@/routes/admin/routes';

type AdminRoute = {
    id: number;
    origin: string;
    destination: string;
    duration_minutes: number;
    distance_km: string;
    price: number;
    image: string | null;
    is_popular: boolean;
    is_high_demand: boolean;
    is_active: boolean;
    sort_order: number;
};

export default function RoutesIndex({ routes }: { routes: AdminRoute[] }) {
    const [deleting, setDeleting] = useState<AdminRoute | null>(null);

    return (
        <>
            <Head title="Routes" />
            <div className="space-y-6 p-4 sm:p-6">
                <div className="flex flex-wrap items-start justify-between gap-4">
                    <Heading
                        title="Home page routes"
                        description="Routes shown in “Most popular” and “High Demand Connections” on the home page"
                    />
                    <Button asChild>
                        <Link href={create()}>
                            <Plus />
                            Add route
                        </Link>
                    </Button>
                </div>

                <Card>
                    <CardContent className="overflow-x-auto p-0">
                        <table className="w-full min-w-[720px] text-sm">
                            <thead className="border-b text-left text-muted-foreground">
                                <tr>
                                    <th className="p-4 font-medium">Route</th>
                                    <th className="p-4 font-medium">Trip</th>
                                    <th className="p-4 font-medium">Price</th>
                                    <th className="p-4 font-medium">
                                        Shown in
                                    </th>
                                    <th className="p-4 font-medium">Order</th>
                                    <th className="p-4" />
                                </tr>
                            </thead>
                            <tbody>
                                {routes.map((route) => (
                                    <tr
                                        key={route.id}
                                        className="border-b last:border-0"
                                    >
                                        <td className="p-4">
                                            <div className="flex items-center gap-3">
                                                <div className="size-12 shrink-0 overflow-hidden rounded-md bg-muted">
                                                    {route.image && (
                                                        <img
                                                            src={route.image}
                                                            alt=""
                                                            className="size-full object-cover"
                                                        />
                                                    )}
                                                </div>
                                                <div>
                                                    <p className="font-medium">
                                                        {route.origin} →{' '}
                                                        {route.destination}
                                                    </p>
                                                    {!route.is_active && (
                                                        <Badge variant="outline">
                                                            Hidden
                                                        </Badge>
                                                    )}
                                                </div>
                                            </div>
                                        </td>
                                        <td className="p-4 whitespace-nowrap text-muted-foreground">
                                            {route.duration_minutes} min ·{' '}
                                            {route.distance_km} km
                                        </td>
                                        <td className="p-4 whitespace-nowrap">
                                            ${route.price.toFixed(2)}
                                        </td>
                                        <td className="p-4">
                                            <div className="flex flex-wrap gap-1">
                                                {route.is_popular && (
                                                    <Badge variant="secondary">
                                                        Popular
                                                    </Badge>
                                                )}
                                                {route.is_high_demand && (
                                                    <Badge variant="secondary">
                                                        High demand
                                                    </Badge>
                                                )}
                                            </div>
                                        </td>
                                        <td className="p-4">
                                            {route.sort_order}
                                        </td>
                                        <td className="p-4">
                                            <div className="flex justify-end gap-1">
                                                <Button
                                                    variant="ghost"
                                                    size="icon"
                                                    asChild
                                                >
                                                    <Link
                                                        href={edit(route.id)}
                                                        aria-label="Edit route"
                                                    >
                                                        <Pencil />
                                                    </Link>
                                                </Button>
                                                <Button
                                                    variant="ghost"
                                                    size="icon"
                                                    aria-label="Delete route"
                                                    onClick={() =>
                                                        setDeleting(route)
                                                    }
                                                >
                                                    <Trash2 />
                                                </Button>
                                            </div>
                                        </td>
                                    </tr>
                                ))}
                                {routes.length === 0 && (
                                    <tr>
                                        <td
                                            colSpan={6}
                                            className="p-8 text-center text-muted-foreground"
                                        >
                                            No routes yet. Add your first one.
                                        </td>
                                    </tr>
                                )}
                            </tbody>
                        </table>
                    </CardContent>
                </Card>
            </div>

            <Dialog
                open={deleting !== null}
                onOpenChange={(open) => !open && setDeleting(null)}
            >
                <DialogContent>
                    <DialogTitle>Delete this route?</DialogTitle>
                    <DialogDescription>
                        {deleting?.origin} → {deleting?.destination} will be
                        removed from the home page. This cannot be undone.
                    </DialogDescription>
                    <DialogFooter className="gap-2">
                        <DialogClose asChild>
                            <Button variant="secondary">Cancel</Button>
                        </DialogClose>
                        <Button
                            variant="destructive"
                            onClick={() =>
                                deleting &&
                                router.delete(destroy.url(deleting.id), {
                                    onFinish: () => setDeleting(null),
                                })
                            }
                        >
                            Delete route
                        </Button>
                    </DialogFooter>
                </DialogContent>
            </Dialog>
        </>
    );
}

RoutesIndex.layout = {
    breadcrumbs: [
        { title: 'Admin', href: dashboard() },
        { title: 'Routes', href: index() },
    ],
};
