const { useState, useEffect, useRef } = React;

/* ------------------------------------------------------------------ */
/*  DATA                                                               */
/* ------------------------------------------------------------------ */
const PHONE = "(725) 321-5492";
const PHONE_RAW = "+17253215492";

const SERVICES = [
  { id: "electrician",  title: "Electricians",        blurb: "Panel work, wiring, lighting & emergency repairs.",   img: "assets/electrician.jpg" },
  { id: "handyman",     title: "Handymen",            blurb: "Drywall, mounting, assembly — every small fix.",      img: "assets/handyman.jpg" },
  { id: "garage",       title: "Garage Door",         blurb: "Springs, openers, rollers, and full replacements.",   img: "assets/garage.jpg" },
  { id: "locksmith",    title: "Locksmith",           blurb: "Lockouts, rekeys, smart locks & deadbolt installs.",  img: "assets/locksmith.jpg" },
  { id: "plumber",      title: "Plumbers",            blurb: "Leaks, drains, water heaters, fixture swaps.",        img: "assets/plumber.jpg" },
  { id: "hvac",         title: "AC Duct Cleaning",    blurb: "HVAC tune-ups, filter & duct deep cleans.",            img: "assets/hvac.jpg" },
  { id: "carpet",       title: "Carpet Cleaning",     blurb: "Steam, stain & odor treatment for every room.",        img: "assets/carpet.jpg" },
  { id: "construction", title: "Construction",        blurb: "Remodels, framing, custom builds & finishes.",         img: "assets/construction.jpg" },
];

/* ------------------------------------------------------------------ */
/*  ICONS                                                              */
/* ------------------------------------------------------------------ */
const Icon = {
  phone: (p) => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2"
         strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.13.96.37 1.9.7 2.81a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.91.33 1.85.57 2.81.7A2 2 0 0 1 22 16.92z" />
    </svg>
  ),
  arrow: (p) => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2"
         strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M5 12h14" /><path d="m13 6 6 6-6 6" />
    </svg>
  ),
  check: (p) => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5"
         strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M20 6 9 17l-5-5" />
    </svg>
  ),
  send: (p) => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"
         strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="m22 2-7 20-4-9-9-4Z" /><path d="M22 2 11 13" />
    </svg>
  ),
  star: (p) => (
    <svg viewBox="0 0 24 24" fill="currentColor" {...p}>
      <path d="m12 2 3.09 6.26L22 9.27l-5 4.87L18.18 22 12 18.27 5.82 22 7 14.14l-5-4.87 6.91-1.01L12 2z" />
    </svg>
  ),
  shield: (p) => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"
         strokeLinecap="round" strokeLinejoin="round" {...p}>
      <path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z" />
      <path d="m9 12 2 2 4-4" />
    </svg>
  ),
  dollar: (p) => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"
         strokeLinecap="round" strokeLinejoin="round" {...p}>
      <line x1="12" y1="2" x2="12" y2="22" />
      <path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6" />
    </svg>
  ),
  clock: (p) => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"
         strokeLinecap="round" strokeLinejoin="round" {...p}>
      <circle cx="12" cy="12" r="10" /><polyline points="12 6 12 12 16 14" />
    </svg>
  ),
  menu: (p) => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2"
         strokeLinecap="round" strokeLinejoin="round" {...p}>
      <line x1="3" y1="7" x2="21" y2="7" />
      <line x1="3" y1="12" x2="21" y2="12" />
      <line x1="3" y1="17" x2="21" y2="17" />
    </svg>
  ),
};

