import { Head, Link, router } from '@inertiajs/react';
import { ArrowLeft, ArrowRight, Accessibility, Thermometer, VolumeX } from 'lucide-react';
import { useState } from 'react';
import PageIntro from '@/components/booking/page-intro';
import SiteFooter from '@/components/home/site-footer';
import SiteHeader from '@/components/home/site-header';
import { cardClass } from '@/components/booking/styles';
import type { TripSummary } from '@/types/booking';
import { cn } from '@/lib/utils';

type Requests = { quiet_ride?: boolean; temperature?: 'cool' | 'standard' | 'warm'; meet_greet_name?: string; wheelchair_storage?: boolean; assisted_boarding?: boolean; chilled_water?: boolean; fast_charging?: boolean; directives?: string };
type Props = { trip: TripSummary; requests: Requests; vehicleName: string; fareCents: number; currency: string };
const money = (cents: number, currency: string) => new Intl.NumberFormat('en-US', { style: 'currency', currency }).format(cents / 100);

export default function SpecialRequests({ trip, requests: initial, vehicleName, fareCents, currency }: Props) {
    const [requests, setRequests] = useState<Requests>({ quiet_ride: false, temperature: 'standard', wheelchair_storage: false, assisted_boarding: false, chilled_water: true, fast_charging: true, ...initial });
    const [processing, setProcessing] = useState(false);
    const set = <K extends keyof Requests>(key: K, value: Requests[K]) => setRequests(current => ({ ...current, [key]: value }));
    const toggle = (key: keyof Requests, title: string, description: string) => <label className="flex cursor-pointer items-start gap-3 rounded-xl border border-navy/15 p-4 dark:border-white/20"><input type="checkbox" checked={Boolean(requests[key])} onChange={event => set(key, event.target.checked)} className="mt-1 size-4 accent-[#0862c7]"/><span><span className="block text-sm font-semibold">{title}</span><span className="mt-1 block text-xs text-navy/65 dark:text-white/65">{description}</span></span></label>;
    const title = trip.destination ? `${trip.pickup} to ${trip.destination}` : trip.pickup;
    return <><Head title="Special Requests & Preferences"/><div className="min-h-screen bg-mist font-google text-navy dark:bg-ink dark:text-white"><SiteHeader variant="solid"/><main className="mx-auto max-w-7xl px-4 pt-10 pb-20 sm:px-6 lg:px-10 lg:pt-16"><PageIntro title="Special Requests & Preferences" breadcrumbs={['Booking', title]}/><div className="mt-10 grid items-start gap-6 lg:grid-cols-[minmax(0,1fr)_360px]"><div className="space-y-5"><section className={cn(cardClass,'p-5 sm:p-7')}><div className="flex items-center gap-3"><Thermometer className="size-5 text-blue-700"/><div><h2 className="text-lg font-semibold">Cabin Environment</h2><p className="text-sm text-navy/65 dark:text-white/65">Set your conversation level and interior climate.</p></div></div><div className="mt-6">{toggle('quiet_ride','Quiet ride','Keep the cabin quiet for calls or relaxation.')}</div><label className="mt-5 block text-sm font-semibold">Cabin temperature</label><div className="mt-2 grid grid-cols-3 gap-2">{(['cool','standard','warm'] as const).map((temp) => <button type="button" key={temp} onClick={() => set('temperature',temp)} className={cn('rounded-lg border px-2 py-3 text-sm font-semibold capitalize',requests.temperature===temp?'border-gold bg-gold text-white':'border-navy/15 dark:border-white/20')}>{temp==='cool'?'❄ ':temp==='warm'?'☀ ':''}{temp}</button>)}</div><label className="mt-5 block text-sm font-semibold" htmlFor="meet_greet_name">Meet & greet name display</label><input id="meet_greet_name" value={requests.meet_greet_name ?? ''} onChange={e=>set('meet_greet_name',e.target.value)} maxLength={120} placeholder="Name or company for the arrival tablet" className="mt-2 w-full rounded-lg border border-navy/15 bg-white px-4 py-3 text-sm dark:border-white/20 dark:bg-ink"/></section><section className={cn(cardClass,'p-5 sm:p-7')}><div className="flex items-center gap-3"><Accessibility className="size-5 text-blue-700"/><div><h2 className="text-lg font-semibold">Accessibility & In-Cabin Support</h2><p className="text-sm text-navy/65 dark:text-white/65">Choose any complimentary services you need.</p></div></div><div className="mt-5 grid gap-3 sm:grid-cols-2">{toggle('wheelchair_storage','Wheelchair storage','Reserve trunk space for a folding wheelchair.')}{toggle('assisted_boarding','Assisted boarding','Request curbside escort and arm support.')}{toggle('chilled_water','Chilled bottled water','Complimentary chilled water.')}{toggle('fast_charging','Fast charging','USB-C, Lightning and MagSafe cables.')}</div></section><section className={cn(cardClass,'p-5 sm:p-7')}><label htmlFor="directives" className="text-lg font-semibold">Flight Notes & Chauffeur Directives</label><textarea id="directives" value={requests.directives ?? ''} onChange={e=>set('directives',e.target.value)} maxLength={500} rows={3} placeholder="Flight information, pickup details, or instructions for your chauffeur..." className="mt-4 w-full resize-y rounded-lg border border-navy/15 bg-white px-4 py-3 text-sm dark:border-white/20 dark:bg-ink"/><p className="mt-1 text-right text-xs text-navy/55 dark:text-white/55">{(requests.directives ?? '').length} / 500</p></section><div className="flex justify-between gap-4"><Link href="/booking/details" className="inline-flex items-center gap-2 rounded-lg border border-navy/15 px-5 py-3 text-sm font-semibold dark:border-white/25"><ArrowLeft className="size-4"/>Back to Booking Summary</Link><button disabled={processing} onClick={()=>router.put('/booking/special-requests',requests,{onStart:()=>setProcessing(true),onFinish:()=>setProcessing(false)})} className="inline-flex items-center gap-2 rounded-lg bg-gold px-6 py-3 text-sm font-semibold text-white disabled:opacity-60">Continue to Payment<ArrowRight className="size-4"/></button></div></div><aside className={cn(cardClass,'p-5 lg:sticky lg:top-6')}><p className="text-xs font-bold tracking-wider text-blue-700 uppercase">Reservation</p><div className="mt-1 flex justify-between gap-3"><h2 className="text-lg font-semibold">{vehicleName}</h2><strong>{money(fareCents,currency)}</strong></div><p className="mt-4 border-t border-navy/10 pt-4 text-sm text-navy/70 dark:border-white/10 dark:text-white/70">{trip.pickup}<br/>{trip.destination ? `to ${trip.destination}` : `${trip.hours ?? ''} hours`}</p><div className="mt-4 flex justify-between border-t border-navy/10 pt-4 font-semibold dark:border-white/10"><span>Total fare</span><span>{money(fareCents,currency)}</span></div><p className="mt-5 flex items-center gap-2 rounded-lg bg-[#fbf8f2] p-3 text-xs text-navy/70 dark:bg-white/5 dark:text-white/70"><VolumeX className="size-4"/>Special requests are complimentary.</p></aside></div></main><SiteFooter className="bg-mist dark:bg-ink"/></div></>;
}
