// Sections.jsx — landing-page sections, themed via CSS vars on the wrapping div.

function Nav({ active }) {
  return (
    <div className="nav">
      <div className="wrap nav-inner">
        <a href="index.html" className="brand">
          <div className="brand-mark">S</div>
          <div className="brand-name">sourceweaver</div>
        </a>
        <div className="nav-links">
          <a href="index.html#how">How it works</a>
          <a href="index.html#pipelines">Pipelines</a>
          <a href="index.html#self">Self-built</a>
          <a href="docs.html" className={active==='docs' ? 'active' : ''}>Docs</a>
        </div>
        <div className="nav-spacer" />
      </div>
    </div>
  );
}

function AppShellFrame() {
  const ref = React.useRef(null);
  const [height, setHeight] = React.useState(620);
  React.useEffect(() => {
    function onMsg(e) {
      const d = e.data;
      if (d && d.type === '__sw_app_height' && typeof d.height === 'number') {
        setHeight(d.height);
      }
    }
    window.addEventListener('message', onMsg);
    return () => window.removeEventListener('message', onMsg);
  }, []);
  // Push the current theme into the iframe as soon as it loads.
  function onLoad() {
    try {
      const theme = document.documentElement.dataset.theme || 'terminal';
      ref.current.contentWindow.postMessage({ type: '__sw_set_theme', theme }, '*');
    } catch {}
  }
  const theme = (typeof document !== 'undefined' && document.documentElement.dataset.theme) || 'terminal';
  return (
    <iframe
      ref={ref}
      data-sw-app="true"
      src={`app.html?theme=${theme}&embed=1&tab=workspace&subtab=activity`}
      title="SourceWeaver app preview"
      onLoad={onLoad}
      style={{
        width: '100%',
        height: height + 'px',
        border: 0,
        background: 'transparent',
        display: 'block',
      }}
      scrolling="no"
    />
  );
}

function Hero() {
  return (
    <section className="hero hero-stacked">
      <div className="wrap">
        <div className="hero-text">
          <div className="hero-stamp">
            <span className="hero-stamp-dot" />
            <span>v0.3 · self-hosted alpha</span>
          </div>
          <h1>
            The AI project manager that <span className="accent">ships your code</span>.
          </h1>
          <p className="hero-sub">
            Create work items. Approve them. SourceWeaver runs the pipeline — research, plan, execute, review — and opens the PR. Self-hosted. Self-building. Yours.
          </p>
          <div className="hero-actions">
            <a className="btn btn-primary" href="#how">See how it works</a>
          </div>
        </div>
        <div className="hero-shell">
          <AppShellFrame />
        </div>
      </div>
    </section>
  );
}

function Strip() {
  return (
    <div className="strip">
      <div className="wrap strip-inner">
        <span>Stack</span>
        <span>Claude Code</span>
        <span>MCP</span>
        <span>PostgreSQL</span>
        <span>.NET</span>
        <span>Docker</span>
      </div>
    </div>
  );
}

function HowItWorks() {
  return (
    <section className="section" id="how">
      <div className="wrap">
        <div className="section-head">
          <div>
            <div className="section-eyebrow">How it works</div>
            <h2>Scope it. Break it down. Ship it.</h2>
          </div>
          <p className="section-blurb">
            You write the Feature. SourceWeaver asks the questions a senior engineer would ask, generates the child tickets, and runs each one through a real pipeline — research, plan, execute, review, PR. Every stage is YAML you own.
          </p>
        </div>
        <div className="flow">
          <div className="flow-card">
            <div className="flow-step">01 · Scope</div>
            <h3>Write a Feature. SourceWeaver clarifies it.</h3>
            <p>Drop a Feature into the workspace with a title and acceptance criteria. SourceWeaver clarifies the gaps with you, then generates the Tasks and Bugs to cover the work.</p>
            <div className="flow-card-foot">clarify → tickets → ready to run</div>
          </div>
          <div className="flow-card">
            <div className="flow-step">02 · Build</div>
            <h3>Each ticket runs the pipeline.</h3>
            <p>Tasks: research → plan → expand → execute → review → PR. Bugs swap research for diagnose and add a regression-test validate step. Plans are split into independent units that execute in parallel. You watch it stream live.</p>
            <div className="flow-card-foot">workers claim → stream → open PR</div>
          </div>
          <div className="flow-card">
            <div className="flow-step">03 · Land</div>
            <h3>Merge queue with self-healing.</h3>
            <p>PRs queue up: rebase, gate on CI, fast-forward. Conflicts trigger an AI rebase and force-push-with-lease. Overwatch sweeps for stuck stages, dead workers, and broken queues every minute. Once the last child PR merges, SourceWeaver opens the feature PR.</p>
            <div className="flow-card-foot">queue → conflict-resolve → overwatch → merged</div>
          </div>
        </div>
      </div>
    </section>
  );
}

