'use client'

import { useState, useEffect } from 'react'
import Image from 'next/image'
import ReCAPTCHA from "react-google-recaptcha"
import axios from "axios"
import styles from './BookTable.module.css'

interface FormErrors {
    name?: string;
    email?: string;
    phone?: string;
    date?: string;
    captcha?: string;
}

export default function BookTable() {
    const [email, setEmail] = useState("");

    useEffect(() => {
        const user = "hello";
        const domain = "antalyarestaurant.ca";
        setEmail(`${user}@${domain}`);
    }, []);

    const [formData, setFormData] = useState({
        name: "",
        email: "",
        phone: "",
        date: "",
        message: "",
    });

    const [formErrors, setFormErrors] = useState<FormErrors>({});
    const [captchaToken, setCaptchaToken] = useState<string | null>(null);
    const [alert, setAlert] = useState({ show: false, type: "", message: "" });
    const [charCount, setCharCount] = useState(0);

    const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
        const { name, value } = e.target;
        setFormData((prev) => ({ ...prev, [name]: value }));

        if (name === "message") {
            setCharCount(value.length);
        }
    };

    const handleCaptchaChange = (token: string | null) => {
        setCaptchaToken(token);
    };

    const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
        e.preventDefault();

        const errors: FormErrors = {};
        if (!formData.name.trim()) errors.name = "Name is required.";
        if (!formData.email.trim()) errors.email = "Email is required.";
        if (!formData.phone.trim()) errors.phone = "Phone is required.";
        if (!formData.date.trim()) errors.date = "Date is required.";
        if (!captchaToken) errors.captcha = "Please verify the CAPTCHA.";

        setFormErrors(errors);
        if (Object.keys(errors).length > 0) return;

        const emailData = {
            name: formData.name,
            phone: formData.phone,
            email: formData.email,
            subject: `Table Reservation - ${formData.name} on ${formData.date}`,
            message: `
                <strong>Reservation Details:</strong><br/>
                Name: ${formData.name}<br/>
                Email: ${formData.email}<br/>
                Phone: ${formData.phone}<br/>
                Date: ${formData.date}<br/><br/>
                <strong>Message:</strong><br/>
                ${formData.message || "None"}
            `,
            to: email,
            senderName: "Antalya Restaurant - Table Reservation",
            recaptchaToken: captchaToken,
        };

        try {
            const res = await axios.post(
                "https://mailserver.metatronnest.com/send",
                emailData,
                { headers: { "Content-Type": "application/json" } }
            );

            setAlert({
                show: true,
                type: "success",
                message: res?.data?.message || "Reservation request sent successfully! We'll contact you soon.",
            });

            setFormData({
                name: "",
                email: "",
                phone: "",
                date: "",
                message: "",
            });
            setCaptchaToken(null);
            setFormErrors({});
            setCharCount(0);
        } catch (error) {
            setAlert({
                show: true,
                type: "danger",
                message: "Failed to send reservation request. Please try again later.",
            });
        }
    };

    useEffect(() => {
        if (alert.show) {
            const timer = setTimeout(() => {
                setAlert((prev) => ({ ...prev, show: false }));
            }, 5000);
            return () => clearTimeout(timer);
        }
    }, [alert.show]);

    return (
        <section className={styles.section} id="book">
            <div className={styles.imageContainer}>
                <Image
                    src="/images/book-table/book-a-table.webp"
                    alt="Luxury Restaurant Interior"
                    fill
                    style={{ objectFit: 'cover' }}
                    priority
                />
            </div>

            <div className={styles.formContainer}>
                <div className={styles.content}>
                    <div className={styles.smallHeading} style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: '10px' }}>
                        <Image src="/images/dinner.png" alt="Home Reservation Decorative Dinner Icon" width={24} height={24} />
                        <span>ANTALYA</span>
                        <Image src="/images/eat.png" alt="Home Reservation Decorative Cutlery Icon" width={24} height={24} />
                    </div>
                    <h2 className={styles.title}>Book A Table</h2>

                    {alert.show && (
                        <div className={`${styles.alert} ${styles[`alert${alert.type.charAt(0).toUpperCase() + alert.type.slice(1)}`]}`}>
                            {alert.message}
                        </div>
                    )}

                    <form
                        id="reservation-form"
                        onSubmit={handleSubmit}
                        className={styles.form}
                    >
                        {/* Name Input with Placeholder */}
                        <div className={styles.inputGroup}>
                            <label htmlFor="name" className={styles.label}>Name *</label>
                            <input
                                type="text"
                                id="name"
                                name="name"
                                required
                                className={styles.input}
                                placeholder="Name"
                                value={formData.name}
                                onChange={handleChange}
                                suppressHydrationWarning
                            />
                            {formErrors.name && <small className={styles.errorText}>{formErrors.name}</small>}
                        </div>

                        {/* Email Input with Placeholder */}
                        <div className={styles.inputGroup}>
                            <label htmlFor="email" className={styles.label}>Email *</label>
                            <input
                                type="email"
                                id="email"
                                name="email"
                                required
                                className={styles.input}
                                placeholder="Email"
                                value={formData.email}
                                onChange={handleChange}
                                suppressHydrationWarning
                            />
                            {formErrors.email && <small className={styles.errorText}>{formErrors.email}</small>}
                        </div>

                        {/* Phone Input with Placeholder */}
                        <div className={styles.inputGroup}>
                            <label htmlFor="phone" className={styles.label}>Phone *</label>
                            <input
                                type="tel"
                                id="phone"
                                name="phone"
                                required
                                className={styles.input}
                                placeholder="Phone"
                                value={formData.phone}
                                onChange={handleChange}
                                suppressHydrationWarning
                            />
                            {formErrors.phone && <small className={styles.errorText}>{formErrors.phone}</small>}
                        </div>

                        <div className={styles.inputGroup}>
                            <label htmlFor="date" className={styles.label}>Date *</label>
                            <input
                                type="date"
                                id="date"
                                name="date"
                                required
                                className={styles.input}
                                value={formData.date}
                                onChange={handleChange}
                            />
                            {formErrors.date && <small className={styles.errorText}>{formErrors.date}</small>}
                        </div>

                        {/* Message Textarea with Placeholder */}
                        <div className={styles.inputGroup}>
                            <label htmlFor="message" className={styles.label}>Message</label>
                            <textarea
                                id="message"
                                name="message"
                                className={styles.textarea}
                                placeholder="Message..."
                                maxLength={500}
                                value={formData.message}
                                onChange={handleChange}
                            />
                            <span className={styles.charCount}>{charCount}/500 characters</span>
                        </div>

                        <div className={styles.recaptchaWrapper}>
                            <ReCAPTCHA
                                sitekey="6Ld1T6UsAAAAAMkLyI4diNDMBYc2n_SImmaOumJZ"
                                onChange={handleCaptchaChange}
                            />
                            {formErrors.captcha && <small className={styles.errorText}>{formErrors.captcha}</small>}
                        </div>

                        <button
                            type="submit"
                            className={styles.submitButton}
                            suppressHydrationWarning
                        >
                            Submit Reservation
                        </button>
                    </form>
                </div>
            </div>
        </section>
    )
}