/* ------------------------------------------------------------------ */
/*  LOGO                                                               */
/* ------------------------------------------------------------------ */
function Logo({ light = false }) {
  const fg = light ? "#fff" : "#152136";
  const accent = "#F58220";
  return (
    <div className="logo" aria-label="Service4free">
      <svg viewBox="0 0 44 44" width="36" height="36" aria-hidden="true">
        <circle cx="22" cy="22" r="20" fill="none" stroke={accent} strokeWidth="3" strokeDasharray="92 30" strokeLinecap="round" transform="rotate(-30 22 22)" />
        <path d="M27.8 16.5a3.5 3.5 0 0 0-3.5-3.5h-4.6a3.5 3.5 0 0 0-3.5 3.5v.5h-1.4a1 1 0 0 0-.95 1.31l1.6 4.79A4 4 0 0 0 19.3 26h5.4a4 4 0 0 0 3.85-2.9l1.6-4.79A1 1 0 0 0 29.2 17h-1.4v-.5z M19 30.5h6 M22 26v4.5"
              fill="none" stroke={fg} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" />
      </svg>
      <div className="logo-text" style={{ color: fg }}>
        <span className="logo-line1">Service<span style={{ color: accent }}>4</span>free</span>
        <span className="logo-line2">Free service call</span>
      </div>
    </div>
  );
}

/* ------------------------------------------------------------------ */
/*  TOP BAR                                                            */
/* ------------------------------------------------------------------ */
function TopBar({ onCallClick }) {
  const [scrolled, setScrolled] = useState(false);
  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 24);
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);
  return (
    <header className={"topbar" + (scrolled ? " is-scrolled" : "")}>
      <div className="topbar-inner">
        <Logo />
        <nav className="topbar-nav">
          <a href="#services">Services</a>
          <a href="#why">Why us</a>
          <a href="#book">Free service call</a>
          <a href="#pros">Pros</a>
        </nav>
        <a href={`tel:${PHONE_RAW}`} className="btn btn-primary btn-sm" onClick={onCallClick}>
          <Icon.phone width="16" height="16" />
          <span>{PHONE}</span>
        </a>
      </div>
    </header>
  );
}

/* ------------------------------------------------------------------ */
/*  HERO                                                               */
/* ------------------------------------------------------------------ */
function Hero() {
  return (
    <section className="hero">
      <div className="hero-bg" style={{ backgroundImage: "url(assets/hero.jpg)" }} />
      <div className="hero-tint" />
      <div className="hero-grid">
        <div className="hero-copy">
          <div className="eyebrow">
            <span className="eyebrow-dot" />
            <span>Available 24 / 7 · Nationwide network</span>
          </div>
          <h1>
            Free service call.<br />
            <span className="hi-light">One call away.</span>
          </h1>
          <p className="hero-sub">
            We connect homeowners with vetted local pros — electricians, plumbers,
            HVAC techs, locksmiths and more. <strong>No service fees. No hidden costs.</strong>
            We bill the trade, never you.
          </p>
          <div className="hero-cta">
            <a href={`tel:${PHONE_RAW}`} className="btn btn-primary btn-lg">
              <Icon.phone width="18" height="18" />
              <span>Call {PHONE}</span>
            </a>
            <a href="#book" className="btn btn-ghost btn-lg">
              Book online <Icon.arrow width="18" height="18" />
            </a>
          </div>
          <div className="hero-trust">
            <div className="trust-stat">
              <div className="trust-num">12<span>+</span></div>
              <div className="trust-label">Years matching pros<br />to homeowners</div>
            </div>
            <div className="trust-divider" />
            <div className="trust-stat">
              <div className="trust-stars">
                <Icon.star width="14" height="14" /><Icon.star width="14" height="14" />
                <Icon.star width="14" height="14" /><Icon.star width="14" height="14" />
                <Icon.star width="14" height="14" />
              </div>
              <div className="trust-label"><strong>4.9 / 5</strong> · 2,300+ jobs<br />completed this year</div>
            </div>
          </div>
        </div>

        <aside className="hero-card">
          <div className="hero-card-pill">
            <span className="pulse" /> Operator online
          </div>
          <h3>Tell us what broke.<br />We dispatch a pro.</h3>
          <ol className="hero-steps">
            <li><b>Call or submit</b> a 30-second request</li>
            <li><b>We match</b> a local, insured specialist</li>
            <li><b>They show up</b> — usually same day</li>
          </ol>
          <a href="#book" className="btn btn-dark btn-block">
            Get my free service call <Icon.arrow width="18" height="18" />
          </a>
          <p className="hero-card-foot">Average wait time today: <b>38 min</b></p>
        </aside>
      </div>
    </section>
  );
}

