import { BenchmarkReplay } from "./components/BenchmarkReplay";
import { ManifestInspector } from "./components/ManifestInspector";

const githubUrl = "https://github.com/CarlSR9001/scopewrit";

const schema = {
  "@context": "https://schema.org",
  "@type": "SoftwareApplication",
  name: "ScopeWrit",
  applicationCategory: "DeveloperApplication",
  operatingSystem: "Cross-platform",
  url: "https://scopewrit.cyberelf.link/",
  codeRepository: githubUrl,
  license: "https://www.gnu.org/licenses/agpl-3.0.html",
  creator: {
    "@type": "Organization",
    name: "Cyberelf Labs",
    url: "https://cyberelf.link/",
  },
  description:
    "Signed, deny-by-default run authority for autonomous agents.",
};

const flow = [
  {
    number: "01",
    label: "Mandate",
    title: "Sign the run boundary",
    body: "A trusted principal signs the exact resources, effects, limits, expiry, and deny rules for one run.",
  },
  {
    number: "02",
    label: "Decision",
    title: "Authorize the action",
    body: "Every consequential proposal is canonicalized, checked deny-first, counted transactionally, and recorded.",
  },
  {
    number: "03",
    label: "Capability",
    title: "Bind permission to execution",
    body: "An exact, expiring, consume-once ticket binds the action to its adapter so approval cannot be replayed elsewhere.",
  },
  {
    number: "04",
    label: "Receipt",
    title: "Prove what happened",
    body: "A hash-chained recorder and signed audit packet bind the mandate, decisions, and final ledger head.",
  },
];

const surfaces = [
  {
    title: "HTTP egress",
    status: "implemented",
    body: "Canonical URL policy, explicit private-network scope, redirect and proxy refusal, DNS evidence, bounded bodies, and forbidden-header checks.",
  },
  {
    title: "Subprocess",
    status: "implemented",
    body: "Absolute executable paths, principal-signed exact argv digests, shell disabled, and adapter-bound execution tickets.",
  },
  {
    title: "MCP tools",
    status: "implemented",
    body: "Exact server and tool identity plus argument hashing before a tool callback receives authority.",
  },
  {
    title: "SDK operations",
    status: "integration layer",
    body: "Fixed-class adapters for browser, cloud, credential, package, and publication operations. The real SDK call must be routed through them.",
  },
];

const attacks = [
  "Manifest tamper and trust-root substitution",
  "Nonce reuse across runs",
  "Approval and ticket replay",
  "Cross-adapter ticket use",
  "Deny-versus-allow precedence",
  "Concurrent counter oversubscription",
  "Ledger mutation and valid-prefix truncation",
  "Path escape, host-header, body-size, and URI ambiguity",
];

