import { Head, useForm, usePage } from '@inertiajs/react';
import Layout from '@/Components/Layout';
import { Card, CardContent } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Textarea } from '@/components/ui/textarea';
import { Label } from '@/components/ui/label';
import { Mail, Phone, MapPin, Send, CheckCircle2, MessageCircleHeart } from 'lucide-react';
import { FormEventHandler, useEffect } from 'react';

interface Settings {
    site_name?: string;
    support_phone?: string;
    support_email?: string;
    address?: string;
}

interface Props {
    settings: Settings;
}

export default function Contact({ settings }: Props) {
    const { flash } = usePage().props as any;
    
    const { data, setData, post, processing, errors, wasSuccessful, reset } = useForm({
        name: '',
        email: '',
        phone: '',
        subject: '',
        message: '',
    });

    const handleSubmit: FormEventHandler = (e) => {
        e.preventDefault();
        post('/contact', {
            preserveScroll: true,
        });
    };

    // Reset form on successful submission
    useEffect(() => {
        if (wasSuccessful) {
            reset();
        }
    }, [wasSuccessful]);

    return (
        <Layout>
            <Head title="Contact Us" />

            {/* Hero Section - Light Modern Style (compact) */}
            <section className="relative overflow-hidden bg-gradient-to-br from-slate-50 via-white to-blue-50/40 py-20 border-b border-slate-100">
                {/* Subtle Mesh Gradient Background */}
                <div className="absolute inset-0 overflow-hidden">
                    <div className="absolute -top-40 -left-40 w-[500px] h-[500px] bg-primary/15 rounded-full filter blur-3xl animate-pulse" />
                    <div className="absolute -bottom-40 -right-40 w-[500px] h-[500px] bg-purple-500/15 rounded-full filter blur-3xl animate-pulse" style={{ animationDelay: '2s' }} />
                </div>

                {/* Grid Pattern - very subtle */}
                <div className="absolute inset-0 bg-[linear-gradient(to_right,#cbd5e10a_1px,transparent_1px),linear-gradient(to_bottom,#cbd5e10a_1px,transparent_1px)] bg-[size:48px_48px]" />

                <div className="container mx-auto px-4 relative z-10">
                    <div className="max-w-3xl mx-auto text-center">
                        <div className="inline-flex items-center gap-2 px-4 py-2 rounded-full bg-white border border-slate-200 shadow-sm text-slate-700 text-sm mb-6">
                            <span className="relative flex h-2 w-2">
                                <span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-emerald-400 opacity-75"></span>
                                <span className="relative inline-flex rounded-full h-2 w-2 bg-emerald-500"></span>
                            </span>
                            <MessageCircleHeart className="w-4 h-4" />
                            <span>We Reply Within 24 Hours</span>
                        </div>
                        <h1 className="text-4xl md:text-5xl lg:text-6xl font-bold mb-4 leading-tight text-slate-900">
                            Get in{' '}
                            <span className="bg-gradient-to-r from-primary via-blue-500 to-purple-500 bg-clip-text text-transparent">
                                Touch
                            </span>
                        </h1>
                        <p className="text-lg md:text-xl text-slate-600 leading-relaxed">
                            Have a question or want to work together? We'd love to hear from you.
                        </p>
                    </div>
                </div>
            </section>

            {/* Contact Form & Info */}
            <section className="py-20 bg-white">
                <div className="container mx-auto px-4">
                    <div className="max-w-6xl mx-auto">
                        <div className="grid lg:grid-cols-3 gap-8">
                            {/* Contact Information */}
                            <div className="lg:col-span-1 space-y-6">
                                <div>
                                    <h2 className="text-2xl font-bold text-slate-900 mb-4">
                                        Contact Information
                                    </h2>
                                    <p className="text-slate-600">
                                        Feel free to reach out to us through any of these channels.
                                    </p>
                                </div>

                                {/* Contact Cards */}
                                <div className="space-y-4">
                                    {settings.support_email && (
                                        <Card className="border-0 shadow-md hover:shadow-lg transition-shadow">
                                            <CardContent className="p-4 flex items-start gap-4">
                                                <div className="shrink-0">
                                                    <div className="w-12 h-12 rounded-lg bg-primary/10 flex items-center justify-center">
                                                        <Mail className="w-6 h-6 text-primary" />
                                                    </div>
                                                </div>
                                                <div>
                                                    <h3 className="font-semibold text-slate-900 mb-1">
                                                        Email
                                                    </h3>
                                                    <a
                                                        href={`mailto:${settings.support_email}`}
                                                        className="text-slate-600 hover:text-primary transition-colors"
                                                    >
                                                        {settings.support_email}
                                                    </a>
                                                </div>
                                            </CardContent>
                                        </Card>
                                    )}

                                    {settings.support_phone && (
                                        <Card className="border-0 shadow-md hover:shadow-lg transition-shadow">
                                            <CardContent className="p-4 flex items-start gap-4">
                                                <div className="shrink-0">
                                                    <div className="w-12 h-12 rounded-lg bg-primary/10 flex items-center justify-center">
                                                        <Phone className="w-6 h-6 text-primary" />
                                                    </div>
                                                </div>
                                                <div>
                                                    <h3 className="font-semibold text-slate-900 mb-1">
                                                        Phone
                                                    </h3>
                                                    <a
                                                        href={`tel:${settings.support_phone}`}
                                                        className="text-slate-600 hover:text-primary transition-colors"
                                                    >
                                                        {settings.support_phone}
                                                    </a>
                                                </div>
                                            </CardContent>
                                        </Card>
                                    )}

                                    {settings.address && (
                                        <Card className="border-0 shadow-md hover:shadow-lg transition-shadow">
                                            <CardContent className="p-4 flex items-start gap-4">
                                                <div className="shrink-0">
                                                    <div className="w-12 h-12 rounded-lg bg-primary/10 flex items-center justify-center">
                                                        <MapPin className="w-6 h-6 text-primary" />
                                                    </div>
                                                </div>
                                                <div>
                                                    <h3 className="font-semibold text-slate-900 mb-1">
                                                        Address
                                                    </h3>
                                                    <p className="text-slate-600">
                                                        {settings.address}
                                                    </p>
                                                </div>
                                            </CardContent>
                                        </Card>
                                    )}
                                </div>
                            </div>

                            {/* Contact Form */}
                            <div className="lg:col-span-2">
                                <Card className="border-0 shadow-xl">
                                    <CardContent className="p-8">
                                        <h2 className="text-2xl font-bold text-slate-900 mb-6">
                                            Send us a Message
                                        </h2>

                                        {/* Success Message */}
                                        {flash?.success && (
                                            <div className="mb-6 p-4 bg-green-50 border border-green-200 rounded-lg flex items-start gap-3">
                                                <CheckCircle2 className="w-5 h-5 text-green-600 shrink-0 mt-0.5" />
                                                <p className="text-green-800">{flash.success}</p>
                                            </div>
                                        )}

                                        <form onSubmit={handleSubmit} className="space-y-6">
                                            {/* Name */}
                                            <div>
                                                <Label htmlFor="name" className="text-slate-900 font-medium">
                                                    Name <span className="text-red-500">*</span>
                                                </Label>
                                                <Input
                                                    id="name"
                                                    type="text"
                                                    value={data.name}
                                                    onChange={(e) => setData('name', e.target.value)}
                                                    className={`mt-1 ${errors.name ? 'border-red-500' : ''}`}
                                                    placeholder="Your name"
                                                />
                                                {errors.name && (
                                                    <p className="mt-1 text-sm text-red-600">{errors.name}</p>
                                                )}
                                            </div>

                                            {/* Email and Phone */}
                                            <div className="grid md:grid-cols-2 gap-6">
                                                <div>
                                                    <Label htmlFor="email" className="text-slate-900 font-medium">
                                                        Email <span className="text-red-500">*</span>
                                                    </Label>
                                                    <Input
                                                        id="email"
                                                        type="email"
                                                        value={data.email}
                                                        onChange={(e) => setData('email', e.target.value)}
                                                        className={`mt-1 ${errors.email ? 'border-red-500' : ''}`}
                                                        placeholder="your.email@example.com"
                                                    />
                                                    {errors.email && (
                                                        <p className="mt-1 text-sm text-red-600">{errors.email}</p>
                                                    )}
                                                </div>

                                                <div>
                                                    <Label htmlFor="phone" className="text-slate-900 font-medium">
                                                        Phone
                                                    </Label>
                                                    <Input
                                                        id="phone"
                                                        type="tel"
                                                        value={data.phone}
                                                        onChange={(e) => setData('phone', e.target.value)}
                                                        className={`mt-1 ${errors.phone ? 'border-red-500' : ''}`}
                                                        placeholder="+880 1234 567890"
                                                    />
                                                    {errors.phone && (
                                                        <p className="mt-1 text-sm text-red-600">{errors.phone}</p>
                                                    )}
                                                </div>
                                            </div>

                                            {/* Subject */}
                                            <div>
                                                <Label htmlFor="subject" className="text-slate-900 font-medium">
                                                    Subject
                                                </Label>
                                                <Input
                                                    id="subject"
                                                    type="text"
                                                    value={data.subject}
                                                    onChange={(e) => setData('subject', e.target.value)}
                                                    className={`mt-1 ${errors.subject ? 'border-red-500' : ''}`}
                                                    placeholder="How can we help?"
                                                />
                                                {errors.subject && (
                                                    <p className="mt-1 text-sm text-red-600">{errors.subject}</p>
                                                )}
                                            </div>

                                            {/* Message */}
                                            <div>
                                                <Label htmlFor="message" className="text-slate-900 font-medium">
                                                    Message <span className="text-red-500">*</span>
                                                </Label>
                                                <Textarea
                                                    id="message"
                                                    value={data.message}
                                                    onChange={(e) => setData('message', e.target.value)}
                                                    className={`mt-1 min-h-[150px] ${errors.message ? 'border-red-500' : ''}`}
                                                    placeholder="Tell us about your project or inquiry..."
                                                />
                                                {errors.message && (
                                                    <p className="mt-1 text-sm text-red-600">{errors.message}</p>
                                                )}
                                            </div>

                                            {/* Submit Button */}
                                            <Button
                                                type="submit"
                                                disabled={processing}
                                                className="w-full md:w-auto bg-primary hover:bg-primary/90 font-semibold px-8 group"
                                                size="lg"
                                            >
                                                {processing ? (
                                                    'Sending...'
                                                ) : (
                                                    <>
                                                        Send Message
                                                        <Send className="w-4 h-4 ml-2 group-hover:translate-x-1 transition-transform" />
                                                    </>
                                                )}
                                            </Button>
                                        </form>
                                    </CardContent>
                                </Card>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

            {/* Map Section (Optional - can be added later) */}
            <section className="py-16 bg-slate-50">
                <div className="container mx-auto px-4">
                    <div className="max-w-6xl mx-auto text-center">
                        <h2 className="text-3xl font-bold text-slate-900 mb-4">
                            Ready to Start Your Project?
                        </h2>
                        <p className="text-lg text-slate-600 max-w-2xl mx-auto">
                            We're here to help you bring your ideas to life. Get in touch with us today
                            and let's discuss how we can help you succeed.
                        </p>
                    </div>
                </div>
            </section>
        </Layout>
    );
}