/* ------------------------------------------------------------------ */
/*  TICKER                                                             */
/* ------------------------------------------------------------------ */
function Ticker() {
  const items = [
    "FULLY VETTED PROS",
    "INSURED & LICENSED",
    "NO SERVICE FEES",
    "AVAILABLE 24/7",
    "SAME-DAY DISPATCH",
    "NATIONWIDE",
  ];
  const row = [...items, ...items];
  return (
    <div className="ticker">
      <div className="ticker-track">
        {row.map((t, i) => (
          <span key={i} className="ticker-item">
            {t}<i className="ticker-dot" />
          </span>
        ))}
      </div>
    </div>
  );
}

/* ------------------------------------------------------------------ */
/*  SERVICES                                                           */
/* ------------------------------------------------------------------ */
function Services() {
  const [active, setActive] = useState(null);
  return (
    <section id="services" className="section services">
      <div className="section-head">
        <span className="kicker">Our services</span>
        <h2>Eight trades. <span className="hi-light">One number.</span></h2>
        <p>Whatever broke at home, we have a specialist for it. Pick what you need or just call us — we'll route you.</p>
      </div>
      <div className="services-grid">
        {SERVICES.map((s, i) => (
          <article
            key={s.id}
            className={"svc-card" + (active === s.id ? " is-active" : "")}
            onMouseEnter={() => setActive(s.id)}
            onMouseLeave={() => setActive(null)}
            style={{ "--i": i }}
          >
            <div className="svc-img" style={{ backgroundImage: `url(${s.img})` }}>
              <span className="svc-num">0{i + 1}</span>
            </div>
            <div className="svc-body">
              <h3>{s.title}</h3>
              <p>{s.blurb}</p>
              <a href={`tel:${PHONE_RAW}`} className="svc-link">
                Call a pro <Icon.arrow width="14" height="14" />
              </a>
            </div>
          </article>
        ))}
      </div>
    </section>
  );
}

/* ------------------------------------------------------------------ */
/*  WHY US                                                             */
/* ------------------------------------------------------------------ */
function Why() {
  const items = [
    { icon: <Icon.dollar width="22" height="22" />,  title: "Free service call",  body: "The technician's visit to diagnose your problem is on us — you only pay for the actual work if you decide to move forward." },
    { icon: <Icon.shield width="22" height="22" />,  title: "Vetted & insured",     body: "Every contractor on our network is background-checked, licensed, and carries liability insurance." },
    { icon: <Icon.clock width="22" height="22" />,   title: "Same-day dispatch",    body: "Most calls are matched within the hour. Emergency? Press 1 — we'll route you to an on-call tech." },
  ];
  return (
    <section id="why" className="section why">
      <div className="section-head left">
        <span className="kicker">Why homeowners call us</span>
        <h2>The only call you'll need to make.</h2>
      </div>
      <div className="why-grid">
        {items.map((it, i) => (
          <div className="why-card" key={i}>
            <div className="why-icon">{it.icon}</div>
            <h3>{it.title}</h3>
            <p>{it.body}</p>
          </div>
        ))}
      </div>
    </section>
  );
}