function WorkspaceSection() {
  return (
    <section className="section" id="workspace">
      <div className="wrap">
        <div className="section-head">
          <div>
            <div className="section-eyebrow">The workspace</div>
            <h2>Features. Tasks. Bugs. The same primitives, but they actually do work.</h2>
          </div>
          <p className="section-blurb">
            Every item is a node in a pipeline graph. Three-panel layout: document tree on the left, a real markdown editor with EDIT / SPLIT / PREVIEW in the middle, context-aware chat on the right. Flip to the board to watch tickets move through stages. The same pane the agent reads when it picks up the work.
          </p>
        </div>
        <TaskEditView />
      </div>
    </section>
  );
}

function MergeQueueSection() {
  return (
    <section className="section" id="mergequeue">
      <div className="wrap">
        <div className="section-head">
          <div>
            <div className="section-eyebrow">
              Merge queue
              <span className="optional-badge">optional</span>
            </div>
            <h2>A serialized merge queue that heals itself.</h2>
          </div>
          <p className="section-blurb">
            Optional. Off by default — review and merge PRs yourself, or flip on the queue and let it serialize them. Every queued PR is rebased onto its base, gated on CI, then fast-forwarded. When a rebase hits conflicts or CI goes red, recovery workflows fire automatically — diagnose, fix, retry.          </p>
        </div>
        <MergeQueue />
      </div>
    </section>
  );
}

function PipelinesSection() {
  return (
    <section className="section" id="pipelines">
      <div className="wrap">
        <div className="section-head">
          <div>
            <div className="section-eyebrow">Pipelines</div>
            <h2>Workflow-driven, not chat-driven.</h2>
          </div>
          <p className="section-blurb">
            Workflows are YAML. Stages run on AI or on the API, with timeouts and retries declared inline. Below is a summarized view of the workflow that runs against an approved Task. Hover any stage.
          </p>
        </div>
        <YamlBlock />
      </div>
    </section>
  );
}

