'use client';

import { useState, useRef } from 'react';
import Link from 'next/link';

export default function ContactUsPage() {
  const API = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost/agrivet_web/backend';
  const [state, setState] = useState<'idle' | 'sending' | 'done' | 'error'>('idle');
  const formRef = useRef<HTMLFormElement>(null);

  async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
    e.preventDefault();
    setState('sending');
    const fd = new FormData(e.currentTarget);
    try {
      const res = await fetch(`${API}/api/leads`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
        body: JSON.stringify({
          name:    fd.get('name'),
          phone:   fd.get('phone'),
          email:   fd.get('email'),
          subject: fd.get('subject'),
          message: fd.get('message'),
          source:  'contact',
        }),
      });
      const json = await res.json();
      if (json.success) { setState('done'); formRef.current?.reset(); }
      else setState('error');
    } catch { setState('error'); }
  }

  return (
    <>
      {/* ── Banner ────────────────────────────────────────────── */}
      <div
        className="op-hero cu-hero"
        style={{
          background:
            "linear-gradient(135deg,rgba(6,18,10,.88) 0%,rgba(8,24,14,.68) 60%,rgba(8,24,14,.52) 100%), url('/images/school-background.webp') center/cover no-repeat",
        }}
      >
        <div className="op-hero-deco" aria-hidden="true">
          <svg className="deco-leaf-1" viewBox="0 0 200 300" fill="none">
            <path d="M100 10 C160 40 190 120 160 200 C130 280 40 290 20 220 C0 150 40 80 100 10Z" fill="#7abf9e"/>
            <path d="M100 50 C140 70 160 130 140 190 C120 250 60 260 40 200" stroke="#a9d4bc" strokeWidth="1.5" fill="none"/>
          </svg>
          <svg className="deco-leaf-2" viewBox="0 0 200 300" fill="none">
            <path d="M100 10 C160 40 190 120 160 200 C130 280 40 290 20 220 C0 150 40 80 100 10Z" fill="#7abf9e"/>
          </svg>
        </div>
        <div className="container op-hero-inner">
          <nav className="op-breadcrumb" aria-label="Breadcrumb">
            <Link href="/" className="op-bc-item">Home</Link>
            <span className="op-bc-sep" aria-hidden="true">/</span>
            <span className="op-bc-item active">Contact Us</span>
          </nav>
          <h1>Contact <em>Us</em></h1>
        </div>
        <div className="op-hero-wave" aria-hidden="true">
          <svg viewBox="0 0 1440 60" preserveAspectRatio="none" fill="#f7f9f8">
            <path d="M0,40 C360,70 1080,10 1440,40 L1440,60 L0,60Z"/>
          </svg>
        </div>
      </div>

      {/* ── Contact Card ──────────────────────────────────────── */}
      <section className="cu-section">
        <div className="container">
          <div className="cu-card">

            {/* Left info panel */}
            <div className="cu-info-panel">
              <p className="cu-info-title">Corporate Office</p>
              <ul className="cu-info-list">
                <li className="cu-info-item">
                  <i className="fas fa-map-marker-alt cu-info-icon" />
                  <a
                    href="https://www.google.com/maps/place/714,+Lake+Town+Rd,+Block+A,+Block+E,+Lake+Town,+South+Dumdum,+West+Bengal+700089"
                    target="_blank" rel="noopener noreferrer"
                  >
                    714, Lake Town, Block – A, Kolkata – 700089
                  </a>
                </li>
                <li className="cu-info-item">
                  <i className="fas fa-envelope cu-info-icon" />
                  <a href="mailto:info@agrivet.in">info@agrivet.in</a>
                </li>
                <li className="cu-info-item">
                  <i className="fas fa-globe cu-info-icon" />
                  <a href="https://www.agrivet.in" target="_blank" rel="noopener noreferrer">www.agrivet.in</a>
                </li>
                <li className="cu-info-item">
                  <i className="fas fa-phone-alt cu-info-icon" />
                  <a href="tel:+919330022176">+91 93300 22176</a>
                </li>
              </ul>
              <div className="cu-social-row">
                <a className="cu-social-icon" href="https://www.facebook.com/people/Agrivet/100057276916798/" target="_blank" rel="noopener noreferrer" aria-label="Facebook">
                  <i className="fab fa-facebook-f" />
                </a>
                <a className="cu-social-icon" href="https://in.linkedin.com/company/agrivet-consultancy" target="_blank" rel="noopener noreferrer" aria-label="LinkedIn">
                  <i className="fab fa-linkedin-in" />
                </a>
                <a className="cu-social-icon" href="https://www.youtube.com/channel/UC0-yPdp11nyAXsUa2U6QQyQ" target="_blank" rel="noopener noreferrer" aria-label="YouTube">
                  <i className="fab fa-youtube" />
                </a>
              </div>
            </div>

            {/* Right form panel */}
            <div className="cu-form-panel">
              <h2 className="cu-form-title">Send Us a Message</h2>
              <p className="cu-form-sub">We typically respond within one business day.</p>

              {state === 'done' ? (
                <div className="cu-form-success">
                  <i className="fas fa-circle-check" />
                  <p>Thank you! Your message has been received. We'll get back to you soon.</p>
                </div>
              ) : (
                <form className="cu-form" ref={formRef} onSubmit={handleSubmit}>
                  <div className="cu-field-row">
                    <div className="cu-field">
                      <label>Full Name <span className="cu-req">*</span></label>
                      <input type="text" name="name" required placeholder="Your name" />
                    </div>
                    <div className="cu-field">
                      <label>Phone <span className="cu-req">*</span></label>
                      <input type="tel" name="phone" required placeholder="+91 XXXXX XXXXX" />
                    </div>
                  </div>
                  <div className="cu-field-row">
                    <div className="cu-field">
                      <label>Email</label>
                      <input type="email" name="email" placeholder="you@example.com" />
                    </div>
                    <div className="cu-field">
                      <label>Subject</label>
                      <input type="text" name="subject" placeholder="How can we help?" />
                    </div>
                  </div>
                  <div className="cu-field">
                    <label>Message <span className="cu-req">*</span></label>
                    <textarea name="message" required rows={4} placeholder="Tell us more about your enquiry…" />
                  </div>
                  {state === 'error' && (
                    <p className="cu-form-error">Something went wrong. Please try again or email us directly.</p>
                  )}
                  <button type="submit" className="cu-submit-btn" disabled={state === 'sending'}>
                    {state === 'sending'
                      ? <><i className="fas fa-spinner fa-spin" /> Sending…</>
                      : <>Send Message <i className="fas fa-arrow-right" /></>
                    }
                  </button>
                </form>
              )}
            </div>
          </div>
        </div>
      </section>

      {/* ── Location Cards ────────────────────────────────────── */}
      <section className="cu-locations">
        <div className="container">
          <h2 className="cu-locations-h2">Our Locations</h2>
          <div className="cu-loc-grid">

            <div className="cu-loc-card">
              <div className="cu-loc-header">Research Laboratory (Lake Town)</div>
              <div className="cu-loc-body">
                <div className="cu-loc-item"><i className="fas fa-map-marker-alt" /><span>First Floor, 238, Block B, Sreebhumi, Lake Town, South Dum Dum, Kolkata – 700089, West Bengal</span></div>
                <div className="cu-loc-item"><i className="fas fa-envelope" /><a href="mailto:centrallaboratory@agrivet.in">centrallaboratory@agrivet.in</a></div>
                <div className="cu-loc-item"><i className="fas fa-globe" /><a href="https://www.agrivet.in" target="_blank" rel="noopener noreferrer">www.agrivet.in</a></div>
                <div className="cu-loc-item"><i className="fas fa-phone-alt" /><a href="tel:+919330022177">+91 93300 22177</a></div>
              </div>
            </div>

            <div className="cu-loc-card">
              <div className="cu-loc-header">Research Center 1 &amp; Laboratories</div>
              <div className="cu-loc-body">
                <div className="cu-loc-item"><i className="fas fa-map-marker-alt" /><span>Jhampa, P.O. Kaliani, P.S. Deganga, N 24 Parganas – 743423, West Bengal</span></div>
                <div className="cu-loc-item"><i className="fas fa-envelope" /><a href="mailto:info@agrivet.in">info@agrivet.in</a></div>
                <div className="cu-loc-item"><i className="fas fa-globe" /><a href="https://www.agrivet.in" target="_blank" rel="noopener noreferrer">www.agrivet.in</a></div>
                <div className="cu-loc-item"><i className="fas fa-phone-alt" /><a href="tel:+919330022177">+91 93300 22177</a></div>
              </div>
            </div>

            <div className="cu-loc-card">
              <div className="cu-loc-header">Research Center 2</div>
              <div className="cu-loc-body">
                <div className="cu-loc-item"><i className="fas fa-map-marker-alt" /><span>Dakatpota, Golabari, Barasat, 24 Parganas (N), West Bengal – 743 423</span></div>
                <div className="cu-loc-item"><i className="fas fa-envelope" /><a href="mailto:info@agrivet.in">info@agrivet.in</a></div>
                <div className="cu-loc-item"><i className="fas fa-globe" /><a href="https://www.agrivet.in" target="_blank" rel="noopener noreferrer">www.agrivet.in</a></div>
                <div className="cu-loc-item"><i className="fas fa-phone-alt" /><a href="tel:+919330022177">+91 93300 22177</a></div>
              </div>
            </div>

          </div>
        </div>
      </section>
    </>
  );
}