/* ------------------------------------------------------------------ */
/*  BOOK FORM                                                          */
/* ------------------------------------------------------------------ */
function BookForm() {
  const [v, setV] = useState({ name: "", phone: "", address: "", zip: "", service: "" });
  const [errors, setErrors] = useState({});
  const [sent, setSent] = useState(false);

  const set = (k) => (e) => setV((p) => ({ ...p, [k]: e.target.value }));

  const submit = (e) => {
    e.preventDefault();
    const next = {};
    if (v.name.trim().length < 2) next.name = "Please enter your name";
    if (!/^[\d\s()+-]{7,}$/.test(v.phone)) next.phone = "Enter a valid phone";
    if (v.address.trim().length < 4) next.address = "Address required";
    if (!/^\d{4,6}$/.test(v.zip)) next.zip = "Valid zip code";
    if (!v.service) next.service = "Pick a service";
    setErrors(next);
    if (Object.keys(next).length === 0) setSent(true);
  };

  return (
    <section id="book" className="section book">
      <div className="book-grid">
        <div className="book-copy">
          <span className="kicker light">Homeowners</span>
          <h2>Interested in a free service call?<br /><span className="hi-light">Leave your details.</span></h2>
          <p>A real human will call you back within 15 minutes during business hours — and dispatch a pro to your address.</p>
          <ul className="book-bullets">
            <li><Icon.check width="16" height="16" /> Free service call — no visit fee</li>
            <li><Icon.check width="16" height="16" /> Licensed & insured techs only</li>
            <li><Icon.check width="16" height="16" /> Same-day & after-hours available</li>
          </ul>
          <div className="book-call">
            <span>Or skip the form —</span>
            <a href={`tel:${PHONE_RAW}`}><Icon.phone width="16" height="16" /> {PHONE}</a>
          </div>
        </div>

        <form className={"book-form" + (sent ? " is-sent" : "")} onSubmit={submit} noValidate>
          {!sent ? (
            <>
              <div className="form-row">
                <Field label="Full name"          v={v.name}    onChange={set("name")}    err={errors.name} />
                <Field label="Phone"   type="tel" v={v.phone}   onChange={set("phone")}   err={errors.phone} />
              </div>
              <Field label="Residential address" v={v.address} onChange={set("address")} err={errors.address} />
              <div className="form-row">
                <Field label="Zip code" v={v.zip} onChange={set("zip")} err={errors.zip} />
                <div className={"field" + (errors.service ? " has-err" : "")}>
                  <label>Service needed</label>
                  <select value={v.service} onChange={set("service")}>
                    <option value="">Choose a service…</option>
                    {SERVICES.map((s) => (<option key={s.id} value={s.id}>{s.title}</option>))}
                    <option value="other">Something else</option>
                  </select>
                  {errors.service && <small>{errors.service}</small>}
                </div>
              </div>
              <button type="submit" className="btn btn-primary btn-lg btn-block">
                <Icon.send width="18" height="18" /> Submit & get matched
              </button>
              <p className="form-foot">By submitting you agree to our terms. We'll never share your info.</p>
            </>
          ) : (
            <div className="sent">
              <div className="sent-check"><Icon.check width="36" height="36" /></div>
              <h3>Got it, {v.name.split(" ")[0] || "friend"}!</h3>
              <p>We'll call you at <b>{v.phone}</b> within 15 minutes to confirm and dispatch your <b>{SERVICES.find((s)=>s.id===v.service)?.title || "service"}</b> pro.</p>
              <button className="btn btn-ghost btn-sm" onClick={() => { setSent(false); setV({ name:"", phone:"", address:"", zip:"", service:"" }); }}>
                Submit another
              </button>
            </div>
          )}
        </form>
      </div>
    </section>
  );
}

function Field({ label, type = "text", v, onChange, err }) {
  return (
    <div className={"field" + (err ? " has-err" : "")}>
      <label>{label}</label>
      <input type={type} value={v} onChange={onChange} placeholder=" " />
      {err && <small>{err}</small>}
    </div>
  );
}

