﻿import React, { useState } from 'react';
import { Head, useForm } from '@inertiajs/react';
import { PublicLayout } from '@/Layouts/PublicLayout';
import { Modal } from '@/Components/Modal';
import { CurrencyPrice } from '@/Components/CurrencyPrice';
import { Flower2, Clock, Sparkles, Check, Calendar, Heart } from 'lucide-react';

interface Props {
    categories: any[];
}

export default function Spa({ categories = [] }: Props) {
    const [bookingModalOpen, setBookingModalOpen] = useState(false);
    const [selectedTreatment, setSelectedTreatment] = useState<any>(null);

    const appointmentForm = useForm({
        spa_treatment_id: '',
        guest_name: '',
        guest_email: '',
        guest_phone: '',
        appointment_date: new Date().toISOString().split('T')[0],
        appointment_time: '14:00',
        notes: '',
    });

    const openBooking = (treatment: any) => {
        setSelectedTreatment(treatment);
        appointmentForm.setData('spa_treatment_id', treatment.id);
        setBookingModalOpen(true);
    };

    const handleSubmit = (e: React.FormEvent) => {
        e.preventDefault();
        appointmentForm.post('/spa/book-treatment', {
            preserveScroll: true,
            onSuccess: () => {
                setBookingModalOpen(false);
                appointmentForm.reset();
            },
        });
    };

    return (
        <PublicLayout>
            <Head title="Spa Sanctuary & Wellness — Primon Hotel Naalya" />

            {/* Header Hero */}
            <section className="relative py-28 bg-[#1A1A1A] text-[#FBF9F5] text-center border-b border-[#C5A880]/20">
                <div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 space-y-4">
                    <span className="text-xs uppercase tracking-[0.3em] text-[#C5A880] font-semibold">
                        Holistic Sanctuary
                    </span>
                    <h1 className="font-serif text-4xl sm:text-6xl font-light text-white">
                        Primon Spa Sanctuary
                    </h1>
                    <p className="text-sm sm:text-base text-gray-300 font-light max-w-2xl mx-auto leading-relaxed">
                        Restorative therapies combining African botanicals, eucalyptus oils, and deep tissue acupressure to dissolve stress and fatigue.
                    </p>
                </div>
            </section>

            {/* Spa Intro */}
            <section className="py-20 bg-[#FBF9F5]">
                <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 space-y-16">
                    <div className="grid grid-cols-1 lg:grid-cols-12 gap-12 items-center">
                        <div className="lg:col-span-6 space-y-6">
                            <span className="text-xs uppercase tracking-[0.25em] text-[#C5A880] font-semibold">
                                The Wellness Experience
                            </span>
                            <h2 className="font-serif text-3xl sm:text-5xl font-light leading-tight">
                                Tailored Therapies for Restorative Rejuvenation.
                            </h2>
                            <p className="text-sm sm:text-base text-gray-700 font-light leading-relaxed">
                                Our private treatment rooms and couples suite offer an intimate haven of soft lighting, calming aromas, and herbal infusions. Every ritual is customized by our certified therapists to restore vitality and inner calm.
                            </p>
                            <div className="grid grid-cols-2 gap-4 pt-2">
                                <div className="p-4 rounded-2xl bg-[#F3EFEA] border border-[#C5A880]/20">
                                    <Clock className="h-5 w-5 text-[#C5A880] mb-2" />
                                    <h4 className="font-serif text-base font-medium">Daily 09:00 - 21:00</h4>
                                    <p className="text-xs text-gray-500 mt-0.5">Flexible appointment booking</p>
                                </div>
                                <div className="p-4 rounded-2xl bg-[#F3EFEA] border border-[#C5A880]/20">
                                    <Heart className="h-5 w-5 text-[#C5A880] mb-2" />
                                    <h4 className="font-serif text-base font-medium">Couples Suite</h4>
                                    <p className="text-xs text-gray-500 mt-0.5">Private dual rituals available</p>
                                </div>
                            </div>
                        </div>

                        <div className="lg:col-span-6">
                            <img
                                src="https://images.unsplash.com/photo-1540555700478-4be289fbecef?auto=format&fit=crop&w=1200&q=80"
                                alt="Primon Spa Suite"
                                className="rounded-3xl shadow-2xl w-full h-[400px] object-cover border border-[#C5A880]/30"
                            />
                        </div>
                    </div>

                    {/* Treatment Categories and List */}
                    <div className="space-y-16 pt-8 border-t border-gray-200">
                        {categories.map((category) => (
                            <div key={category.id} className="space-y-8">
                                <div>
                                    <span className="text-[10px] uppercase tracking-widest text-[#C5A880] font-semibold">
                                        Treatment Collection
                                    </span>
                                    <h3 className="font-serif text-3xl font-light text-[#1A1A1A]">
                                        {category.name}
                                    </h3>
                                    <p className="text-xs text-gray-600 font-light mt-1 max-w-xl">
                                        {category.description}
                                    </p>
                                </div>

                                <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                                    {(category.treatments || []).map((treatment: any) => (
                                        <div
                                            key={treatment.id}
                                            className="bg-[#F3EFEA] rounded-2xl p-6 border border-[#C5A880]/20 flex flex-col justify-between space-y-4 shadow-sm hover:shadow-md transition-shadow"
                                        >
                                            <div className="space-y-2">
                                                <div className="flex items-center justify-between text-xs text-gray-500">
                                                    <span className="flex items-center gap-1">
                                                        <Clock className="h-3.5 w-3.5 text-[#C5A880]" />
                                                        {treatment.duration_minutes} Minutes
                                                    </span>
                                                    <span className="font-serif text-base font-bold text-[#C5A880]">
                                                        <CurrencyPrice amountUsd={treatment.price_usd} />
                                                    </span>
                                                </div>
                                                <h4 className="font-serif text-xl font-medium text-[#1A1A1A]">
                                                    {treatment.name}
                                                </h4>
                                                <p className="text-xs text-gray-600 font-light leading-relaxed">
                                                    {treatment.description}
                                                </p>
                                            </div>

                                            <button
                                                onClick={() => openBooking(treatment)}
                                                className="w-full bg-[#1A1A1A] hover:bg-[#C5A880] hover:text-[#1A1A1A] text-[#FBF9F5] py-2.5 rounded-xl text-xs font-semibold uppercase tracking-wider transition-all"
                                            >
                                                Book Appointment
                                            </button>
                                        </div>
                                    ))}
                                </div>
                            </div>
                        ))}
                    </div>
                </div>
            </section>

            {/* Spa Appointment Modal */}
            <Modal
                isOpen={bookingModalOpen}
                onClose={() => setBookingModalOpen(false)}
                title={`Book Treatment — ${selectedTreatment?.name || 'Primon Spa'}`}
            >
                <form onSubmit={handleSubmit} className="space-y-4">
                    <div className="p-3 bg-[#F3EFEA] rounded-xl border border-[#C5A880]/30 text-xs flex justify-between items-center">
                        <span className="font-semibold text-[#1A1A1A]">{selectedTreatment?.name}</span>
                        <span className="text-[#C5A880] font-serif font-bold">
                            <CurrencyPrice amountUsd={selectedTreatment?.price_usd || 0} /> • {selectedTreatment?.duration_minutes} Mins
                        </span>
                    </div>

                    <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
                        <div>
                            <label className="text-xs font-semibold uppercase tracking-wider text-gray-600 block mb-1">
                                Full Name
                            </label>
                            <input
                                type="text"
                                required
                                value={appointmentForm.data.guest_name}
                                onChange={(e) => appointmentForm.setData('guest_name', e.target.value)}
                                className="w-full bg-white border border-gray-300 rounded-xl px-3.5 py-2.5 text-xs focus:outline-none focus:border-[#C5A880]"
                            />
                        </div>
                        <div>
                            <label className="text-xs font-semibold uppercase tracking-wider text-gray-600 block mb-1">
                                Email Address
                            </label>
                            <input
                                type="email"
                                required
                                value={appointmentForm.data.guest_email}
                                onChange={(e) => appointmentForm.setData('guest_email', e.target.value)}
                                className="w-full bg-white border border-gray-300 rounded-xl px-3.5 py-2.5 text-xs focus:outline-none focus:border-[#C5A880]"
                            />
                        </div>
                    </div>

                    <div className="grid grid-cols-1 sm:grid-cols-3 gap-4">
                        <div>
                            <label className="text-xs font-semibold uppercase tracking-wider text-gray-600 block mb-1">
                                Phone
                            </label>
                            <input
                                type="text"
                                required
                                value={appointmentForm.data.guest_phone}
                                onChange={(e) => appointmentForm.setData('guest_phone', e.target.value)}
                                className="w-full bg-white border border-gray-300 rounded-xl px-3.5 py-2.5 text-xs focus:outline-none focus:border-[#C5A880]"
                            />
                        </div>
                        <div>
                            <label className="text-xs font-semibold uppercase tracking-wider text-gray-600 block mb-1">
                                Date
                            </label>
                            <input
                                type="date"
                                required
                                value={appointmentForm.data.appointment_date}
                                onChange={(e) => appointmentForm.setData('appointment_date', e.target.value)}
                                className="w-full bg-white border border-gray-300 rounded-xl px-3.5 py-2.5 text-xs focus:outline-none focus:border-[#C5A880]"
                            />
                        </div>
                        <div>
                            <label className="text-xs font-semibold uppercase tracking-wider text-gray-600 block mb-1">
                                Time Slot
                            </label>
                            <select
                                value={appointmentForm.data.appointment_time}
                                onChange={(e) => appointmentForm.setData('appointment_time', e.target.value)}
                                className="w-full bg-white border border-gray-300 rounded-xl px-3.5 py-2.5 text-xs focus:outline-none focus:border-[#C5A880]"
                            >
                                <option value="10:00">10:00 AM</option>
                                <option value="11:30">11:30 AM</option>
                                <option value="14:00">02:00 PM</option>
                                <option value="15:30">03:30 PM</option>
                                <option value="17:00">05:00 PM</option>
                                <option value="18:30">06:30 PM</option>
                            </select>
                        </div>
                    </div>

                    <div>
                        <label className="text-xs font-semibold uppercase tracking-wider text-gray-600 block mb-1">
                            Pressure Preferences or Health Notes
                        </label>
                        <textarea
                            rows={2}
                            placeholder="e.g. Medium-firm pressure, focus on shoulder tension"
                            value={appointmentForm.data.notes}
                            onChange={(e) => appointmentForm.setData('notes', e.target.value)}
                            className="w-full bg-white border border-gray-300 rounded-xl px-3.5 py-2 text-xs focus:outline-none focus:border-[#C5A880]"
                        />
                    </div>

                    <button
                        type="submit"
                        disabled={appointmentForm.processing}
                        className="w-full bg-[#C5A880] hover:bg-[#DFC7A5] text-[#1A1A1A] py-3.5 rounded-xl text-xs font-bold uppercase tracking-widest transition-all shadow-md"
                    >
                        Confirm Spa Appointment
                    </button>
                </form>
            </Modal>
        </PublicLayout>
    );
}
