/* sections.jsx — Nav, Problem, HowItWorks, Features, Demo, Pricing, Testimonials, FAQ, FinalCTA, Footer */

/* ── NAV ──────────────────────────────────────────────────────────────── */
function Nav() {
  const [scrolled, setScrolled] = useState(false);
  const [open, setOpen] = useState(false);
  useEffect(() => {
    const on = () => setScrolled(window.scrollY > 16);
    on();
    window.addEventListener("scroll", on, {passive: true});
    return () => window.removeEventListener("scroll", on);
  }, []);
  const links = [
    ["Features", "features"],
    ["How it works", "how"],
    ["FAQ", "faq"],
  ];
  return (
    <header
      className={
        "fixed inset-x-0 top-0 z-50 transition-all duration-300 " +
        (scrolled
          ? "border-b border-brand-700/10 bg-cream/80 backdrop-blur-xl"
          : "border-b border-transparent bg-transparent")
      }
    >
      <nav className="mx-auto flex max-w-7xl items-center justify-between px-5 py-3.5 sm:px-8">
        <a
          href="#top"
          className="focus-ring rounded-xl"
          aria-label="My PA home"
        >
          <Logo />
        </a>
        <div className="hidden items-center gap-1 lg:flex">
          {links.map(([label, id]) => (
            <button
              key={id}
              onClick={() => scrollToId(id)}
              className="focus-ring rounded-full px-3.5 py-2 text-[15px] font-medium text-brand-900/75 transition-colors hover:text-brand-900"
            >
              {label}
            </button>
          ))}
          <a
            href="/pricing"
            className="focus-ring rounded-full px-3.5 py-2 text-[15px] font-medium text-brand-900/75 transition-colors hover:text-brand-900"
          >
            Pricing
          </a>
        </div>
        <div className="flex items-center gap-2">
          <PlayPrimary className="hidden text-sm sm:inline-flex">
            Get the app
          </PlayPrimary>
          <button
            onClick={() => setOpen((o) => !o)}
            aria-label="Toggle menu"
            aria-expanded={open}
            className="focus-ring flex h-10 w-10 items-center justify-center rounded-full border border-brand-700/15 bg-white/60 text-brand-800 lg:hidden"
          >
            {open ? <Icons.X size={18} /> : <Icons.Menu size={18} />}
          </button>
        </div>
      </nav>
      {open && (
        <div className="border-t border-brand-700/10 bg-cream/95 px-5 py-5 backdrop-blur-xl lg:hidden">
          <div className="flex flex-col gap-1">
            {links.map(([label, id]) => (
              <button
                key={id}
                onClick={() => {
                  setOpen(false);
                  scrollToId(id);
                }}
                className="focus-ring flex items-center justify-between rounded-2xl px-4 py-3.5 text-left text-[16px] font-semibold text-brand-900 transition-colors hover:bg-brand-700/6"
              >
                {label}
                <Icons.ArrowRight size={16} className="text-brand-600/50" />
              </button>
            ))}
            <a
              href="/pricing"
              className="focus-ring flex items-center justify-between rounded-2xl px-4 py-3.5 text-left text-[16px] font-semibold text-brand-900 transition-colors hover:bg-brand-700/6"
            >
              Pricing
              <Icons.ArrowRight size={16} className="text-brand-600/50" />
            </a>
            <div className="mt-3">
              <PlayBtn tone="dark" className="w-full justify-center" />
            </div>
          </div>
        </div>
      )}
    </header>
  );
}

/* ── Section heading helper ───────────────────────────────────────────── */
function Heading({eyebrow, title, sub, onDark, center = true}) {
  return (
    <div className={"mx-auto max-w-2xl " + (center ? "text-center" : "")}>
      <div data-reveal className={center ? "flex justify-center" : ""}>
        <Eyebrow onDark={onDark}>{eyebrow}</Eyebrow>
      </div>
      <h2
        data-reveal
        data-delay="60"
        className={
          "display-tight mt-5 font-display text-[30px] font-extrabold sm:text-[40px] " +
          (onDark ? "text-white" : "text-brand-900")
        }
      >
        {title}
      </h2>
      {sub && (
        <p
          data-reveal
          data-delay="120"
          className={
            "mt-4 text-[17px] leading-relaxed " +
            (onDark ? "text-white/75" : "text-brand-800/70")
          }
        >
          {sub}
        </p>
      )}
    </div>
  );
}

