import { useState, useEffect } from 'react';
import { Shield, Phone, Mail, User, CheckCircle, AlertCircle, Loader2 } from 'lucide-react';
import { validateName, validatePhone, validateEmail, formatPhoneNumber, submitHeroForm } from '../utils/formUtils';

const Hero = () => {
  const [formData, setFormData] = useState({
    name: '',
    phone: '',
    email: '',
    serviceType: 'medicare'
  });

  const [errors, setErrors] = useState({});
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [submitStatus, setSubmitStatus] = useState(null); // 'success' or 'error'

  const validateForm = () => {
    const newErrors = {};

    if (!validateName(formData.name)) {
      newErrors.name = 'Please enter your full name (at least 2 characters)';
    }

    if (!validatePhone(formData.phone)) {
      newErrors.phone = 'Please enter a valid 10-digit phone number';
    }

    if (!validateEmail(formData.email)) {
      newErrors.email = 'Please enter a valid email address';
    }

    setErrors(newErrors);
    return Object.keys(newErrors).length === 0;
  };

  const handleSubmit = async (e) => {
    e.preventDefault();
    console.log('=== HERO FORM SUBMIT STARTED ===');
    console.log('Form data:', formData);
    console.log('TCPA checkbox checked:', document.getElementById('leadid_tcpa_disclosure')?.checked);
    
    if (!validateForm()) {
      console.log('Form validation failed');
      return;
    }

    console.log('Form validation passed, starting submission');
    setIsSubmitting(true);
    setSubmitStatus(null);

    try {
      console.log('Calling submitHeroForm with data:', formData);
      await submitHeroForm(formData);
      console.log('Hero form submission completed successfully');
      setSubmitStatus('success');
      setFormData({ name: '', phone: '', email: '', serviceType: 'medicare' });
      
      // Reset success message after 5 seconds
      setTimeout(() => {
        setSubmitStatus(null);
      }, 5000);
    } catch (error) {
      console.error('Hero form submission error:', error);
      setSubmitStatus('error');
    } finally {
      setIsSubmitting(false);
    }
  };

  const handleChange = (e) => {
    const { name, value } = e.target;
    
    // Format phone number as user types
    if (name === 'phone') {
      const formatted = formatPhoneNumber(value);
      setFormData({
        ...formData,
        [name]: formatted,
      });
    } else {
      setFormData({
        ...formData,
        [name]: value,
      });
    }

    // Clear error when user starts typing
    if (errors[name]) {
      setErrors({
        ...errors,
        [name]: '',
      });
    }
  };

  return (
    <section
      id="home"
      className="relative min-h-screen flex items-center pt-20 overflow-hidden"
    >
      {/* Background Image with Overlay */}
      <div className="absolute inset-0">
        <img
          src="/micheile-henderson-PpZasS086os-unsplash.jpg"
          alt="Professional insurance and home services"
          className="w-full h-full object-cover"
          loading="eager"
          decoding="async"
        />
        <div className="absolute inset-0 bg-gradient-to-br from-navy-800/95 via-blue-700/90 to-emerald-600/85"></div>
      </div>

      {/* Background Pattern */}
      <div className="absolute inset-0 opacity-10">
        <div className="absolute inset-0" style={{
          backgroundImage: 'radial-gradient(circle at 2px 2px, white 1px, transparent 0)',
          backgroundSize: '40px 40px'
        }}></div>
      </div>

      <div className="relative max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-16 lg:py-24">
        <div className="grid lg:grid-cols-2 gap-12 items-center">
          {/* Left Content */}
          <div className="text-white">
            <div className="inline-flex items-center space-x-2 bg-white/20 backdrop-blur-sm px-4 py-2 rounded-full mb-6 fade-in-up">
              <Shield className="text-blue-400" size={20} />
              <span className="text-sm font-semibold">Trusted Since 2012</span>
            </div>
            
            <h1 className="text-4xl md:text-5xl lg:text-6xl font-bold leading-tight mb-6 fade-in-up animate-delay-100">
              Instant Leads Coverage for Your <span className="text-blue-400">Peace of Mind</span>
            </h1>
            
            <p className="text-xl md:text-2xl text-cream-50 mb-8 leading-relaxed fade-in-up animate-delay-200">
              Comprehensive insurance and home services that protect what matters most to you and your family.
            </p>

            <div className="space-y-4 text-cream-100 fade-in-up animate-delay-300">
              <div className="flex items-center space-x-3 hover:translate-x-2 transition-transform duration-300">
                <div className="flex-shrink-0 w-6 h-6 bg-blue-400 rounded-full flex items-center justify-center">
                  <span className="text-navy-800 font-bold text-sm">✓</span>
                </div>
                <span className="text-lg">Final Expense Services</span>
              </div>
              <div className="flex items-center space-x-3 hover:translate-x-2 transition-transform duration-300">
                <div className="flex-shrink-0 w-6 h-6 bg-blue-400 rounded-full flex items-center justify-center">
                  <span className="text-navy-800 font-bold text-sm">✓</span>
                </div>
                <span className="text-lg">Personalized coverage options</span>
              </div>
              <div className="flex items-center space-x-3 hover:translate-x-2 transition-transform duration-300">
                <div className="flex-shrink-0 w-6 h-6 bg-blue-400 rounded-full flex items-center justify-center">
                  <span className="text-navy-800 font-bold text-sm">✓</span>
                </div>
                <span className="text-lg">Expert guidance and support</span>
              </div>
            </div>
          </div>

          {/* Right Side - Call to Action */}
          <div className="lg:ml-8 fade-in-up animate-delay-400">
            <div className="bg-white rounded-2xl shadow-2xl p-8 md:p-10 hover:shadow-3xl transition-all duration-300">
              <div className="text-center">
                <h2 className="text-3xl font-bold text-navy-800 mb-4">
                  Ready to Get Started?
                </h2>
                <p className="text-navy-700 mb-8">
                  Contact our licensed agents for personalized final expense insurance quotes
                </p>
                
                <div className="space-y-4 sm:space-y-6">
                  <a
                    href="/services"
                    className="block w-full bg-gradient-to-r from-blue-500 to-blue-400 text-navy-800 font-bold py-4 px-8 rounded-lg transition-all duration-200 text-lg hover:from-blue-400 hover:to-blue-300 text-center hover:shadow-2xl hover:scale-105 active:scale-95 shadow-lg"
                  >
                    View Our Services
                  </a>
                  
                  <a
                    href="/contact"
                    className="block w-full bg-white border-2 border-blue-500 text-blue-600 font-bold py-4 px-8 rounded-lg transition-all duration-200 text-lg hover:bg-blue-50 text-center hover:shadow-lg active:scale-95"
                  >
                    Contact Us
                  </a>
                </div>

                <div className="mt-8 text-center">
                  <p className="text-sm text-navy-600 mb-2">Call us directly:</p>
                  <a 
                    href="tel:+1-659-220-0667" 
                    className="inline-flex items-center space-x-3 bg-white text-blue-600 font-bold py-3 px-6 rounded-lg transition-all duration-200 text-xl hover:bg-blue-50 hover:shadow-lg active:scale-95 shadow-md"
                  >
                    <Phone size={24} />
                    <span>+1 (659) 220-0667</span>
                  </a>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
};

export default Hero;
