'use client';

import { useState, useRef, useEffect, FormEvent } from 'react';

type FormData = {
  name: string;
  email: string;
  company: string;
  phone: string;
  message: string;
};

type FormErrors = Partial<Record<keyof FormData, string>>;
type SubmitStatus = 'idle' | 'loading' | 'success' | 'error';

const initialData: FormData = {
  name: '',
  email: '',
  company: '',
  phone: '',
  message: '',
};

function validateForm(data: FormData): FormErrors {
  const errors: FormErrors = {};
  if (!data.name.trim()) errors.name = 'El nombre es requerido';
  if (!data.email.trim()) {
    errors.email = 'El email es requerido';
  } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(data.email)) {
    errors.email = 'Ingresa un email válido';
  }
  if (!data.company.trim()) errors.company = 'La empresa es requerida';
  if (data.phone && !/^[+\d\s\-().]{7,20}$/.test(data.phone)) {
    errors.phone = 'Ingresa un teléfono válido';
  }
  if (!data.message.trim()) {
    errors.message = 'El mensaje es requerido';
  } else if (data.message.trim().length < 10) {
    errors.message = 'El mensaje debe tener al menos 10 caracteres';
  }
  return errors;
}

export default function ContactForm() {
  const [formData, setFormData] = useState<FormData>(initialData);
  const [errors, setErrors] = useState<FormErrors>({});
  const [touched, setTouched] = useState<Partial<Record<keyof FormData, boolean>>>({});
  const [status, setStatus] = useState<SubmitStatus>('idle');
  const sectionRef = useRef<HTMLElement>(null);

  useEffect(() => {
    const observer = new IntersectionObserver(
      (entries) => {
        entries.forEach((entry) => {
          if (entry.isIntersecting) {
            entry.target.classList.add('opacity-100', 'translate-y-0');
            entry.target.classList.remove('opacity-0', 'translate-y-6');
          }
        });
      },
      { threshold: 0.1 }
    );
    const elements = sectionRef.current?.querySelectorAll('.reveal');
    elements?.forEach((el) => observer.observe(el));
    return () => observer.disconnect();
  }, []);

  const handleChange = (
    e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
  ) => {
    const { name, value } = e.target;
    setFormData((prev) => ({ ...prev, [name]: value }));
    if (touched[name as keyof FormData]) {
      const newErrors = validateForm({ ...formData, [name]: value });
      setErrors((prev) => ({ ...prev, [name]: newErrors[name as keyof FormData] }));
    }
  };

  const handleBlur = (
    e: React.FocusEvent<HTMLInputElement | HTMLTextAreaElement>
  ) => {
    const { name } = e.target;
    setTouched((prev) => ({ ...prev, [name]: true }));
    const newErrors = validateForm(formData);
    setErrors((prev) => ({ ...prev, [name]: newErrors[name as keyof FormData] }));
  };

  const handleSubmit = async (e: FormEvent) => {
    e.preventDefault();
    const allTouched = Object.keys(formData).reduce(
      (acc, k) => ({ ...acc, [k]: true }),
      {} as Record<keyof FormData, boolean>
    );
    setTouched(allTouched);

    const validationErrors = validateForm(formData);
    if (Object.keys(validationErrors).length > 0) {
      setErrors(validationErrors);
      return;
    }

    setStatus('loading');
    // Simulate API call
    await new Promise((resolve) => setTimeout(resolve, 1800));
    setStatus('success');
    setFormData(initialData);
    setTouched({});
    setErrors({});
  };

  const handleReset = () => {
    setStatus('idle');
    setFormData(initialData);
    setErrors({});
    setTouched({});
  };

  return (
    <section
      className="section bg-white"
      id="contact"
      ref={sectionRef}
      aria-labelledby="contact-heading"
    >
      <div className="container">
        <div className="grid grid-cols-1 lg:grid-cols-2 gap-16 lg:gap-16 items-start">
          {/* Left info panel */}
          <div className="reveal opacity-0 translate-y-6 transition-all duration-700 ease-out flex flex-col gap-6 lg:sticky lg:top-[104px]">
            <div className="mb-2">
              <span className="badge badge-dark">Contacto</span>
            </div>
            <h2
              id="contact-heading"
              className="text-[clamp(1.875rem,5vw,3rem)] font-black tracking-[-0.04em] leading-[1.05] text-black"
            >
              Hablemos de
              <br />
              tu negocio
            </h2>
            <p className="text-lg text-gray-600 leading-relaxed">
              Cuéntanos qué necesitas. Nuestro equipo responderá en menos de 24 horas
              con una propuesta personalizada.
            </p>

            {/* Contact details */}
            <div className="flex flex-col gap-4 p-6 bg-gray-50 border border-gray-200 rounded-xl">
              <div className="flex items-center gap-4">
                <div className="w-10 h-10 bg-white border border-gray-200 rounded-lg flex items-center justify-center shrink-0 text-black">
                  <svg width="18" height="18" viewBox="0 0 24 24" fill="none" aria-hidden="true">
                    <path
                      d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"
                      stroke="currentColor"
                      strokeWidth="2"
                    />
                    <polyline points="22,6 12,13 2,6" stroke="currentColor" strokeWidth="2" />
                  </svg>
                </div>
                <div>
                  <div className="text-xs font-semibold uppercase tracking-widest text-gray-400">Email</div>
                  <div className="text-base font-semibold text-black">hola@flowsync.io</div>
                </div>
              </div>

              <div className="flex items-center gap-4">
                <div className="w-10 h-10 bg-white border border-gray-200 rounded-lg flex items-center justify-center shrink-0 text-black">
                  <svg width="18" height="18" viewBox="0 0 24 24" fill="none" aria-hidden="true">
                    <path
                      d="M22 16.92v3a2 2 0 01-2.18 2 19.79 19.79 0 01-8.63-3.07A19.5 19.5 0 013.07 9.62a19.79 19.79 0 01-3.07-8.63A2 2 0 012.18 0h3a2 2 0 012 1.72c.127.96.361 1.903.7 2.81a2 2 0 01-.45 2.11L6.91 7.91a16 16 0 006.18 6.18l1.28-1.27a2 2 0 012.11-.45c.907.339 1.85.573 2.81.7A2 2 0 0122 16.92z"
                      stroke="currentColor"
                      strokeWidth="2"
                    />
                  </svg>
                </div>
                <div>
                  <div className="text-xs font-semibold uppercase tracking-widest text-gray-400">Teléfono</div>
                  <div className="text-base font-semibold text-black">+1 (800) 123-4567</div>
                </div>
              </div>

              <div className="flex items-center gap-4">
                <div className="w-10 h-10 bg-white border border-gray-200 rounded-lg flex items-center justify-center shrink-0 text-black">
                  <svg width="18" height="18" viewBox="0 0 24 24" fill="none" aria-hidden="true">
                    <circle cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="2" />
                    <polyline points="12 6 12 12 16 14" stroke="currentColor" strokeWidth="2" />
                  </svg>
                </div>
                <div>
                  <div className="text-xs font-semibold uppercase tracking-widest text-gray-400">Respuesta</div>
                  <div className="text-base font-semibold text-black">Menos de 24 horas</div>
                </div>
              </div>
            </div>

            {/* Testimonial */}
            <blockquote className="bg-black text-white rounded-xl p-6 flex flex-col gap-4">
              <p className="text-base leading-relaxed text-white/85 italic">
                &ldquo;FlowSync transformó la forma en que colaboramos. Lo que antes
                tomaba días, ahora lo hacemos en horas.&rdquo;
              </p>
              <footer className="flex items-center gap-3">
                <div className="w-10 h-10 bg-white/15 rounded-full flex items-center justify-center text-xs font-bold tracking-wider shrink-0">
                  MR
                </div>
                <div>
                  <div className="text-sm font-bold">María Rodríguez</div>
                  <div className="text-xs text-white/50">CTO en TechLatam</div>
                </div>
              </footer>
            </blockquote>
          </div>

          {/* Form panel */}
          <div className="reveal opacity-0 translate-y-6 transition-all duration-700 ease-out delay-150 bg-white border border-gray-200 rounded-2xl p-10 max-sm:p-6 shadow-lg">
            {status === 'success' ? (
              <div
                className="flex flex-col items-center justify-center text-center gap-4 py-16 px-8 animate-scaleIn"
                role="alert"
                aria-live="polite"
              >
                <div className="w-18 h-18 bg-black text-white rounded-full flex items-center justify-center mb-2">
                  <svg width="32" height="32" viewBox="0 0 32 32" fill="none" aria-hidden="true">
                    <path
                      d="M6 16l7 7L26 9"
                      stroke="currentColor"
                      strokeWidth="3"
                      strokeLinecap="round"
                      strokeLinejoin="round"
                    />
                  </svg>
                </div>
                <h3 className="text-2xl font-extrabold tracking-tight text-black">
                  ¡Mensaje enviado!
                </h3>
                <p className="text-base text-gray-600 max-w-[320px] leading-relaxed mb-4">
                  Gracias por contactarnos. Nuestro equipo te responderá en menos de 24 horas.
                </p>
                <button
                  className="btn btn-secondary"
                  onClick={handleReset}
                  id="contact-send-another-btn"
                >
                  Enviar otro mensaje
                </button>
              </div>
            ) : (
              <form
                className="flex flex-col gap-5"
                onSubmit={handleSubmit}
                noValidate
                aria-label="Formulario de contacto"
                id="contact-form"
              >
                <div className="mb-2">
                  <h3 className="text-2xl font-extrabold tracking-tight text-black">
                    Envíanos un mensaje
                  </h3>
                  <p className="text-sm text-gray-400 mt-1">
                    Todos los campos marcados con * son requeridos
                  </p>
                </div>

                {/* Row: Name + Email */}
                <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
                  <div className="flex flex-col gap-2">
                    <label htmlFor="contact-name" className="text-sm font-semibold text-dark">
                      Nombre completo <span className="text-error">*</span>
                    </label>
                    <input
                      id="contact-name"
                      name="name"
                      type="text"
                      className={`w-full px-4 py-3 text-base font-sans text-dark bg-white border-[1.5px] rounded-lg transition-all duration-150 outline-none placeholder:text-gray-400 focus:border-black focus:shadow-[0_0_0_3px_rgba(0,0,0,0.06)] ${errors.name && touched.name
                          ? 'border-error shadow-[0_0_0_3px_rgba(255,59,59,0.08)]'
                          : 'border-gray-200'
                        }`}
                      placeholder="Juan García"
                      value={formData.name}
                      onChange={handleChange}
                      onBlur={handleBlur}
                      autoComplete="name"
                      aria-required="true"
                      aria-describedby={errors.name && touched.name ? 'name-error' : undefined}
                      aria-invalid={!!(errors.name && touched.name)}
                    />
                    {errors.name && touched.name && (
                      <span id="name-error" className="text-xs text-error font-medium flex items-center gap-1" role="alert">
                        {errors.name}
                      </span>
                    )}
                  </div>

                  <div className="flex flex-col gap-2">
                    <label htmlFor="contact-email" className="text-sm font-semibold text-dark">
                      Email <span className="text-error">*</span>
                    </label>
                    <input
                      id="contact-email"
                      name="email"
                      type="email"
                      className={`w-full px-4 py-3 text-base font-sans text-dark bg-white border-[1.5px] rounded-lg transition-all duration-150 outline-none placeholder:text-gray-400 focus:border-black focus:shadow-[0_0_0_3px_rgba(0,0,0,0.06)] ${errors.email && touched.email
                          ? 'border-error shadow-[0_0_0_3px_rgba(255,59,59,0.08)]'
                          : 'border-gray-200'
                        }`}
                      placeholder="juan@empresa.com"
                      value={formData.email}
                      onChange={handleChange}
                      onBlur={handleBlur}
                      autoComplete="email"
                      aria-required="true"
                      aria-describedby={errors.email && touched.email ? 'email-error' : undefined}
                      aria-invalid={!!(errors.email && touched.email)}
                    />
                    {errors.email && touched.email && (
                      <span id="email-error" className="text-xs text-error font-medium flex items-center gap-1" role="alert">
                        {errors.email}
                      </span>
                    )}
                  </div>
                </div>

                {/* Row: Company + Phone */}
                <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
                  <div className="flex flex-col gap-2">
                    <label htmlFor="contact-company" className="text-sm font-semibold text-dark">
                      Empresa <span className="text-error">*</span>
                    </label>
                    <input
                      id="contact-company"
                      name="company"
                      type="text"
                      className={`w-full px-4 py-3 text-base font-sans text-dark bg-white border-[1.5px] rounded-lg transition-all duration-150 outline-none placeholder:text-gray-400 focus:border-black focus:shadow-[0_0_0_3px_rgba(0,0,0,0.06)] ${errors.company && touched.company
                          ? 'border-error shadow-[0_0_0_3px_rgba(255,59,59,0.08)]'
                          : 'border-gray-200'
                        }`}
                      placeholder="Mi Empresa S.A."
                      value={formData.company}
                      onChange={handleChange}
                      onBlur={handleBlur}
                      autoComplete="organization"
                      aria-required="true"
                      aria-describedby={errors.company && touched.company ? 'company-error' : undefined}
                      aria-invalid={!!(errors.company && touched.company)}
                    />
                    {errors.company && touched.company && (
                      <span id="company-error" className="text-xs text-error font-medium flex items-center gap-1" role="alert">
                        {errors.company}
                      </span>
                    )}
                  </div>

                  <div className="flex flex-col gap-2">
                    <label htmlFor="contact-phone" className="text-sm font-semibold text-dark">
                      Teléfono
                      <span className="font-normal text-gray-400"> (opcional)</span>
                    </label>
                    <input
                      id="contact-phone"
                      name="phone"
                      type="tel"
                      className={`w-full px-4 py-3 text-base font-sans text-dark bg-white border-[1.5px] rounded-lg transition-all duration-150 outline-none placeholder:text-gray-400 focus:border-black focus:shadow-[0_0_0_3px_rgba(0,0,0,0.06)] ${errors.phone && touched.phone
                          ? 'border-error shadow-[0_0_0_3px_rgba(255,59,59,0.08)]'
                          : 'border-gray-200'
                        }`}
                      placeholder="+52 55 1234 5678"
                      value={formData.phone}
                      onChange={handleChange}
                      onBlur={handleBlur}
                      autoComplete="tel"
                      aria-describedby={errors.phone && touched.phone ? 'phone-error' : undefined}
                      aria-invalid={!!(errors.phone && touched.phone)}
                    />
                    {errors.phone && touched.phone && (
                      <span id="phone-error" className="text-xs text-error font-medium flex items-center gap-1" role="alert">
                        {errors.phone}
                      </span>
                    )}
                  </div>
                </div>

                {/* Message */}
                <div className="flex flex-col gap-2">
                  <label htmlFor="contact-message" className="text-sm font-semibold text-dark">
                    Mensaje <span className="text-error">*</span>
                  </label>
                  <textarea
                    id="contact-message"
                    name="message"
                    className={`w-full px-4 py-3 text-base font-sans text-dark bg-white border-[1.5px] rounded-lg transition-all duration-150 outline-none placeholder:text-gray-400 focus:border-black focus:shadow-[0_0_0_3px_rgba(0,0,0,0.06)] resize-y min-h-[120px] ${errors.message && touched.message
                        ? 'border-error shadow-[0_0_0_3px_rgba(255,59,59,0.08)]'
                        : 'border-gray-200'
                      }`}
                    placeholder="Cuéntanos sobre tu proyecto, equipo y qué quieres lograr con FlowSync..."
                    rows={5}
                    value={formData.message}
                    onChange={handleChange}
                    onBlur={handleBlur}
                    aria-required="true"
                    aria-describedby={errors.message && touched.message ? 'message-error' : undefined}
                    aria-invalid={!!(errors.message && touched.message)}
                  />
                  <div className="flex justify-between items-center">
                    {errors.message && touched.message ? (
                      <span id="message-error" className="text-xs text-error font-medium flex items-center gap-1" role="alert">
                        {errors.message}
                      </span>
                    ) : (
                      <span />
                    )}
                    <span className="text-xs text-gray-400 ml-auto">
                      {formData.message.length} caracteres
                    </span>
                  </div>
                </div>

                {/* Submit */}
                <button
                  type="submit"
                  className="btn btn-primary btn-lg w-full justify-center mt-2 disabled:opacity-70 disabled:cursor-not-allowed disabled:transform-none"
                  disabled={status === 'loading'}
                  id="contact-submit-btn"
                  aria-busy={status === 'loading'}
                >
                  {status === 'loading' ? (
                    <>
                      <span
                        className="w-[18px] h-[18px] border-2 border-white/30 border-t-white rounded-full animate-[spin_0.7s_linear_infinite] shrink-0"
                        aria-hidden="true"
                      />
                      Enviando...
                    </>
                  ) : (
                    <>
                      Enviar mensaje
                      <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
                        <path
                          d="M2 8h12M10 4l4 4-4 4"
                          stroke="currentColor"
                          strokeWidth="2"
                          strokeLinecap="round"
                          strokeLinejoin="round"
                        />
                      </svg>
                    </>
                  )}
                </button>

                <p className="text-xs text-gray-400 text-center leading-relaxed">
                  Al enviar, aceptas nuestra{' '}
                  <a href="#" className="text-gray-600 underline underline-offset-2 transition-colors duration-150 hover:text-black">
                    política de privacidad
                  </a>
                  . Nunca compartiremos tu información.
                </p>
              </form>
            )}
          </div>
        </div>
      </div>
    </section>
  );
}
