/* global React, Icon, Footer, SiteHeader */
// Generic feature page, driven by window.FEATURE_PAGES[<id>] (feature-pages-data.js).
// Each page = hero, storyboard (steps with a mini product panel), feature grid,
// "why it's different", FAQ, related pages, CTA. Pages are revised one by one later.

const TONE = {
  neutral: "bg-stone-100 text-stone-700",
  orange:  "bg-orange-100 text-orange-800",
  green:   "bg-green-100 text-green-800",
  red:     "bg-red-100 text-red-700",
  dark:    "bg-stone-900 text-white",
};

function Pill({ tone = "neutral", children }) {
  return <span className={"text-[11px] font-semibold px-2 py-0.5 rounded-full whitespace-nowrap " + (TONE[tone] || TONE.neutral)}>{children}</span>;
}

// A small product-window mock: title bar, rows (label / value pill), or a code block.
function MiniPanel({ panel }) {
  return (
    <div className="rounded-2xl border border-stone-200 bg-white shadow-sm overflow-hidden">
      <div className="flex items-center gap-1.5 px-4 py-2.5 border-b border-stone-100 bg-stone-50">
        <span className="w-2.5 h-2.5 rounded-full bg-stone-300" /><span className="w-2.5 h-2.5 rounded-full bg-stone-300" /><span className="w-2.5 h-2.5 rounded-full bg-stone-300" />
        <span className="ml-3 text-xs font-medium text-stone-500 truncate">{panel.title}</span>
      </div>
      {panel.code ? (
        <pre className="p-4 text-[12.5px] leading-relaxed text-stone-800 font-mono overflow-x-auto whitespace-pre">{panel.code}</pre>
      ) : (
        <ul className="divide-y divide-stone-100">
          {panel.rows.map((r, i) => (
            <li key={i} className="flex items-center justify-between gap-3 px-4 py-3">
              <div className="min-w-0">
                <div className="text-[14px] font-medium text-stone-900 truncate">{r.a}</div>
                {r.sub && <div className="text-xs text-stone-500 mt-0.5 truncate">{r.sub}</div>}
              </div>
              {r.b && <Pill tone={r.tone}>{r.b}</Pill>}
            </li>
          ))}
        </ul>
      )}
    </div>
  );
}

function FpHero({ p }) {
  return (
    <section className="pt-16 lg:pt-20 pb-14">
      <div className="max-w-7xl mx-auto px-6 grid lg:grid-cols-12 gap-10 items-center">
        <div className="lg:col-span-7">
          <span className="text-[11px] uppercase tracking-[0.18em] font-semibold text-orange-700">{p.eyebrow}</span>
          <h1 className="font-bold tracking-tight text-stone-900 mt-3" style={{ fontSize: "clamp(36px, 5vw, 62px)", lineHeight: 1.03, letterSpacing: "-0.025em" }}>{p.h1}</h1>
          <p className="text-[18px] text-stone-700 leading-relaxed mt-5 max-w-2xl">{p.sub}</p>
          <div className="flex flex-wrap gap-3 mt-8">
            <a href="contact.html" className="inline-flex items-center gap-1.5 text-sm font-semibold px-5 py-3 rounded-lg bg-stone-900 text-white hover:bg-stone-800 transition-colors">Start free trial <Icon name="arrow" size={14} /></a>
            <a href="pricing.html" className="inline-flex items-center text-sm font-semibold px-5 py-3 rounded-lg border border-stone-300 text-stone-800 hover:bg-stone-100 transition-colors">See pricing</a>
          </div>
        </div>
        <div className="lg:col-span-5"><MiniPanel panel={p.heroPanel} /></div>
      </div>
    </section>
  );
}

