// Seckira landing — main app
const { useState: useStateA, useEffect: useEffectA, useRef: useRefA } = React;

const WHATSAPP_NAV = "https://wa.me/60102049494?text=" + encodeURIComponent("Hi Seckira, I'd like to chat about your services.");
const WHATSAPP_HREF = "https://wa.me/60102049494?text=" + encodeURIComponent("Hi Seckira, I'd like to chat about setting up my company.");

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "headlinePreset": "lightning"
} /*EDITMODE-END*/;

const HEADLINES = {
  sorted: { l1: "Let's get your", l2: <>paperwork <em>sorted.</em></> },
  lightning: { l1: "Malaysian Business at", l2: <><em>lightning</em> speed.</> },
  easy: { l1: "Start a company,", l2: <>just by <em>chatting.</em></> }
};

// ─── Magnetic hover hook ───────────────────────────────
const useMagnetic = (strength = 0.28, range = 130) => {
  const ref = useRefA();
  useEffectA(() => {
    const el = ref.current;
    if (!el) return;
    let raf = null;
    const handleMove = (e) => {
      if (raf) cancelAnimationFrame(raf);
      raf = requestAnimationFrame(() => {
        const r = el.getBoundingClientRect();
        const cx = r.left + r.width / 2;
        const cy = r.top + r.height / 2;
        const dx = e.clientX - cx;
        const dy = e.clientY - cy;
        const dist = Math.hypot(dx, dy);
        if (dist < range) {
          const pull = 1 - dist / range;
          el.style.transform = `translate(${dx * strength * pull}px, ${dy * strength * pull}px)`;
        } else {
          el.style.transform = '';
        }
      });
    };
    const handleLeave = () => {el.style.transform = '';};
    window.addEventListener('pointermove', handleMove);
    window.addEventListener('pointerleave', handleLeave);
    document.addEventListener('mouseleave', handleLeave);
    return () => {
      window.removeEventListener('pointermove', handleMove);
      window.removeEventListener('pointerleave', handleLeave);
      document.removeEventListener('mouseleave', handleLeave);
      if (raf) cancelAnimationFrame(raf);
    };
  }, [strength, range]);
  return ref;
};

// ─── Scroll-progress hook (for parallax reveal) ────────
// progress: 0 when top of element is below viewport, 1 once it's fully in.
const useScrollProgress = (startOffset = 0, span = 0.85) => {
  const ref = useRefA();
  const [p, setP] = useStateA(0);
  useEffectA(() => {
    const el = ref.current;
    if (!el) return;
    let raf = null;
    const compute = () => {
      raf = null;
      const r = el.getBoundingClientRect();
      const vh = window.innerHeight || 1;
      // progress maps from when the element's top enters the bottom of the viewport
      // up through `span * vh` distance.
      const raw = (vh - r.top - startOffset * vh) / (vh * span);
      setP(Math.max(0, Math.min(1, raw)));
    };
    const onScroll = () => {
      if (raf) return;
      raf = requestAnimationFrame(compute);
    };
    compute();
    window.addEventListener('scroll', onScroll, { passive: true });
    window.addEventListener('resize', compute);
    return () => {
      window.removeEventListener('scroll', onScroll);
      window.removeEventListener('resize', compute);
      if (raf) cancelAnimationFrame(raf);
    };
  }, [startOffset, span]);
  return [ref, p];
};