export default function Home() {
  return (
    <>
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(schema) }}
      />
      <a className="skip-link" href="#main">
        Skip to content
      </a>

      <header className="site-header">
        <a className="brand" href="#top" aria-label="ScopeWrit home">
          <span className="brand-mark" aria-hidden="true">
            <i />
            <b>SW</b>
          </span>
          <span>
            <strong>ScopeWrit</strong>
            <small>Cyberelf Labs</small>
          </span>
        </a>
        <nav aria-label="Primary navigation">
          <a href="#inspect">Inspect a manifest</a>
          <a href="#proof">Proof</a>
          <a href="#mechanism">Mechanism</a>
          <a href="#surface">Surface</a>
          <a href="#boundary">Boundary</a>
        </nav>
        <a className="header-cta" href={githubUrl}>
          Source <span aria-hidden="true">↗</span>
        </a>
      </header>

      <main id="main">
        <ManifestInspector />
        <section className="hero shell" id="top">
          <div className="hero-grid" aria-hidden="true" />
          <div className="hero-copy">
            <p className="eyebrow">
              <span>PUBLIC ALPHA</span>
              Deterministic run authority
            </p>
            <h1>
              Reachable is not
              <em> authorized.</em>
            </h1>
            <p className="hero-lede">
              ScopeWrit is a signed, deny-by-default execution boundary for
              autonomous agents. The model can propose anything. Consequences
              still require exact authority for this action, this resource, and
              this run.
            </p>
            <div className="hero-actions">
              <a className="button button-primary" href="#proof">
                Replay the evidence <span aria-hidden="true">↓</span>
              </a>
              <a className="button button-secondary" href={githubUrl}>
                Inspect the implementation <span aria-hidden="true">↗</span>
              </a>
            </div>
            <p className="hero-note">
              AGPL-3.0 · Python 3.11–3.13 · Ed25519 · SQLite · no model
              dependency
            </p>
          </div>

          <div className="authority-orbit" aria-label="ScopeWrit authorization flow">
            <div className="orbit-grid" aria-hidden="true" />
            <div className="orbit-ring ring-one" aria-hidden="true" />
            <div className="orbit-ring ring-two" aria-hidden="true" />
            <div className="orbit-core">
              <span>ACTION 02</span>
              <strong>DENY</strong>
              <small>explicit_deny_rule</small>
            </div>
            <div className="orbit-node node-mandate">
              <span>01</span>
              <b>Signed mandate</b>
              <small>trusted principal</small>
            </div>
            <div className="orbit-node node-proposal">
              <span>02</span>
              <b>Action proposal</b>
              <small>outside target</small>
            </div>
            <div className="orbit-node node-recorder">
              <span>03</span>
              <b>Flight recorder</b>
              <small>chain verified</small>
            </div>
          </div>

          <div className="hero-strip">
            <span>Engineering gate</span>
            <strong><i /> PASS / PUBLIC ALPHA</strong>
            <ul>
              <li>47 regression cases</li>
              <li>3 Python versions</li>
              <li>0 guarded outside effects</li>
            </ul>
          </div>
        </section>

        <section className="proof-section shell" id="proof">
          <div className="section-heading split-heading">
            <div>
              <p className="kicker">01 / Falsifiable first</p>
              <h2>The same action. One hard boundary.</h2>
            </div>
            <p>
              The published benchmark creates a synthetic internet on loopback.
              The deterministic control reaches an outside target. Routed
              through ScopeWrit, it attempts the same URL and is denied before
              network execution.
            </p>
          </div>

          <BenchmarkReplay />

          <div className="evidence-row" aria-label="Published evidence summary">
            <div>
              <span>UNGARDED OUTSIDE EFFECT</span>
              <strong className="danger">1</strong>
              <small>synthetic compromise: true</small>
            </div>
            <div>
              <span>GUARDED OUTSIDE EFFECT</span>
              <strong className="safe">0</strong>
              <small>synthetic compromise: false</small>
            </div>
            <div>
              <span>AUTHORIZED WORK</span>
              <strong>2/2</strong>
              <small>briefing and health preserved</small>
            </div>
            <div>
              <span>AUDIT INTEGRITY</span>
              <strong>9/9</strong>
              <small>events · chain and packet valid</small>
            </div>
          </div>
        </section>

        <section className="mechanism-section" id="mechanism">
          <div className="shell">
            <div className="section-heading">
              <p className="kicker">02 / The enforcement path</p>
              <h2>Authority lives outside the model.</h2>
              <p>
                Prompts express intent. ScopeWrit controls effects. The
                enforcement decision is deterministic, signed, expiring,
                replay-resistant, and auditable.
              </p>
            </div>

            <ol className="flow-grid">
              {flow.map((item) => (
                <li key={item.number}>
                  <span className="flow-number">{item.number}</span>
                  <p>{item.label}</p>
                  <h3>{item.title}</h3>
                  <div className="flow-line" aria-hidden="true">
                    <i />
                  </div>
                  <small>{item.body}</small>
                </li>
              ))}
            </ol>

            <div className="policy-sample">
              <div className="code-card">
                <div className="code-head">
                  <span>scope.yaml</span>
                  <b>PRINCIPAL SIGNED</b>
                </div>
                <pre>
                  <code>{`mandate_id: eval-7f91
objective: retrieve_flag
expires_at: 2026-07-31T02:00:00Z

rules:
  - effect: network.http
    resource: https://challenge.internal/*
    decision: allow
    max_uses: 200

  - effect: package.publish
    resource: "*"
    decision: deny`}</code>
                </pre>
              </div>
              <div className="decision-stack">
                <p className="stack-label">PROPOSED ACTION</p>
                <div>
                  <span>effect</span>
                  <strong>package.publish</strong>
                </div>
                <div>
                  <span>resource</span>
                  <strong>pypi.org/project/new</strong>
                </div>
                <i aria-hidden="true">↓</i>
                <div className="denied">
                  <span>DECISION</span>
                  <strong>DENY</strong>
                  <small>explicit_deny_rule</small>
                </div>
              </div>
            </div>
          </div>
        </section>

        <section className="surface-section shell" id="surface">
          <div className="section-heading split-heading">
            <div>
              <p className="kicker">03 / Consequence surfaces</p>
              <h2>One decision grammar. Explicit adapters.</h2>
            </div>
            <p>
              ScopeWrit does not pretend a Python package is a kernel sandbox.
              It enforces the paths actually integrated through its adapters
              and tells you exactly where the boundary ends.
            </p>
          </div>
          <div className="surface-grid">
            {surfaces.map((surface) => (
              <article key={surface.title}>
                <p>{surface.status}</p>
                <h3>{surface.title}</h3>
                <span>{surface.body}</span>
              </article>
            ))}
          </div>
        </section>

        <section className="test-section">
          <div className="shell test-layout">
            <div className="section-heading">
              <p className="kicker">04 / Adversarial gate</p>
              <h2>Built to fail closed.</h2>
              <p>
                The test suite attacks the authorization boundary, not just the
                happy path. Every listed case is executable in the public
                repository.
              </p>
              <a className="text-link" href={`${githubUrl}/tree/main/tests_py`}>
                Read the test cases <span aria-hidden="true">↗</span>
              </a>
            </div>
            <div className="attack-list">
              {attacks.map((attack, index) => (
                <div key={attack}>
                  <span>{String(index + 1).padStart(2, "0")}</span>
                  <p>{attack}</p>
                  <b>PASS</b>
                </div>
              ))}
            </div>
          </div>
        </section>

        <section className="boundary-section shell" id="boundary">
          <div className="section-heading">
            <p className="kicker">05 / Evidence boundary</p>
            <h2>What the alpha proves—and what it does not.</h2>
          </div>
          <div className="boundary-grid">
            <article className="proves">
              <p>PROVEN IN THE PUBLISHED HARNESS</p>
              <ul>
                <li>
                  Authorized loopback HTTP work survives the enforcement path.
                </li>
                <li>
                  The identical synthetic outside request is denied before
                  execution.
                </li>
                <li>
                  Signed scope, counters, tickets, replay controls, and the
                  audit chain survive adversarial regression tests.
                </li>
                <li>
                  The wheel builds and installs cleanly across the supported
                  Python line.
                </li>
              </ul>
            </article>
            <article className="not-proves">
              <p>NOT CLAIMED</p>
              <ul>
                <li>Transparent host, kernel, VM, or container isolation.</li>
                <li>
                  Safety for an agent that retains raw capabilities outside a
                  guarded adapter.
                </li>
                <li>Multi-node high availability or KMS/HSM key custody.</li>
                <li>
                  Independent penetration testing, certification, or a
                  real-model behavioral study.
                </li>
              </ul>
            </article>
          </div>
          <div className="latency-strip">
            <div>
              <span>ALLOW + TICKET CONSUME</span>
              <strong>20.07 ms</strong>
              <small>median · 27.25 ms p95</small>
            </div>
            <div>
              <span>DEFAULT DENY</span>
              <strong>2.71 ms</strong>
              <small>median · 4.36 ms p95</small>
            </div>
            <p>
              500 durable local iterations per path, including Ed25519,
              transactional state, ticket mint/consume, and fsynced recording.
              This is a machine-local receipt, not a capacity SLA.
            </p>
          </div>
        </section>

        <section className="cta-section">
          <div className="shell cta-inner">
            <p className="kicker">Scope the run. Then let the agent work.</p>
            <h2>
              Agent autonomy needs
              <em> mechanical authority.</em>
            </h2>
            <p>
              ScopeWrit is an open public alpha for teams operating autonomous
              coding, security, cloud, and evaluation agents in
              high-consequence environments.
            </p>
            <div className="hero-actions">
              <a className="button button-primary" href={githubUrl}>
                Clone ScopeWrit <span aria-hidden="true">↗</span>
              </a>
              <a
                className="button button-secondary"
                href="mailto:justin@cyberelf.link?subject=ScopeWrit"
              >
                Discuss an integration <span aria-hidden="true">↗</span>
              </a>
            </div>
          </div>
        </section>
      </main>

      <footer>
        <div className="shell footer-inner">
          <a className="brand" href="#top">
            <span className="brand-mark" aria-hidden="true">
              <i />
              <b>SW</b>
            </span>
            <span>
              <strong>ScopeWrit</strong>
              <small>Cyberelf Labs</small>
            </span>
          </a>
          <p>Signed, deny-by-default run authority for autonomous agents.</p>
          <div>
            <a href={githubUrl}>GitHub ↗</a>
            <a href="https://cyberelf.link/">Cyberelf Labs ↗</a>
            <a href="mailto:justin@cyberelf.link">Contact ↗</a>
          </div>
        </div>
      </footer>
    </>
  );
}