/* ── PROBLEM ──────────────────────────────────────────────────────────── */
function Problem() {
  const cards = [
    {
      icon: Icons.MessageCircle,
      t: "“How much is this?” — fifty times a day",
      d: "The same questions about price, sizes, opening hours and delivery, over and over, eating your whole afternoon.",
    },
    {
      icon: Icons.Moon,
      t: "Orders slip away while you sleep",
      d: "A customer messages at 1am ready to buy. By morning they've gone to the shop that replied first.",
    },
    {
      icon: Icons.Receipt,
      t: "Receipts and records live in screenshots",
      d: "Payments confirmed in chats, totals on scraps of paper. When it's time to count, nothing adds up.",
    },
  ];
  return (
    <section className="bg-cream py-20 sm:py-28">
      <div className="mx-auto max-w-7xl px-5 sm:px-8">
        <Heading
          eyebrow="The 2am problem"
          title="Running a business on social media shouldn't mean answering messages at midnight."
        />
        <div className="mt-14 grid gap-5 sm:grid-cols-2 lg:grid-cols-3">
          {cards.map((c, i) => (
            <div
              key={i}
              data-reveal
              data-delay={i * 90}
              className="group rounded-4xl border border-brand-700/8 bg-white p-7 shadow-soft transition-all duration-200 hover:-translate-y-1 hover:shadow-lift"
            >
              <span className="flex h-12 w-12 items-center justify-center rounded-2xl bg-brand-50 text-brand-600 transition-colors group-hover:bg-amber-500/15 group-hover:text-amber-600">
                <c.icon size={24} />
              </span>
              <h3 className="mt-5 font-display text-[20px] font-bold text-brand-900">
                {c.t}
              </h3>
              <p className="mt-2.5 text-[15.5px] leading-relaxed text-brand-800/70">
                {c.d}
              </p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ── HOW IT WORKS ─────────────────────────────────────────────────────── */
function HowItWorks() {
  const steps = [
    {
      n: "01",
      icon: Icons.Link,
      t: "Connect your channels",
      d: "Verify WhatsApp in 60 seconds, then add Instagram and Messenger from the same settings screen. No new app for your customers.",
      graphic: StepConnect,
    },
    {
      n: "02",
      icon: Icons.Upload,
      t: "Upload your menu, prices, FAQs",
      d: "Drag in a PDF price list or just paste your text. My PA reads it and learns how your shop works.",
      graphic: StepUpload,
    },
    {
      n: "03",
      icon: Icons.Sparkles,
      t: "Your assistant takes over",
      d: "It replies in seconds, sends receipts, and logs every order — and flags you only when it truly needs you.",
      graphic: StepAssistant,
    },
  ];
  return (
    <section
      id="how"
      className="relative scroll-mt-20 overflow-hidden bg-brand-700 py-20 text-white sm:py-28"
    >
      <div
        className="pointer-events-none absolute -right-32 top-10 h-[420px] w-[420px] rounded-full bg-brand-500/30 blur-3xl"
        aria-hidden="true"
      />
      <div className="relative mx-auto max-w-7xl px-5 sm:px-8">
        <Heading
          onDark
          eyebrow="Up and running in minutes"
          title="Three steps. No tech skills. No new app to learn."
        />
        <div className="relative mt-16 grid gap-8 lg:grid-cols-3">
          <div
            className="pointer-events-none absolute left-0 right-0 top-[34px] hidden h-px bg-gradient-to-r from-transparent via-white/25 to-transparent lg:block"
            aria-hidden="true"
          />
          {steps.map((s, i) => (
            <div key={i} data-reveal data-delay={i * 110} className="relative">
              <div className="flex items-center gap-4">
                <span className="relative z-10 flex h-[68px] w-[68px] flex-none items-center justify-center rounded-2xl bg-amber-500 text-brand-900 shadow-amber">
                  <s.icon size={28} />
                </span>
                <span className="font-display text-[44px] font-extrabold text-white/15">
                  {s.n}
                </span>
              </div>
              <h3 className="mt-6 font-display text-[22px] font-bold">{s.t}</h3>
              <p className="mt-2.5 text-[15.5px] leading-relaxed text-white/75">
                {s.d}
              </p>
              <div className="mt-6 overflow-hidden rounded-3xl bg-white/5 p-2 ring-1 ring-white/10">
                <div className="h-44 overflow-hidden rounded-2xl bg-brand-800/60">
                  <s.graphic />
                </div>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ── FEATURES ─────────────────────────────────────────────────────────── */
function Features() {
  const feats = [
    {
      icon: Icons.MessageCircle,
      t: "Auto-reply on every channel",
      d: "Answers customers in seconds across WhatsApp, Instagram and Messenger — in English or Pidgin, just how they wrote to you.",
      tag: "Pidgin ready",
    },
    {
      icon: Icons.Receipt,
      t: "Receipts with VAT",
      d: "Generates a clean receipt the moment a sale closes, VAT worked out and ready to send.",
    },
    {
      icon: Icons.Users,
      t: "Customer history & spend",
      d: "Every buyer remembered — what they ordered, how much they've spent, when they last bought.",
    },
    {
      icon: Icons.BookOpen,
      t: "Knowledge from your own docs",
      d: "Upload your menu, price list or FAQs and My PA answers exactly the way your shop would.",
    },
    {
      icon: Icons.BarChart,
      t: "Daily revenue dashboard",
      d: "Wake up to today's sales, messages handled and what needs your attention — all in one glance.",
    },
    {
      icon: Icons.CreditCard,
      t: "Paystack-powered payments",
      d: "Send a secure pay link in the chat and watch the money land, confirmed automatically.",
    },
  ];
  return (
    <section id="features" className="scroll-mt-20 bg-cream py-20 sm:py-28">
      <div className="mx-auto max-w-7xl px-5 sm:px-8">
        <Heading
          eyebrow="Everything in one place"
          title="A whole back office, built into every customer chat."
          sub="No dashboards to babysit. My PA does the repetitive work so you can do the part only you can do."
        />
        <div className="mt-14 grid gap-5 sm:grid-cols-2 lg:grid-cols-3">
          {feats.map((f, i) => (
            <div
              key={i}
              data-reveal
              data-delay={(i % 3) * 80}
              className="group relative rounded-4xl border border-brand-700/8 bg-white p-7 shadow-soft transition-all duration-200 hover:-translate-y-1.5 hover:shadow-lift"
            >
              {f.tag && (
                <span className="absolute right-5 top-5 rounded-full bg-amber-500/15 px-2.5 py-1 text-[11px] font-bold uppercase tracking-wide text-amber-600">
                  {f.tag}
                </span>
              )}
              <span className="flex h-12 w-12 items-center justify-center rounded-2xl bg-brand-600 text-white shadow-soft transition-transform duration-200 group-hover:scale-105">
                <f.icon size={23} />
              </span>
              <h3 className="mt-5 font-display text-[19px] font-bold text-brand-900">
                {f.t}
              </h3>
              <p className="mt-2.5 text-[15px] leading-relaxed text-brand-800/70">
                {f.d}
              </p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ── DEMO / SCREENSHOT ────────────────────────────────────────────────── */
function Demo() {
  const notes = [
    {
      icon: Icons.BadgeCheck,
      t: "AI-handled badge",
      d: "See at a glance which chats My PA closed on its own.",
    },
    {
      icon: Icons.BarChart,
      t: "Today's revenue",
      d: "Live takings, updated as each sale is confirmed.",
    },
    {
      icon: Icons.Pencil,
      t: "Compose new message",
      d: "Jump in yourself anytime — My PA steps aside instantly.",
    },
  ];
  return (
    <section className="bg-cream py-20 sm:py-28">
      <div className="mx-auto max-w-7xl px-5 sm:px-8">
        <Heading
          eyebrow="See it for real"
          title="This is what you wake up to."
          sub="Your inbox, sorted. Your money, counted. Your customers, looked after."
        />
        <div className="mt-14 grid items-center gap-10 sm:gap-12 lg:grid-cols-[0.9fr_1.1fr]">
          <div data-reveal className="relative mx-auto w-full max-w-[320px]">
            <PhoneFrame>
              {/* Was a fixed h-[560px] — on a 360-width phone the content
                  felt cramped and the FAB clipped the tab bar. Letting the
                  height scale to the viewport (with a min) keeps the layout
                  honest on small screens while desktop still gets the full
                  display. */}
              <div className="h-[520px] sm:h-[560px]">
                <AppHome />
              </div>
            </PhoneFrame>
          </div>
          <div className="space-y-4">
            {notes.map((n, i) => (
              <div
                key={i}
                data-reveal
                data-delay={i * 90}
                className="flex items-start gap-4 rounded-3xl border border-brand-700/8 bg-white p-5 shadow-soft"
              >
                <span className="flex h-11 w-11 flex-none items-center justify-center rounded-xl bg-amber-500 text-brand-900">
                  <n.icon size={20} />
                </span>
                <div>
                  <h3 className="font-display text-[18px] font-bold text-brand-900">
                    {n.t}
                  </h3>
                  <p className="mt-1 text-[15px] leading-relaxed text-brand-800/70">
                    {n.d}
                  </p>
                </div>
              </div>
            ))}
            <p className="px-1 pt-1 text-[14px] text-brand-800/55">
              A live preview of your My PA home — your real numbers appear here
              once you connect.
            </p>
          </div>
        </div>
      </div>
    </section>
  );
}

/* ── TESTIMONIALS ─────────────────────────────────────────────────────── */
function Testimonials() {
  const quotes = [
    {
      q: "I sleep through the night now and still wake up to closed sales. My PA replied to 38 customers while I was offline last week.",
      name: "Bisi",
      biz: "Owner — Adire Lagos Boutique",
      initials: "BL",
    },
    {
      q: "Customers used to wait an hour for a price. Now they get it in seconds, in Pidgin, exactly how I'd say it. My orders have doubled.",
      name: "Chidi",
      biz: "Owner — Mama's Kitchen, Enugu",
      initials: "MK",
    },
    {
      q: "The receipts alone are worth it. No more arguing about who paid what — everything is logged and I can see my daily revenue at a glance.",
      name: "Aisha",
      biz: "Founder — Glow Beauty Bar, Abuja",
      initials: "GB",
    },
  ];
  return (
    <section className="bg-cream py-20 sm:py-28">
      <div className="mx-auto max-w-7xl px-5 sm:px-8">
        <Heading
          eyebrow="From real shop owners"
          title="Loved by shops that never stop."
        />
        <div className="mt-14 flex snap-x snap-mandatory gap-5 overflow-x-auto pb-4 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden sm:grid sm:grid-cols-2 sm:overflow-visible lg:grid-cols-3">
          {quotes.map((t, i) => (
            <figure
              key={i}
              data-reveal
              data-delay={i * 90}
              className="flex min-w-[85%] snap-center flex-col rounded-4xl border border-brand-700/8 bg-white p-7 shadow-soft sm:min-w-0"
            >
              <div className="flex gap-0.5 text-amber-500">
                {[0, 1, 2, 3, 4].map((s) => (
                  <Icons.Star key={s} size={16} />
                ))}
              </div>
              <blockquote className="mt-4 flex-1 text-[16.5px] leading-relaxed text-brand-900/85">
                “{t.q}”
              </blockquote>
              <figcaption className="mt-6 flex items-center gap-3">
                <span className="flex h-11 w-11 items-center justify-center rounded-full bg-brand-600 font-display text-[15px] font-bold text-white">
                  {t.initials}
                </span>
                <div>
                  <div className="font-display text-[15px] font-bold text-brand-900">
                    {t.name}
                  </div>
                  <div className="text-[13.5px] text-brand-800/60">{t.biz}</div>
                </div>
              </figcaption>
            </figure>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ── FAQ ──────────────────────────────────────────────────────────────── */
function FAQItem({q, a, open, onToggle}) {
  const ref = useRef(null);
  return (
    <div className="overflow-hidden rounded-3xl border border-brand-700/8 bg-white shadow-soft">
      <button
        onClick={onToggle}
        aria-expanded={open}
        className="focus-ring flex w-full items-center justify-between gap-4 px-6 py-5 text-left"
      >
        <span className="font-display text-[17px] font-bold text-brand-900">
          {q}
        </span>
        <span
          className={
            "flex h-8 w-8 flex-none items-center justify-center rounded-full bg-brand-50 text-brand-600 transition-transform duration-300 " +
            (open ? "rotate-180" : "")
          }
        >
          <Icons.ChevronDown size={18} />
        </span>
      </button>
      <div
        style={{
          maxHeight: open
            ? ref.current
              ? ref.current.scrollHeight + 24
              : 300
            : 0,
        }}
        className="overflow-hidden transition-[max-height] duration-300 ease-in-out"
      >
        <p
          ref={ref}
          className="px-6 pb-5 text-[15.5px] leading-relaxed text-brand-800/75"
        >
          {a}
        </p>
      </div>
    </div>
  );
}

function FAQ() {
  const [open, setOpen] = useState(0);
  const faqs = [
    {
      q: "Will WhatsApp ban my number?",
      a: "No. My PA runs on the official WhatsApp Business API, so your number stays safe and fully approved. You're not breaking any rules — you're using WhatsApp exactly the way it's meant for business.",
    },
    {
      q: "How long does setup take?",
      a: "Under 5 minutes. Verify your business number, upload your price list or paste your details, and your assistant is live. No developer, no waiting.",
    },
    {
      q: "Do you support Pidgin?",
      a: "Yes. My PA understands and replies in Pidgin as naturally as it does in English — it matches the way your customer wrote to you, so every chat feels like your shop.",
    },
    {
      q: "What if my customer asks something the AI doesn't know?",
      a: "It never guesses. When something falls outside what it has learned, My PA flags the chat for you to handle personally and steps aside — so your customer always gets a real, correct answer.",
    },
    {
      q: "Can I cancel anytime?",
      a: "Yes. No contracts, no lock-in. Upgrade, downgrade or cancel whenever you like — your free plan stays active with 500 AI replies a month, forever.",
    },
    {
      q: "How is my customer data stored?",
      a: "Encrypted in transit and at rest, hosted securely, and never sold to anyone. Your customers' information belongs to you and your shop — full stop.",
    },
  ];
  return (
    <section id="faq" className="scroll-mt-20 bg-cream py-20 sm:py-28">
      <div className="mx-auto max-w-3xl px-5 sm:px-8">
        <Heading
          eyebrow="Good questions"
          title="The things shop owners ask us first."
        />
        <div className="mt-12 space-y-3.5">
          {faqs.map((f, i) => (
            <FAQItem
              key={i}
              q={f.q}
              a={f.a}
              open={open === i}
              onToggle={() => setOpen(open === i ? -1 : i)}
            />
          ))}
        </div>
      </div>
    </section>
  );
}

/* ── FINAL CTA ────────────────────────────────────────────────────────── */
function FinalCTA() {
  return (
    <section className="bg-cream px-5 pb-20 sm:px-8 sm:pb-28">
      <div className="relative mx-auto max-w-6xl overflow-hidden rounded-[2.5rem] bg-brand-700 px-7 py-16 text-center text-white shadow-lift sm:px-12 sm:py-20">
        <div
          className="rings pointer-events-none absolute inset-0"
          aria-hidden="true"
        />
        <div
          className="pointer-events-none absolute -left-24 -bottom-24 h-[360px] w-[360px] rounded-full bg-amber-500/20 blur-3xl"
          aria-hidden="true"
        />
        <div className="relative z-10">
          <h2
            data-reveal
            className="display-tight mx-auto max-w-2xl font-display text-[34px] font-extrabold sm:text-[48px]"
          >
            Stop losing sales while you sleep.
          </h2>
          <p
            data-reveal
            data-delay="80"
            className="mx-auto mt-5 max-w-xl text-[18px] leading-relaxed text-white/80 sm:text-[20px]"
          >
            Connect WhatsApp, Instagram or Messenger in 60 seconds. First 50 AI
            replies are always free.
          </p>
          <div
            data-reveal
            data-delay="140"
            className="mt-9 flex flex-col items-center justify-center gap-3 sm:flex-row"
          >
            <PlayBtn tone="onDark" />
            <span className="text-[14px] text-white/65">
              Free to download · No card needed
            </span>
          </div>
        </div>
      </div>
    </section>
  );
}

/* ── FOOTER ───────────────────────────────────────────────────────────── */
function Footer() {
  const cols = [
    {
      h: "Product",
      links: [
        {label: "Features", scroll: "features"},
        {label: "Pricing", href: "/pricing"},
        {label: "How it works", scroll: "how"},
        {label: "WhatsApp setup", href: "/whatsapp-setup"},
      ],
    },
    {
      h: "Legal",
      links: [
        {label: "Privacy Policy", href: "/privacy"},
        {label: "Terms of Service", href: "/terms"},
        {label: "Data Security", href: "/data-security"},
        {label: "Refund Policy", href: "/refunds"},
      ],
    },
    {
      h: "Contact",
      links: [
        {label: "support@mypa.ng", href: "mailto:support@mypa.ng"},
        {
          label: "Chat on WhatsApp",
          href: "https://wa.me/2348000000000",
          external: true,
        },
      ],
    },
  ];
  return (
    <footer className="border-t border-brand-700/10 bg-cream pt-16">
      <div className="mx-auto max-w-7xl px-5 sm:px-8">
        <div className="grid gap-10 lg:grid-cols-[1.4fr_2fr]">
          <div>
            <Logo />
            <p className="mt-4 max-w-xs text-[15px] leading-relaxed text-brand-800/65">
              The WhatsApp assistant that answers your customers, sends receipts
              and runs your shop — day or night.
            </p>
            <div className="mt-6 flex flex-wrap gap-2.5">
              <a
                href="mailto:support@mypa.ng"
                className="focus-ring inline-flex items-center gap-2 whitespace-nowrap rounded-full border border-brand-700/12 bg-white/60 px-3.5 py-2 text-[13.5px] font-semibold text-brand-700 transition-colors hover:bg-brand-600 hover:text-white"
              >
                <Icons.MessageCircle size={15} /> support@mypa.ng
              </a>
              <a
                href="https://wa.me/2348000000000"
                target="_blank"
                rel="noopener"
                className="focus-ring inline-flex items-center gap-2 whitespace-nowrap rounded-full border border-brand-700/12 bg-white/60 px-3.5 py-2 text-[13.5px] font-semibold text-brand-700 transition-colors hover:bg-whatsapp hover:text-white hover:border-whatsapp"
              >
                <Icons.Send size={15} /> WhatsApp us
              </a>
            </div>
          </div>
          <div className="grid grid-cols-2 gap-8 sm:grid-cols-3">
            {cols.map((c, i) => (
              <div key={i}>
                <h4 className="font-display text-[14px] font-bold uppercase tracking-wide text-brand-900">
                  {c.h}
                </h4>
                <ul className="mt-4 space-y-3">
                  {c.links.map((l, j) => (
                    <li key={j}>
                      {l.scroll ? (
                        <button
                          onClick={() => scrollToId(l.scroll)}
                          className="focus-ring rounded text-left text-[14.5px] text-brand-800/65 transition-colors hover:text-brand-700"
                        >
                          {l.label}
                        </button>
                      ) : (
                        <a
                          href={l.href}
                          {...(l.external
                            ? {target: "_blank", rel: "noopener"}
                            : {})}
                          className="focus-ring rounded text-[14.5px] text-brand-800/65 transition-colors hover:text-brand-700"
                        >
                          {l.label}
                        </a>
                      )}
                    </li>
                  ))}
                </ul>
              </div>
            ))}
          </div>
        </div>
        <div className="mt-14 flex flex-col items-center justify-between gap-4 border-t border-brand-700/10 py-7 sm:flex-row">
          <p className="text-[14px] text-brand-800/55">
            © 2026 My PA. All rights reserved.
          </p>
          <p className="flex items-center gap-2 text-[14px] font-medium text-brand-800/65">
            <span className="h-1.5 w-1.5 rounded-full bg-amber-500" /> Built For
            Africa's small businesses
          </p>
        </div>
      </div>
    </footer>
  );
}

Object.assign(window, {
  Nav,
  Problem,
  HowItWorks,
  Features,
  Demo,
  Testimonials,
  FAQ,
  FinalCTA,
  Footer,
});
