import { useEffect, useState } from "react";
import { Link } from "@inertiajs/react";
import RatingModal from "@/components/RatingModal";
import LogoMark from "@/components/LogoMark";

export default function VerifyPaymentPage() {
    const searchParams = new URLSearchParams(window.location.search);
    const ref = searchParams.get("ref") || "";

    const [reservation, setReservation] = useState<Record<string, any> | null>(null);
    const [showRatingModal, setShowRatingModal] = useState(false);

    useEffect(() => {
        // Recover the reservation context stored before the SumUp redirect
        const stored = localStorage.getItem("RESERVATION");
        if (stored) {
            try {
                setReservation(JSON.parse(stored));
            } catch {
                setReservation(null);
            }
        }

        // Automatically trigger rating modal after 1.2s
        const timer = setTimeout(() => {
            setShowRatingModal(true);
        }, 1200);

        return () => clearTimeout(timer);
    }, []);

    return (
        <div className="relative min-h-screen flex flex-col justify-between overflow-x-hidden bg-cream font-body text-navy">
            {/* Background Subtle Lines */}
            <div className="fixed inset-0 -z-20 flex items-center justify-around pointer-events-none">
                <span className="flex h-full w-[1px] animate-line1 bg-gold/20"></span>
                <span className="flex h-full w-[1px] animate-line2 bg-gold/20"></span>
                <span className="flex h-full w-[1px] animate-line3 bg-gold/20"></span>
            </div>
            {/* Header */}
            <header className="fixed top-0 left-0 z-50 w-full border-b border-gold/20 bg-cream/95 py-4 backdrop-blur-md">
                <div className="mx-auto flex max-w-7xl items-center justify-between px-6 md:px-12">
                    <Link href="/" className="group flex items-center gap-3">
                        <LogoMark color="#0f1f3d" size={34} />
                        <span className="font-display text-xl font-bold tracking-widest text-navy transition-colors group-hover:text-gold">AEROCAB</span>
                    </Link>
                    <Link href="/" className="text-sm font-semibold tracking-wide text-navy/70 transition-colors hover:text-gold">
                        ← Accueil
                    </Link>
                </div>
            </header>

            {/* Main Voucher Container */}
            <main className="flex-1 flex flex-col items-center justify-center px-4 pb-8 pt-24 sm:px-8 sm:pt-28">
                <div className="animate-fade-up w-full max-w-2xl bg-white rounded-3xl p-6 sm:p-10 shadow-xl border border-gold/20 text-center space-y-6">

                    {/* Status Badge */}
                    <div className="inline-flex h-20 w-20 items-center justify-center rounded-full bg-gold/10 border border-gold/40 text-gold mx-auto">
                        <svg className="w-10 h-10" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2.5" d="M5 13l4 4L19 7" />
                        </svg>
                    </div>

                    <div>
                        <span className="text-xs uppercase tracking-widest text-navy font-bold bg-gold/15 border border-gold/40 px-3.5 py-1 rounded-full">
                            Paiement en ligne validé
                        </span>
                        <h1 className="font-display text-3xl tracking-tight text-navy sm:text-4xl mt-3">
                            Réservation <em className="italic text-gold">Confirmée</em>
                        </h1>
                        <p className="text-navy/60 text-sm mt-2">
                            Votre paiement via SumUp a été traité avec succès. Votre chauffeur a été notifié.
                        </p>
                    </div>

                    {/* Details Card */}
                    <div className="bg-cream/70 rounded-2xl p-6 border border-navy/15 text-left space-y-4">
                        <div className="flex justify-between items-center border-b border-gold/20 pb-3">
                            <span className="text-xs text-navy/50 uppercase tracking-wider font-semibold">
                                Référence Dossier
                            </span>
                            <span className="text-lg font-mono font-black text-gold">
                                {ref || "AC-CONFIRMED"}
                            </span>
                        </div>

                        <div className="grid grid-cols-1 sm:grid-cols-2 gap-4 text-sm">
                            <div>
                                <p className="text-xs text-navy/50 uppercase tracking-wide mb-0.5">Référence de réservation</p>
                                <p className="text-navy font-bold">
                                    {reservation?.bookingRef ?? ref}
                                </p>
                            </div>

                            <div>
                                <p className="text-xs text-navy/50 uppercase tracking-wide mb-0.5">Email de confirmation</p>
                                <p className="text-navy font-medium">
                                    {reservation?.contactEmail ?? "—"}
                                </p>
                            </div>

                            <div>
                                <p className="text-xs text-navy/50 uppercase tracking-wide mb-0.5">Statut Paiement</p>
                                <p className="text-navy font-bold flex items-center gap-1.5">
                                    <span className="w-2 h-2 rounded-full bg-gold"></span>
                                    Payé en ligne (€{reservation?.amount ?? "—"} TTC)
                                </p>
                                <p className="text-[11px] text-navy/50">Règlement sécurisé SumUp</p>
                            </div>
                        </div>
                    </div>

                    {/* Action Buttons */}
                    <div className="flex flex-col sm:flex-row justify-center gap-3 pt-2">
                        <button
                            type="button"
                            onClick={() => setShowRatingModal(true)}
                            className="px-5 py-3 font-bold text-navy bg-gold hover:bg-gold-light rounded-xl transition flex items-center justify-center gap-2 cursor-pointer shadow-md"
                        >
                            <span>⭐ Évaluer mon trajet</span>
                        </button>

                        <button
                            type="button"
                            onClick={() => window.print()}
                            className="px-5 py-3 font-semibold text-navy bg-white hover:bg-navy/5 border border-navy/20 rounded-xl transition flex items-center justify-center gap-2 cursor-pointer"
                        >
                            <svg className="w-4 h-4 text-navy/60" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z" />
                            </svg>
                            <span>Imprimer le reçu</span>
                        </button>

                        <Link
                            href="/"
                            className="px-6 py-3 font-bold text-navy bg-gold hover:bg-gold-light rounded-xl transition shadow-md text-center"
                        >
                            Retour à l'accueil
                        </Link>
                    </div>

                </div>
            </main>

            {/* Rating Modal */}
            <RatingModal
                isOpen={showRatingModal}
                bookingRef={ref}
                vehicleName={reservation?.vehicleName}
                onClose={() => setShowRatingModal(false)}
            />

            <footer className="border-t border-gold/15 bg-navy py-6 text-center text-xs text-cream/50">
                &copy; {new Date().getFullYear()} AEROCAB. Navettes aéroport Bruxelles.
            </footer>
        </div>
    );
}