function SelfBuilt() {
  return (
    <section className="self" id="self">
      <div className="wrap">
        <div className="section-head">
          <div>
            <div className="section-eyebrow">Proof</div>
            <h2>This product builds itself. The site’s backend included.</h2>
          </div>
          <p className="section-blurb">
            We don’t demo on a toy repo. SourceWeaver’s own backlog runs through SourceWeaver. If it can’t ship its own features, you’d be the first to know.
          </p>
        </div>
        <div className="self-grid">
          <div>
            <div className="self-pr">
              <div className="self-pr-head">most recent self-built PR</div>
              <div className="self-pr-title">#556 · Add OAuth fallback for self-hosted SSO</div>
              <div className="self-pr-quote">Opened by sourceweaver-bot. 8 of 8 acceptance checks. CI green. Merged 14 minutes after approval.</div>
            </div>
          </div>
          <div>
            <p style={{ fontSize: 17, lineHeight: 1.55, textWrap: 'pretty' }}>
              Every feature in the changelog has a link to the PR that built it. Every PR has a link to the work item that scoped it. Every work item has a stream of the agent that wrote the code. No black boxes.
            </p>
            <div className="self-stat-row">
              <div>
                <div className="self-stat-num">556</div>
                <div className="self-stat-label">self-built PRs</div>
              </div>
              <div>
                <div className="self-stat-num">87%</div>
                <div className="self-stat-label">merged unchanged</div>
              </div>
              <div>
                <div className="self-stat-num">14m</div>
                <div className="self-stat-label">median lead time</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

function AudienceSplit() {
  return (
    <section className="section" id="audience">
      <div className="wrap">
        <div className="section-head">
          <div>
            <div className="section-eyebrow">Who uses it</div>
            <h2>Built for solo builders. Hardened for teams that can’t use the cloud.</h2>
          </div>
          <p className="section-blurb">
            The same binary runs on a Hetzner box and inside an air-gapped VPC. Pick your reason.
          </p>
        </div>
        <div className="split">
          <div className="split-col">
            <h3>Solo devs and indie hackers</h3>
            <p>You’re shipping ambitious projects alone. Get five autonomous engineers without hiring five autonomous engineers.</p>
            <ul>
              <li>Dispatch a backlog before bed; review PRs over coffee.</li>
              <li>One docker-compose up. Bring your AI key from anywhere.</li>
              <li>Approval gates and stage timeouts between every step — no runaway loops.</li>
              <li>Fork the YAML. It’s your workflow. Edit anything.</li>
            </ul>
          </div>
          <div className="split-col">
            <h3>Teams with sovereignty requirements</h3>
            <p>Defense, fintech, healthcare, anyone with a security review. Your code never leaves your VPC.</p>
            <ul>
              <li>Fully self-hosted. No phone-home telemetry.</li>
              <li>Per-repo, per-pipeline RBAC. SSO via your IdP.</li>
              <li>Audit log of every agent action and approval gate.</li>
              <li>MCP tools you control — agents see only what you expose.</li>
            </ul>
          </div>
        </div>
      </div>
    </section>
  );
}

function DiffGrid() {
  const cells = [
    ['01', 'Self-hosted, self-contained', 'Your code, your infra, your data. Nothing leaves the box. No vendor lock-in.'],
    ['02', 'Self-building', 'SourceWeaver develops itself. Public proof, every release.'],
    ['03', 'Workflow over chat', 'Declarative YAML pipelines. Versioned. Diffable. Reviewable.'],
    ['04', 'Human-in-the-loop', 'Approval gates between every stage. Manual, automatic, or conditional.'],
    ['05', 'Real engineering integration', 'Git branches. Real PRs. CI. Auto-deploy. Not a chat log.'],
    ['06', 'Live streams, not status pages', 'Watch the agent read, plan, and write. SignalR over a real socket.'],
  ];
  return (
    <section className="section" id="diff">
      <div className="wrap">
        <div className="section-head">
          <div>
            <div className="section-eyebrow">Differentiators</div>
            <h2>What you actually get — and what you don’t.</h2>
          </div>
          <p className="section-blurb">
            Not another assistant. Not another chat. SourceWeaver is closer to a CI system that happens to write code than a copilot that happens to ship.
          </p>
        </div>
        <div className="diffgrid">
          {cells.map(([n, h, p]) => (
            <div key={n} className="diff-cell">
              <div className="diff-icon">{n}</div>
              <h4>{h}</h4>
              <p>{p}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

function InterestCTA() {
  const [email, setEmail] = React.useState('');
  const [note, setNote] = React.useState('');
  const [state, setState] = React.useState('idle'); // idle | sending | ok | err
  const [errMsg, setErrMsg] = React.useState('');

  async function onSubmit(e) {
    e.preventDefault();
    setState('sending');
    setErrMsg('');
    try {
      const res = await fetch('/api/interest', {
        method: 'POST',
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify({ email, note }),
      });
      if (!res.ok) {
        const data = await res.json().catch(() => ({}));
        throw new Error(data.error || `http_${res.status}`);
      }
      setState('ok');
    } catch (err) {
      setState('err');
      setErrMsg(err.message || 'unknown');
    }
  }

  return (
    <section className="cta" id="interest">
      <div className="wrap">
        <div className="section-eyebrow" style={{ marginBottom: 14 }}>Closed alpha</div>
        <h2>Register interest.</h2>
        <p className="cta-sub">SourceWeaver isn’t open yet. Drop your email and we’ll reach out when we open up access.</p>
        {state === 'ok' ? (
          <div className="interest-ok">
            <span className="accent">✓</span> You’re on the list. We’ll be in touch.
          </div>
        ) : (
          <form className="interest-form" onSubmit={onSubmit}>
            <input
              type="email"
              required
              placeholder="you@company.com"
              value={email}
              onChange={(e) => setEmail(e.target.value)}
              disabled={state === 'sending'}
              autoComplete="email"
            />
            <textarea
              rows={3}
              placeholder="What would you use it for? (optional)"
              value={note}
              onChange={(e) => setNote(e.target.value)}
              disabled={state === 'sending'}
              maxLength={1000}
            />
            <button type="submit" className="btn btn-primary" disabled={state === 'sending' || !email}>
              {state === 'sending' ? 'Sending…' : 'Register interest'}
            </button>
            {state === 'err' && (
              <div className="interest-err">Couldn’t submit ({errMsg}). Try again in a moment.</div>
            )}
          </form>
        )}
      </div>
    </section>
  );
}

function Footer() {
  return (
    <footer className="foot">
      <div className="wrap">
        <div className="foot-grid">
          <div className="foot-brand">
            <div className="brand">
              <div className="brand-mark">S</div>
              <div className="brand-name">SourceWeaver</div>
            </div>
            <div className="foot-tag">Autonomous project management for people who actually ship. Self-hosted. Self-building.</div>
          </div>
          <div className="foot-col">
            <h5>Product</h5>
            <a href="#how">How it works</a>
            <a href="#pipelines">Pipelines</a>
            <a href="#self">Self-built</a>
          </div>
          <div className="foot-col">
            <h5>Docs</h5>
            <a href="#docs">Quickstart</a>
            <a href="#docs">Pipeline reference</a>
            <a href="#docs">MCP tools</a>
            <a href="#docs">Self-hosting</a>
          </div>
          <div className="foot-col">
            <h5>Community</h5>
            <a href="#">Discord</a>
            <a href="#">Changelog</a>
            <a href="#">Status</a>
          </div>
        </div>
        <div className="foot-bottom">
          <span>© 2026 SourceWeaver</span>
          <span>built by SourceWeaver</span>
        </div>
      </div>
    </footer>
  );
}

function Landing() {
  return (
    <div className="shell">
      <Nav />
      <Hero />
      <Strip />
      <HowItWorks />
      <WorkspaceSection />
      <PipelinesSection />
      <MergeQueueSection />
      <SelfBuilt />
      <AudienceSplit />
      <DiffGrid />
      <InterestCTA />
      <Footer />
    </div>
  );
}

Object.assign(window, { Landing });
