'use client';

import { useState } from 'react';

export default function ContactPage() {
  const [loading, setLoading] = useState(false);
  const [submitted, setSubmitted] = useState(false);
  const [error, setError] = useState('');
  const [formData, setFormData] = useState({
    firstName: '',
    lastName: '',
    email: '',
    subject: '',
    message: '',
  });

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

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

    try {
      const response = await fetch('/api/contact', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(formData),
      });

      if (response.ok) {
        setSubmitted(true);
        setFormData({ firstName: '', lastName: '', email: '', subject: '', message: '' });
        setTimeout(() => setSubmitted(false), 5000);
      } else {
        const data = await response.json();
        setError(data.message || 'Failed to send message. Please try again.');
      }
    } catch (err) {
      setError('An error occurred. Please try again later.');
      console.error(err);
    } finally {
      setLoading(false);
    }
  };

  return (
    <main className="relative overflow-hidden">
      <div className="absolute inset-0 bg-gradient-to-b from-slate-50 via-white to-slate-50 dark:from-slate-950 dark:via-slate-950 dark:to-slate-900" />
      <div className="absolute -top-24 right-0 h-64 w-64 rounded-full bg-primary/15 blur-3xl" />
      <div className="absolute -bottom-24 left-0 h-64 w-64 rounded-full bg-secondary/15 blur-3xl" />

      <div className="relative max-w-6xl mx-auto px-6 py-16 lg:py-20">
        <div className="text-center mb-12">
          <p className="inline-flex items-center gap-2 text-sm font-semibold tracking-wide uppercase text-primary/80 bg-primary/10 px-3 py-1 rounded-full">
            Have some questions?
          </p>
          <h1 className="mt-4 text-4xl md:text-5xl font-bold text-gray-900 dark:text-white">
            Get in touch with our team
          </h1>
          <p className="mt-3 text-lg text-gray-600 dark:text-gray-400 max-w-2xl mx-auto">
            We’ll help you choose the right webinar and guide you through planning your next steps.
          </p>
        </div>

        <div className="grid lg:grid-cols-[1.1fr_1.4fr] gap-8">
          <div className="rounded-2xl border border-gray-200/70 dark:border-slate-800/70 bg-white/80 dark:bg-slate-900/70 backdrop-blur-md p-8 shadow-lg">
            <h2 className="text-2xl font-semibold text-gray-900 dark:text-white">Contact details</h2>
            <p className="mt-2 text-gray-600 dark:text-gray-400">
              Reach out directly or send a message using the form.
            </p>

            <div className="mt-6 space-y-4">
              <div className="flex items-start gap-3 rounded-xl bg-slate-50 dark:bg-slate-800/60 p-4">
                <div className="h-10 w-10 rounded-lg bg-primary/15 text-primary flex items-center justify-center">📧</div>
                <div>
                  <p className="text-sm text-gray-500 dark:text-gray-400">Email</p>
                  <p className="font-semibold text-gray-900 dark:text-white">support@estateplanning.com</p>
                </div>
              </div>
              <div className="flex items-start gap-3 rounded-xl bg-slate-50 dark:bg-slate-800/60 p-4">
                <div className="h-10 w-10 rounded-lg bg-primary/15 text-primary flex items-center justify-center">📞</div>
                <div>
                  <p className="text-sm text-gray-500 dark:text-gray-400">Phone</p>
                  <p className="font-semibold text-gray-900 dark:text-white">+1 (555) 123-4567</p>
                </div>
              </div>
              <div className="flex items-start gap-3 rounded-xl bg-slate-50 dark:bg-slate-800/60 p-4">
                <div className="h-10 w-10 rounded-lg bg-primary/15 text-primary flex items-center justify-center">📍</div>
                <div>
                  <p className="text-sm text-gray-500 dark:text-gray-400">Office</p>
                  <p className="font-semibold text-gray-900 dark:text-white">123 Main St, Suite 100</p>
                </div>
              </div>
            </div>

            <div className="mt-8 rounded-2xl overflow-hidden border border-gray-200/70 dark:border-slate-800/70">
              <div className="h-40 bg-gradient-to-br from-primary/20 via-transparent to-secondary/20 flex items-center justify-center text-sm text-gray-600 dark:text-gray-400">
                Map preview coming soon
              </div>
            </div>
          </div>

          <div className="rounded-2xl border border-gray-200/70 dark:border-slate-800/70 bg-white dark:bg-slate-900 p-8 shadow-xl">
            {submitted && (
              <div className="mb-6 p-4 bg-success/10 border border-success text-success rounded-lg">
                ✓ Thank you for your message! We'll get back to you soon.
              </div>
            )}
            {error && (
              <div className="mb-6 p-4 bg-danger/10 border border-danger text-danger rounded-lg">
                ✗ {error}
              </div>
            )}
            <form className="space-y-5" onSubmit={handleSubmit}>
              <div className="grid md:grid-cols-2 gap-5">
                <div>
                  <label className="block text-sm font-semibold mb-2 text-gray-700 dark:text-gray-200">First Name *</label>
                  <input
                    className="input-field"
                    type="text"
                    name="firstName"
                    placeholder="John"
                    value={formData.firstName}
                    onChange={handleChange}
                    required
                  />
                </div>
                <div>
                  <label className="block text-sm font-semibold mb-2 text-gray-700 dark:text-gray-200">Last Name *</label>
                  <input
                    className="input-field"
                    type="text"
                    name="lastName"
                    placeholder="Doe"
                    value={formData.lastName}
                    onChange={handleChange}
                    required
                  />
                </div>
              </div>

              <div>
                <label className="block text-sm font-semibold mb-2 text-gray-700 dark:text-gray-200">Email *</label>
                <input
                  className="input-field"
                  type="email"
                  name="email"
                  placeholder="john@example.com"
                  value={formData.email}
                  onChange={handleChange}
                  required
                />
              </div>

              <div>
                <label className="block text-sm font-semibold mb-2 text-gray-700 dark:text-gray-200">Subject *</label>
                <input
                  className="input-field"
                  type="text"
                  name="subject"
                  placeholder="How can we help you?"
                  value={formData.subject}
                  onChange={handleChange}
                  required
                />
              </div>

              <div>
                <label className="block text-sm font-semibold mb-2 text-gray-700 dark:text-gray-200">Message *</label>
                <textarea
                  className="input-field"
                  name="message"
                  rows={6}
                  placeholder="Tell us more about your inquiry..."
                  value={formData.message}
                  onChange={handleChange}
                  required
                />
              </div>

              <button
                type="submit"
                className="btn-primary w-full"
                disabled={loading}
              >
                {loading ? 'Sending...' : 'Send Message'}
              </button>
            </form>
          </div>
        </div>
      </div>
    </main>
  );
}
