import { Head, Link, router } from '@inertiajs/react';
import { Search } from 'lucide-react';
import type { FormEvent } from '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 { Input } from '@/components/ui/input';
import { dashboard } from '@/routes/admin';
import { index } from '@/routes/admin/bookings';
import type { BookingSummary } from '@/types/booking';

type Paginated<T> = {
    data: T[];
    current_page: number;
    last_page: number;
    total: number;
    prev_page_url: string | null;
    next_page_url: string | null;
};

type Props = {
    bookings: Paginated<BookingSummary & { is_guest: boolean }>;
    search: string;
};

export default function Bookings({ bookings, search }: Props) {
    const [query, setQuery] = useState(search);

    const submit = (event: FormEvent<HTMLFormElement>) => {
        event.preventDefault();
        router.get(index.url(), query ? { search: query } : {}, {
            preserveState: true,
        });
    };

    return (
        <>
            <Head title="Bookings" />
            <div className="space-y-6 p-4 sm:p-6">
                <Heading
                    title="Bookings"
                    description={`${bookings.total.toLocaleString()} bookings in total`}
                />

                <form onSubmit={submit} className="flex max-w-md gap-2">
                    <div className="relative flex-1">
                        <Search className="absolute top-1/2 left-3 size-4 -translate-y-1/2 text-muted-foreground" />
                        <Input
                            value={query}
                            onChange={(event) => setQuery(event.target.value)}
                            placeholder="Reference, name or email"
                            className="pl-9"
                        />
                    </div>
                    <Button variant="secondary">Search</Button>
                </form>

                <Card>
                    <CardContent className="overflow-x-auto p-0">
                        <table className="w-full min-w-[860px] text-sm">
                            <thead className="border-b text-left text-muted-foreground">
                                <tr>
                                    <th className="p-4 font-medium">Booking</th>
                                    <th className="p-4 font-medium">Pickup</th>
                                    <th className="p-4 font-medium">Route</th>
                                    <th className="p-4 font-medium">
                                        Customer
                                    </th>
                                    <th className="p-4 font-medium">Vehicle</th>
                                    <th className="p-4 font-medium">Fare</th>
                                </tr>
                            </thead>
                            <tbody>
                                {bookings.data.map((booking) => (
                                    <tr
                                        key={booking.reference}
                                        className="border-b align-top last:border-0"
                                    >
                                        <td className="p-4">
                                            <p className="font-semibold tracking-wide">
                                                {booking.reference}
                                            </p>
                                            <Badge
                                                variant="secondary"
                                                className="mt-1"
                                            >
                                                {booking.status_label}
                                            </Badge>
                                        </td>
                                        <td className="p-4 whitespace-nowrap">
                                            <p>{booking.pickup_at_label}</p>
                                            <p className="text-xs text-muted-foreground">
                                                {booking.timezone}
                                            </p>
                                        </td>
                                        <td className="max-w-64 p-4">
                                            <p className="truncate">
                                                {booking.pickup}
                                            </p>
                                            <p className="truncate text-muted-foreground">
                                                {booking.destination ??
                                                    `${booking.hours} hours`}
                                            </p>
                                        </td>
                                        <td className="p-4">
                                            <p>{booking.contact_name}</p>
                                            <p className="text-xs text-muted-foreground">
                                                {booking.contact_email}
                                                {booking.is_guest && ' · guest'}
                                            </p>
                                        </td>
                                        <td className="p-4">
                                            <p>{booking.vehicle}</p>
                                            <p className="text-xs text-muted-foreground">
                                                {booking.passengers} pax ·{' '}
                                                {booking.bags} bags
                                            </p>
                                        </td>
                                        <td className="p-4 font-semibold whitespace-nowrap">
                                            {booking.fare}
                                        </td>
                                    </tr>
                                ))}
                                {bookings.data.length === 0 && (
                                    <tr>
                                        <td
                                            colSpan={6}
                                            className="p-8 text-center text-muted-foreground"
                                        >
                                            No bookings found.
                                        </td>
                                    </tr>
                                )}
                            </tbody>
                        </table>
                    </CardContent>
                </Card>

                {bookings.last_page > 1 && (
                    <div className="flex items-center justify-between text-sm">
                        <span className="text-muted-foreground">
                            Page {bookings.current_page} of {bookings.last_page}
                        </span>
                        <div className="flex gap-2">
                            {bookings.prev_page_url && (
                                <Button variant="outline" size="sm" asChild>
                                    <Link href={bookings.prev_page_url}>
                                        Previous
                                    </Link>
                                </Button>
                            )}
                            {bookings.next_page_url && (
                                <Button variant="outline" size="sm" asChild>
                                    <Link href={bookings.next_page_url}>
                                        Next
                                    </Link>
                                </Button>
                            )}
                        </div>
                    </div>
                )}
            </div>
        </>
    );
}

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