/* ------------------------------------------------------------------ */
/*  PRO FORM                                                           */
/* ------------------------------------------------------------------ */
function ProsForm() {
  const [v, setV] = useState({ name: "", phone: "", trade: "", message: "" });
  const [sent, setSent] = useState(false);
  const set = (k) => (e) => setV((p) => ({ ...p, [k]: e.target.value }));
  const submit = (e) => { e.preventDefault(); if (v.name && v.phone) setSent(true); };

  return (
    <section id="pros" className="section pros">
      <div className="pros-card">
        <div className="pros-left">
          <span className="kicker">For experts</span>
          <h2>Are you a pro?<br /><span className="hi-light">Join the network.</span></h2>
          <p>We send 2,300+ pre-qualified jobs every month to our partners. No bidding, no lead farms — just real work in your zip code.</p>
          <ul className="pro-bullets">
            <li><Icon.check width="16" height="16" /> Exclusive jobs in your area</li>
            <li><Icon.check width="16" height="16" /> Paid weekly, no chasing</li>
            <li><Icon.check width="16" height="16" /> You set your own schedule</li>
          </ul>
        </div>
        <form className={"pros-form" + (sent ? " is-sent" : "")} onSubmit={submit}>
          {!sent ? (
            <>
              <h3>Apply to join</h3>
              <Field label="Full name" v={v.name} onChange={set("name")} />
              <Field label="Phone" type="tel" v={v.phone} onChange={set("phone")} />
              <div className="field">
                <label>Trade</label>
                <select value={v.trade} onChange={set("trade")}>
                  <option value="">Choose your trade…</option>
                  {SERVICES.map((s) => (<option key={s.id} value={s.id}>{s.title}</option>))}
                </select>
              </div>
              <div className="field">
                <label>Message (optional)</label>
                <textarea rows="3" value={v.message} onChange={set("message")} placeholder=" " />
              </div>
              <button type="submit" className="btn btn-primary btn-block">
                Submit application <Icon.send width="16" height="16" />
              </button>
            </>
          ) : (
            <div className="sent">
              <div className="sent-check"><Icon.check width="36" height="36" /></div>
              <h3>Thanks, {v.name.split(" ")[0]}!</h3>
              <p>Our partner team will reach out at <b>{v.phone}</b> within 24 hours.</p>
            </div>
          )}
        </form>
      </div>
    </section>
  );
}

/* ------------------------------------------------------------------ */
/*  FOOTER                                                             */
/* ------------------------------------------------------------------ */
function Footer() {
  return (
    <footer className="footer">
      <div className="footer-top">
        <div className="footer-brand">
          <Logo light />
          <p>Free service call dispatch, nationwide. Available 24 / 7.</p>
        </div>
        <div className="footer-cols">
          <div>
            <h4>Services</h4>
            <ul>{SERVICES.slice(0,4).map((s) => <li key={s.id}><a href="#services">{s.title}</a></li>)}</ul>
          </div>
          <div>
            <h4>More</h4>
            <ul>{SERVICES.slice(4).map((s) => <li key={s.id}><a href="#services">{s.title}</a></li>)}</ul>
          </div>
          <div>
            <h4>Company</h4>
            <ul>
              <li><a href="#why">About</a></li>
              <li><a href="#pros">Become a pro</a></li>
              <li><a href="#book">Book service</a></li>
            </ul>
          </div>
          <div>
            <h4>Contact</h4>
            <ul>
              <li><a href={`tel:${PHONE_RAW}`}>{PHONE}</a></li>
              <li><a href="mailto:hello@service4free.com">hello@service4free.com</a></li>
              <li>Mon–Sun · 24 / 7</li>
            </ul>
          </div>
        </div>
      </div>
      <div className="footer-bottom">
        <span>© 2026 Service4free. All rights reserved.</span>
        <span>Licensed referral service. Pros are independent contractors.</span>
      </div>
    </footer>
  );
}

/* ------------------------------------------------------------------ */
/*  STICKY MOBILE CTA                                                  */
/* ------------------------------------------------------------------ */
function StickyCall() {
  return (
    <a href={`tel:${PHONE_RAW}`} className="sticky-call">
      <Icon.phone width="18" height="18" /> Call {PHONE}
    </a>
  );
}

/* ------------------------------------------------------------------ */
/*  APP                                                                */
/* ------------------------------------------------------------------ */
function App() {
  return (
    <>
      <TopBar />
      <Hero />
      <Ticker />
      <Services />
      <Why />
      <BookForm />
      <ProsForm />
      <Footer />
      <StickyCall />
    </>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<App />);