function Storyboard({ p }) {
  return (
    <section className="py-16 lg:py-20 bg-stone-100/60 border-y border-stone-200">
      <div className="max-w-6xl mx-auto px-6">
        <h2 className="font-bold text-stone-900 tracking-tight" style={{ fontSize: "clamp(26px, 3.2vw, 40px)", letterSpacing: "-0.02em" }}>{p.storyTitle}</h2>
        <div className="mt-12 space-y-16">
          {p.steps.map((s, i) => (
            <div key={i} className="grid lg:grid-cols-12 gap-8 lg:gap-12 items-center">
              <div className={"lg:col-span-5 " + (i % 2 ? "lg:order-2" : "")}>
                <div className="w-9 h-9 rounded-full bg-orange-500 text-white font-bold flex items-center justify-center text-sm">{i + 1}</div>
                <h3 className="text-2xl font-bold text-stone-900 tracking-tight mt-4" style={{ letterSpacing: "-0.015em" }}>{s.title}</h3>
                <p className="text-[16px] text-stone-700 leading-relaxed mt-3">{s.body}</p>
              </div>
              <div className={"lg:col-span-7 " + (i % 2 ? "lg:order-1" : "")}><MiniPanel panel={s.panel} /></div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// Optional: strip naming the areas of the product this page covers.
function TabsStrip({ p }) {
  if (!p.tabs) return null;
  return (
    <section className="pb-12">
      <div className="max-w-7xl mx-auto px-6">
        <div className="text-xs uppercase tracking-wider font-semibold text-stone-500 mb-3">{p.tabsLabel || "Inside"}</div>
        <div className="flex flex-wrap gap-2">
          {p.tabs.map((t) => <span key={t} className="text-sm font-medium px-3.5 py-1.5 rounded-full border border-stone-300 bg-white text-stone-800">{t}</span>)}
        </div>
      </div>
    </section>
  );
}

// Optional: the full inventory, grouped by area. Each group = icon, title, intro, items[{title, body}].
function Groups({ p }) {
  if (!p.groups) return null;
  return (
    <section className="py-16 lg:py-20">
      <div className="max-w-6xl mx-auto px-6">
        <h2 className="font-bold text-stone-900 tracking-tight" style={{ fontSize: "clamp(26px, 3.2vw, 40px)", letterSpacing: "-0.02em" }}>{p.groupsTitle || "Everything it does"}</h2>
        {p.groupsIntro && <p className="text-[17px] text-stone-700 leading-relaxed mt-3 max-w-3xl">{p.groupsIntro}</p>}
        <div className="mt-12 space-y-14">
          {p.groups.map((g, gi) => (
            <div key={gi} className="grid lg:grid-cols-12 gap-8">
              <div className="lg:col-span-4">
                <div className="w-10 h-10 rounded-lg bg-orange-50 text-orange-600 ring-1 ring-orange-100 flex items-center justify-center"><Icon name={g.icon} size={18} /></div>
                <h3 className="text-xl font-bold text-stone-900 tracking-tight mt-4">{g.title}</h3>
                <p className="text-[15px] text-stone-700 leading-relaxed mt-2">{g.intro}</p>
              </div>
              <div className="lg:col-span-8 grid sm:grid-cols-2 gap-x-8 gap-y-5">
                {g.items.map((it, ii) => (
                  <div key={ii} className="flex gap-3">
                    <span className="mt-1.5 w-1.5 h-1.5 rounded-full bg-orange-500 flex-none" />
                    <div>
                      <div className="font-semibold text-stone-900 text-[15px]">{it.title}</div>
                      <p className="text-[14px] text-stone-700 leading-relaxed mt-0.5">{it.body}</p>
                    </div>
                  </div>
                ))}
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

function FeatureGrid({ p }) {
  if (!p.features) return null;
  return (
    <section className="py-16 lg:py-20">
      <div className="max-w-6xl mx-auto px-6">
        <h2 className="font-bold text-stone-900 tracking-tight" style={{ fontSize: "clamp(26px, 3.2vw, 40px)", letterSpacing: "-0.02em" }}>What's inside</h2>
        <div className="grid sm:grid-cols-2 lg:grid-cols-3 gap-5 mt-10">
          {p.features.map((f, i) => (
            <div key={i} className="rounded-2xl border border-stone-200 bg-white p-6">
              <div className="w-10 h-10 rounded-lg bg-orange-50 text-orange-600 ring-1 ring-orange-100 flex items-center justify-center"><Icon name={f.icon} size={18} /></div>
              <h3 className="font-semibold text-stone-900 mt-4">{f.title}</h3>
              <p className="text-[14.5px] text-stone-700 leading-relaxed mt-1.5">{f.body}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

function Different({ p }) {
  return (
    <section className="py-16 bg-stone-900 text-white">
      <div className="max-w-6xl mx-auto px-6">
        <h2 className="font-bold tracking-tight" style={{ fontSize: "clamp(26px, 3.2vw, 38px)", letterSpacing: "-0.02em" }}>{p.differentTitle}</h2>
        <div className="grid md:grid-cols-3 gap-8 mt-10">
          {p.different.map((d, i) => (
            <div key={i}>
              <div className="text-orange-400 font-bold text-sm">0{i + 1}</div>
              <h3 className="font-semibold text-lg mt-2">{d.title}</h3>
              <p className="text-stone-300 text-[15px] leading-relaxed mt-2">{d.body}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

function Faq({ p }) {
  return (
    <section className="py-16 lg:py-20">
      <div className="max-w-3xl mx-auto px-6">
        <h2 className="font-bold text-stone-900 tracking-tight" style={{ fontSize: "clamp(26px, 3.2vw, 38px)", letterSpacing: "-0.02em" }}>Questions</h2>
        <div className="mt-8 divide-y divide-stone-200 border-y border-stone-200">
          {p.faq.map((f, i) => (
            <details key={i} className="group py-5">
              <summary className="flex items-center justify-between gap-4 cursor-pointer list-none font-semibold text-stone-900">
                {f.q}<Icon name="plus" size={16} className="flex-none text-stone-500 group-open:rotate-45 transition-transform" />
              </summary>
              <p className="text-[15px] text-stone-700 leading-relaxed mt-3">{f.a}</p>
            </details>
          ))}
        </div>
      </div>
    </section>
  );
}

function Related({ p }) {
  const all = window.FEATURE_PAGES || {};
  const items = (p.related || []).map((id) => all[id]).filter(Boolean);
  return (
    <section className="pb-8">
      <div className="max-w-6xl mx-auto px-6">
        <div className="text-xs uppercase tracking-wider font-semibold text-stone-500 mb-4">Works with</div>
        <div className="grid sm:grid-cols-3 gap-4">
          {items.map((r) => (
            <a key={r.slug} href={r.slug + ".html"} className="block rounded-2xl border border-stone-200 bg-white p-5 hover:border-stone-400 transition-colors">
              <div className="font-semibold text-stone-900">{r.name}</div>
              <div className="text-sm text-stone-600 mt-1 leading-snug">{r.blurb}</div>
            </a>
          ))}
        </div>
      </div>
    </section>
  );
}

function FpCta({ p }) {
  return (
    <section className="py-16">
      <div className="max-w-4xl mx-auto px-6 text-center">
        <h2 className="font-bold text-stone-900 tracking-tight" style={{ fontSize: "clamp(26px, 3.4vw, 40px)", letterSpacing: "-0.02em" }}>{p.ctaTitle}</h2>
        <p className="text-stone-700 text-[17px] mt-3">Every feature is in every plan. No per-module fees. Questions? Call <a href="tel:+441254948887" className="font-semibold underline decoration-orange-300 underline-offset-4">+44 1254 948887</a>.</p>
        <div className="flex flex-wrap justify-center gap-3 mt-7">
          <a href="contact.html" className="inline-flex items-center gap-1.5 text-sm font-semibold px-5 py-3 rounded-lg bg-stone-900 text-white hover:bg-stone-800 transition-colors">Start free trial <Icon name="arrow" size={14} /></a>
          <a href="updates.html" className="inline-flex items-center text-sm font-semibold px-5 py-3 rounded-lg border border-stone-300 text-stone-800 hover:bg-stone-100 transition-colors">See what's new</a>
        </div>
      </div>
    </section>
  );
}

function FeaturePage({ id }) {
  const p = (window.FEATURE_PAGES || {})[id];
  if (!p) return <div className="p-10">Page not found.</div>;
  return (
    <>
      <SiteHeader active={p.slug} cta={{ label: "Start free trial", href: "contact.html" }}
        breadcrumb={[{ label: "Features", href: "features.html" }, { label: p.name }]} />
      <main>
        <FpHero p={p} />
        <TabsStrip p={p} />
        <Storyboard p={p} />
        <FeatureGrid p={p} />
        <Groups p={p} />
        <Different p={p} />
        <Faq p={p} />
        <Related p={p} />
        <FpCta p={p} />
      </main>
      <Footer />
    </>
  );
}

window.FeaturePage = FeaturePage;