// ─── Scroll-direction hook (for nav hide/show) ─────────────
const useScrollDirection = () => {
  const [hidden, setHidden] = useStateA(false);
  useEffectA(() => {
    let lastY = window.scrollY;
    let ticking = false;
    const onScroll = () => {
      if (ticking) return;
      ticking = true;
      requestAnimationFrame(() => {
        const y = window.scrollY;
        const dy = y - lastY;
        // Always show near top
        if (y < 80) {
          setHidden(false);
        } else if (Math.abs(dy) > 4) {
          setHidden(dy > 0); // hide on scroll-down, show on scroll-up
        }
        lastY = y;
        ticking = false;
      });
    };
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);
  return hidden;
};

// ─── NAV ─────────────────────────────────────────────────
const Nav = () => {
  const signupRef = useMagnetic(0.30, 110);
  const hidden = useScrollDirection();
  return (
    <nav className={`nav-wrap ${hidden ? 'nav-wrap-hidden' : ''}`}>
      <div className="nav-pill">
        <a className="nav-logo" href="#" aria-label="Seckira home">
          <img src="assets/logo.png" alt="Seckira" />
        </a>
        <div className="nav-links">
          <a className="nav-link" href="services/secretarial.html">Secretarial</a>
          <a className="nav-link" href="services/accounting.html">Accounting</a>
          <a className="nav-link" href="services/payroll.html">Payroll</a>
          <a className="nav-link" href="services/auditing.html">Auditing</a>
          <a className="nav-link" href="services/tax-services.html">Tax services</a>
        </div>
        <a className="nav-signup" ref={signupRef} href={WHATSAPP_NAV} target="_blank" rel="noopener noreferrer">Contact us</a>
      </div>
    </nav>);

};

// ─── HERO ────────────────────────────────────────────────
const Hero = ({ headlinePreset }) => {
  const h = HEADLINES[headlinePreset] || HEADLINES.lightning;
  const ctaRef = useMagnetic(0.32, 140);
  const onStart = () => {
    const el = document.querySelector('.manifesto');
    if (!el) return;
    const top = el.getBoundingClientRect().top + window.scrollY - 20;
    window.scrollTo({ top, behavior: 'smooth' });
  };
  return (
    <header className="hero">
      <h1 className="hero-display">
        <span>{h.l1}</span><br />
        <span>{h.l2}</span>
      </h1>
      <div className="hero-ctas">
        <button className="hero-cta-primary" ref={ctaRef} onClick={onStart}>
          See it in action
          <svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true">
            <path d="M3 8h10m-4-4 4 4-4 4" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" />
          </svg>
        </button>
      </div>
    </header>);

};

// ─── MANIFESTO (Dashboard) ──────────────────────────────
// Dashboard floats 25% into the hero and "rises" with a soft parallax + scale-in.
const Manifesto = () => {
  const [ref, p] = useScrollProgress(0.05, 0.9);
  // Easing: ease-out cubic
  const e = 1 - Math.pow(1 - p, 3);
  const scale = 0.88 + e * 0.12;
  const ty = (1 - e) * 70;
  return (
    <section className="manifesto" id="manifesto">
      <div
        className="manifesto-inner"
        ref={ref}
        style={{
          transform: `translateY(${ty}px) scale(${scale})`,
          opacity: 0.6 + e * 0.4
        }}>
        <ManifestoDashboard />
      </div>
    </section>);

};


// ─── PRICING ─────────────────────────────────────────────
// Lives in pricing.jsx (window.Pricing). Malaysian Sdn. Bhd. incorporation
// comparison table.

// ─── HOME FAQ ────────────────────────────────────────────
const HOME_FAQ = [
  {
    q: "Do I need to be Malaysian to incorporate a Sdn. Bhd.?",
    a: "No. Foreigners can fully own a Sdn. Bhd. — there's no local shareholding requirement for most industries. You will, however, need at least one resident director (a Malaysian citizen, PR, or pass-holder ordinarily resident in Malaysia). We arrange a nominee resident director as part of our Full Compliance plan."
  },
  {
    q: "How long does Sdn. Bhd. incorporation actually take?",
    a: "Once we have your KYC documents, name reservation typically takes 1–2 working days and the SSM incorporation itself another 2–3 days. Most companies are fully registered within a week. Bank account opening and SST registration are handled separately and depend on the bank's KYC turnaround."
  },
  {
    q: "What's the minimum paid-up capital?",
    a: "Legally, the minimum is RM 1 — but most operating companies start at RM 1,000 to RM 100,000 for credibility with banks, landlords and government tenders. Foreign-owned companies in certain industries (wholesale, distribution, F&B) require RM 500,000 to RM 1,000,000."
  },
  {
    q: "Can I use my home address as the registered address?",
    a: "Yes, but it must be a Malaysian address that can receive statutory mail during business hours. If you'd rather keep your home address off SSM's public register, we provide a registered office in Kuala Lumpur as part of every plan."
  },
  {
    q: "What's a company secretary and why do I need one?",
    a: "Every Sdn. Bhd. must appoint a qualified company secretary within 30 days of incorporation under Section 236(1) of the Companies Act 2016. The secretary keeps your statutory registers current, files annual returns with SSM, holds AGMs, and signs off changes in directors, shareholders or address. Missing it can trigger fines up to RM 50,000."
  },
  {
    q: "What are the ongoing costs after incorporation?",
    a: "At minimum: company secretary retainer (from RM 80/month with us), the annual return filing fee to SSM, and your corporate tax & audit (where applicable) once a year. The Full Compliance plan bundles secretary, accounting, monthly bookkeeping and corporate tax filing into one annual fee."
  },
  {
    q: "Do I need to be in Malaysia to incorporate?",
    a: "No. KYC is done over video call, signatures are captured electronically, and we file with SSM on your behalf. You only need to be physically present in Malaysia later if your chosen bank requires an in-person account opening — most do not."
  },
  {
    q: "Can you migrate me from another secretary or accountant?",
    a: "Yes — most of our new clients come from another provider. The switch takes 3–5 working days for the secretary and 1–2 weeks for accounting (depending on the cleanliness of your prior-year books). We handle the handover paperwork and chase your previous firm directly."
  }
];

const HomeFAQ = () => (
  <section className="home-faq" id="faq">
    <div className="home-faq-inner">
      <div className="home-faq-head">
        <h2 className="section-title">Questions, <em>answered.</em></h2>
        <p className="home-faq-sub">Still figuring out whether a Sdn. Bhd. is right for you, or how the process works? Here are the questions we hear most often.</p>
        <a className="home-faq-cta" href={WHATSAPP_HREF} target="_blank" rel="noopener noreferrer">
          Ask us anything →
        </a>
      </div>
      <div className="home-faq-list">
        {HOME_FAQ.map((item, i) => (
          <details key={i} className="home-faq-item">
            <summary>{item.q}</summary>
            <p>{item.a}</p>
          </details>
        ))}
      </div>
    </div>
  </section>
);

// ─── FOOTER (black bubble + WhatsApp CTA) ───────────────

const Footer = () => {
  const waRef = useMagnetic(0.22, 130);
  return (
    <footer className="footer-section">
      <div className="footer-bubble">
        <div className="footer-bubble-top">
          <div className="footer-bubble-headline">
            <div className="footer-eyebrow">Got questions?</div>
            <h3 className="footer-headline">
              Talk to a licensed<br />company secretary.
            </h3>
          </div>
          <a
            ref={waRef}
            href={WHATSAPP_HREF}
            className="footer-wa-cta"
            target="_blank"
            rel="noopener noreferrer">
            
            <svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true">
              <path fill="currentColor" d="M20.5 3.5A11 11 0 0 0 3.5 17.4L2 22l4.7-1.5A11 11 0 1 0 20.5 3.5Zm-8.5 17a8.6 8.6 0 0 1-4.4-1.2l-.3-.2-2.8.9.9-2.7-.2-.3A8.6 8.6 0 1 1 12 20.5Zm4.7-6.4c-.2-.1-1.5-.7-1.8-.8-.2-.1-.4-.1-.6.1l-.8 1c-.1.2-.3.2-.6.1a7 7 0 0 1-3.5-3 .4.4 0 0 1 .1-.6c.1-.1.3-.4.4-.5.1-.1.2-.3.3-.4.1-.2 0-.4 0-.5l-.8-2c-.2-.5-.4-.4-.6-.4h-.5a1 1 0 0 0-.7.3 3 3 0 0 0-.9 2.2c0 1.3.9 2.6 1 2.8.1.2 1.9 3 4.6 4.2 1.7.7 2.3.8 3.1.7.5-.1 1.5-.6 1.7-1.2.2-.6.2-1.1.2-1.2 0-.1-.2-.2-.4-.3Z" />
            </svg>
            Chat with us on WhatsApp
          </a>
        </div>

        <div className="footer-grid">
          <div className="footer-col footer-col-brand">
            <div className="footer-brand">seckira</div>
            <div className="footer-col-body">
              An SSM-licensed company secretary firm in Malaysia. AI prepares the paperwork. Licensed secretaries verify and submit.
            </div>
          </div>
          <div className="footer-col">
            <div className="footer-col-title">Services</div>
            <ul>
              <li><a href="services/secretarial.html">Company secretary</a></li>
              <li><a href="services/accounting.html">Tax &amp; accounting</a></li>
              <li><a href="services/payroll.html">Payroll</a></li>
              <li><a href="#pricing">Sdn. Bhd. incorporation</a></li>
            </ul>
          </div>
          <div className="footer-col">
            <div className="footer-col-title">Company</div>
            <ul>
              <li><a href="#manifesto">Dashboard</a></li>
              <li><a href="#offer">What we offer</a></li>
              <li><a href="#pricing">Pricing</a></li>
            </ul>
          </div>
          <div className="footer-col">
            <div className="footer-col-title">Contact</div>
            <ul>
              <li><a href="mailto:info@seckira.com">info@seckira.com</a></li>
              <li><a href="tel:+60102049494">+60 10-204 9494</a></li>
              <li>No. 38-1, Jalan Aman Tiara 8, Triana,<br />Bandar Tropicana Aman, 42500<br />Telok Panglima Garang, Selangor,<br />Malaysia</li>
            </ul>
          </div>
        </div>

        <div className="footer-fine">
          <div>
            Seckira Sdn. Bhd. (Co. No. 202401017441 / 1563291-W) · SSM-licensed company secretary firm · MIA member firm
          </div>
          <div className="footer-legal">
            <a href="legal/privacy.html">Privacy</a>
            <a href="legal/terms.html">Terms</a>
            <a href="legal/cookie-policy.html">Cookie policy</a>
            <span>© 2026 Seckira. All rights reserved.</span>
          </div>
        </div>
      </div>
    </footer>);

};

// ─── APP SHELL ───────────────────────────────────────────
const App = () => {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);
  return (
    <>
      <Nav />
      <Hero headlinePreset={t.headlinePreset} />

      <Manifesto />
      <BentoOffer />
      <Pricing />
      <HomeFAQ />
      <Footer />

      <TweaksPanel title="Tweaks">
        <TweakSection label="Headline" />
        <TweakRadio
          label="Preset"
          value={t.headlinePreset}
          options={['lightning', 'sorted', 'easy']}
          onChange={(v) => setTweak('headlinePreset', v)} />
      </TweaksPanel>
    </>);

};

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