/* Docs — Getting started, core concepts, code walkthrough, architecture.
   Globals from shared.jsx. */
const DocsDS = window.ZeroZ4JDesignSystem_f7e0ed;

function DocHeader() {
  return (
    <div style={{ padding: "96px 64px 72px", borderBottom: "1px solid var(--color-steel)", position: "relative" }}>
      <div style={{ maxWidth: 1440, margin: "0 auto" }}>
        <Eyebrow>Documentation</Eyebrow>
        <h1 style={{ fontFamily: "var(--font-sans)", fontWeight: 300, fontSize: "var(--text-64)", letterSpacing: "var(--tracking-tight)", lineHeight: 1.02, color: "var(--color-bone)", margin: 0, maxWidth: 900 }}>
          Everything is <Serif>Java.</Serif> Here is how it fits.
        </h1>
        <p style={{ fontFamily: "var(--font-sans)", fontSize: "var(--text-16)", color: "var(--color-ash)", marginTop: 28, lineHeight: 1.6, maxWidth: 620 }}>
          A unified, zero-impedance Java stack — no REST, no JSON, no ORM, no JavaScript. Build a full-stack feature, learn the core concepts that define the model, then start coding.
        </p>
        <div style={{ display: "flex", gap: 32, marginTop: 40, flexWrap: "wrap", fontFamily: "var(--font-mono)", fontSize: "var(--text-12)", textTransform: "uppercase", letterSpacing: "var(--tracking-wide)" }}>
          {[["#start", "Getting started"], ["#concepts", "Core concepts"], ["#walkthrough", "Code walkthrough"], ["#arch", "Architecture"]].map(([h, l]) =>
          <a key={h} href={h} style={{ color: "var(--color-brass)", textDecoration: "none" }}>{l} ↓</a>
          )}
        </div>
      </div>
    </div>);

}

function GettingStarted() {
  return (
    <Section id="start">
      <div style={{ maxWidth: 820, marginBottom: 56 }}>
        <Eyebrow>01 · Getting started</Eyebrow>
        <H2>A full-stack feature in <Serif>four files.</Serif></H2>
        <p style={{ fontFamily: "var(--font-sans)", fontSize: "var(--text-16)", color: "var(--color-ash)", marginTop: 24, lineHeight: 1.6, maxWidth: 660 }}>
          You avoid boilerplate HTTP mapping, JSON translation, and ORM schemas entirely. Define a model, an interface, its implementation, and a UI — all Java, all sharing the exact same object.
        </p>
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 24 }}>
        <CodeBlock file="1 · ChatMessage.java" lang="SHARED · MODEL">
{cm("// becomes serializable + persistable")}{"\n"}
{cy("@DataModel")}{"\n"}
{kw("public class")} ChatMessage {kw("implements")} BinaryPackable {"{"}{"\n"}
{"    "}{kw("private")} String author;{"\n"}
{"    "}{kw("private")} String text;{"\n\n"}
{"    "}{kw("public")} ChatMessage() {"{}"}{"\n"}
{"    "}{cm("// constructor, getters, setters…")}{"\n"}
{"}"}
        </CodeBlock>
        <CodeBlock file="2 · ChatService.java" lang="SHARED · API">
{cm("// the RPC contract — one interface")}{"\n"}
{cy("@RmiService")}{"\n"}
{kw("public interface")} ChatService {"{"}{"\n"}
{"    "}{kw("void")} sendMessage(ChatMessage msg);{"\n"}
{"}"}
        </CodeBlock>
        <CodeBlock file="3 · ChatServiceImpl.java" lang="SERVER">
{cy("@ApplicationScoped")}{"\n"}
{kw("public class")} ChatServiceImpl {kw("implements")} ChatService {"{"}{"\n"}
{"    "}{cy("@Override")}{"\n"}
{"    "}{kw("public")} {kw("void")} sendMessage(ChatMessage msg) {"{"}{"\n"}
{"        "}{cm("// receives the exact object sent")}{"\n"}
{"        "}System.out.println(msg.getText());{"\n"}
{"    "}{"}"}{"\n"}
{"}"}
        </CodeBlock>
        <CodeBlock file="4 · ChatView.java" lang="CLIENT · WASM">
{kw("public class")} ChatView {kw("extends")} Div {"{"}{"\n"}
{"  "}ChatView(ChatService chat) {"{"}{"\n"}
{"    "}Button b = {kw("new")} Button({cy("\"Send\"")});{"\n"}
{"    "}b.onClick(e -> {"{"}{"\n"}
{"      "}{cm("// calls backend over binary WS")}{"\n"}
{"      "}chat.sendMessage({kw("new")} ChatMessage({cy("\"Alice\"")}, {cy("\"Hi!\"")}));{"\n"}
{"    "}{"}"});{"\n"}
{"    "}add(b);{"\n"}
{"  "}{"}"}{"\n"}
{"}"}
        </CodeBlock>
      </div>
      <div style={{ marginTop: 24, fontFamily: "var(--font-mono)", fontSize: "var(--text-12)", color: "var(--color-ash)" }}>
        → scaffold the multi-module project with the Maven archetype, then <span style={{ color: "var(--color-cyan)" }}>mvn clean install</span> and open <span style={{ color: "var(--color-cyan)" }}>http://localhost:8080</span>
      </div>
    </Section>);

}

function Concepts() {
  const items = [
  ["@DataModel", "Marks a domain model or entity for transmission. Triggers zeroz4j-apt to generate an ultra-fast binary serializer at compile time."],
  ["@RmiService", "Annotates a service interface. A client-side Wasm stub is generated; you call the interface, it executes on the server over a persistent WebSocket."],
  ["Zeroz4jClient", "Bootstraps the Wasm client. connect(url, callback) opens the binary WebSocket and initializes the RMI subsystem."],
  ["Zeroz4jServer", "Bootstraps the Helidon backend. start(port, name) spins up HTTP/WebSocket, the Weld CDI environment, and the RMI dispatcher."],
  ["@Secured / @RolesAllowed", "Method-level security on the server. Restrict RMI invocations to authenticated users, or to specific roles."],
  ["RmiSecurityContext", "Handles auth. On a validated AUTH frame, onAuthenticated() fires client-side so you can load authenticated views."],
  ["@LiveSync", "Reactive real-time updates, fully implicit. Annotate a @DataModel with @LiveSync and a fetched object updates itself inline whenever the server changes it — no watch(), no subscribe(), no polling."],
  ["EclipseStore", "Native persistence. Your Java memory graph is the database. store(object) serializes the delta straight to disk — no JPA, no SQL."],
  ["zeroz4j-ui-components", "A Vaadin-like programmatic UI model in Java — new Button(\"Click\") — styled with Tailwind + DaisyUI. No HTML or DOM code."],
  ["Reactive Signals", "Frontend reactive primitives in com.zeroz4j.ui.signals: ValueSignal holds state, Computed derives from it, Effect re-runs when tracked dependencies change — all in the Wasm heap."]];

  return (
    <Section id="concepts" style={{ background: "var(--color-vault)" }}>
      <div style={{ maxWidth: 760, marginBottom: 64 }}>
        <Eyebrow>02 · Core concepts</Eyebrow>
        <H2>Ten ideas define the <Serif>whole model.</Serif></H2>
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(2, 1fr)", border: "1px solid var(--color-steel)", borderLeft: "none", borderTop: "none" }}>
        {items.map(([t, b], i) =>
        <div key={t} style={{ borderLeft: "1px solid var(--color-steel)", borderTop: "1px solid var(--color-steel)", padding: 32, background: "var(--color-graphite)" }}>
            <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 14 }}>
              <code style={{ fontFamily: "var(--font-mono)", fontSize: "var(--text-16)", color: "var(--color-bone)" }}>{t}</code>
              <span style={{ fontFamily: "var(--font-mono)", fontSize: "var(--text-10)", color: "var(--color-ash)", letterSpacing: "var(--tracking-wide)" }}>[{String(i + 1).padStart(2, "0")}]</span>
            </div>
            <p style={{ fontFamily: "var(--font-sans)", fontSize: "var(--text-14)", color: "var(--color-ash)", margin: 0, lineHeight: 1.6 }}>{b}</p>
          </div>
        )}
      </div>
    </Section>);

}

function Walkthrough() {
  const steps = [
  { n: "01", t: "Declare the domain model", d: "Annotate the model with @DataModel and implement BinaryPackable. A compile-time serializer reads and writes fields without reflection.",
    code: <CodeBlock file="UserInfo.java" lang="SHARED">
{cy("@DataModel")}{"\n"}
{kw("public class")} UserInfo {kw("implements")} BinaryPackable {"{"}{"\n"}
{"    "}{kw("private")} String name;{"\n"}
{"    "}{kw("private")} {kw("int")} score;{"\n"}
{"    "}{kw("public")} UserInfo() {"{}"} {cm("// required")}{"\n"}
{"}"}
      </CodeBlock> },
  { n: "02", t: "Declare the service", d: "Annotate the contract interface with @RmiService. Client stub and server dispatch are generated from this one definition.",
    code: <CodeBlock file="UserService.java" lang="SHARED">
{cy("@RmiService")}{"\n"}
{kw("public interface")} UserService {"{"}{"\n"}
{"    "}UserInfo getUserInfo(String username);{"\n"}
{"    "}{kw("void")} updateScore(String username, {kw("int")} newScore);{"\n"}
{"}"}
      </CodeBlock> },
  { n: "03", t: "Persist with EclipseStore", d: "No JPA, no SQL. Query with plain Java streams over the stored object graph, then store(user) to persist the delta. Saves are always explicit.",
    code: <CodeBlock file="UserServiceImpl.java" lang="SERVER">
{cy("@ApplicationScoped")}{"\n"}
{kw("public class")} UserServiceImpl {kw("implements")} UserService {"{"}{"\n"}
{"    "}{cy("@Inject")} EmbeddedStorageManager storage;{"\n\n"}
{"    "}{kw("public")} {kw("void")} updateScore(String user, {kw("int")} n) {"{"}{"\n"}
{"        "}UserInfo u = root().getUsers().stream(){"\n"}
{"            "}.filter(x -> x.getName().equals(user)){"\n"}
{"            "}.findFirst().orElseThrow();{"\n"}
{"        "}u.setScore(n);{"\n"}
{"        "}storage.store(u); {cm("// persist the delta")}{"\n"}
{"    "}{"}"}{"\n"}
{"}"}
      </CodeBlock> },
  { n: "04", t: "Run the Wasm client", d: "Register serializers, open the channel, obtain the generated stub, and invoke. The call suspends cooperatively until the response returns — the UI thread never blocks.",
    code: <CodeBlock file="ClientApplication.java" lang="CLIENT · WASM">
BinaryPackableRegistrar.registerAll();{"\n"}
{kw("var")} channel = {kw("new")} WasmRmiClientChannel({"\n"}
{"    "}{cy("\"ws://localhost:8080/wasm-rmi\"")}, () -> {"{}"});{"\n"}
WasmRmiClient.initialize(channel);{"\n\n"}
UserService service = {kw("new")} UserService_Stub();{"\n"}
UserInfo profile = service.getUserInfo({cy("\"franz\"")});
      </CodeBlock> }];

  return (
    <Section id="walkthrough">
      <div style={{ maxWidth: 760, marginBottom: 72 }}>
        <Eyebrow>03 · Code walkthrough</Eyebrow>
        <H2>One object, front end to <Serif>persistence.</Serif></H2>
      </div>
      <div style={{ display: "flex", flexDirection: "column", gap: 0 }}>
        {steps.map((s) =>
        <div key={s.n} style={{ display: "grid", gridTemplateColumns: "380px 1fr", gap: 64, padding: "48px 0", borderTop: "1px solid var(--color-steel)" }}>
            <div>
              <div style={{ fontFamily: "var(--font-mono)", fontSize: "var(--text-10)", color: "var(--color-brass)", letterSpacing: "var(--tracking-wide)", marginBottom: 16 }}>STEP {s.n}</div>
              <h3 style={{ fontFamily: "var(--font-sans)", fontWeight: 300, fontSize: "var(--text-28)", color: "var(--color-bone)", letterSpacing: "var(--tracking-tight)", margin: 0, lineHeight: 1.1 }}>{s.t}</h3>
              <p style={{ fontFamily: "var(--font-sans)", fontSize: "var(--text-14)", color: "var(--color-ash)", marginTop: 16, lineHeight: 1.6 }}>{s.d}</p>
            </div>
            <div>{s.code}</div>
          </div>
        )}
      </div>
    </Section>);

}

function ArchitectureBlock() {
  const cards = [
  { i: "AOT.01", t: "Annotation processing", b: "During Maven compilation, zeroz4j-apt scans for @DataModel and @RmiService, generating _Serializer and _Stub classes. No runtime reflection — which WebAssembly handles poorly." },
  { i: "AOT.02", t: "TeaVM WasmGC", b: "The client module transpiles Java bytecode — including generated stubs and serializers — directly into WasmGC to run in the browser." },
  { i: "AOT.03", t: "Cooperative coroutines", b: "Wasm runs on the single UI thread, so it cannot block. An RMI call suspends the coroutine, sends its frame, yields to the browser, and resumes exactly when the response arrives." },
  { i: "SRV.01", t: "Virtual threads (Loom)", b: "Each incoming WebSocket frame is handed to a virtual thread, so the server's I/O threads never block on long queries — thousands of persistent connections stay responsive." },
  { i: "SRV.02", t: "Modular backend", b: "zeroz4j-server-core is the agnostic CDI engine and RMI dispatcher; a binding such as zeroz4j-server-helidon supplies the HTTP/WebSocket transport." },
  { i: "SYNC.01", t: "Explicit LiveSync", b: "Subscriptions are explicit, never sync-everything: it protects bandwidth and browser memory, keeps the UI contextually scoped, and lets the server authorize each watched object." }];

  return (
    <Section id="arch" style={{ background: "var(--color-vault)" }}>
      <div style={{ maxWidth: 820, marginBottom: 64 }}>
        <Eyebrow>04 · Architecture</Eyebrow>
        <H2>Ahead-of-time by design, <Serif>reflection-free.</Serif></H2>
        <p style={{ fontFamily: "var(--font-sans)", fontSize: "var(--text-16)", color: "var(--color-ash)", marginTop: 24, lineHeight: 1.6, maxWidth: 640 }}>
          Performance in the browser is guaranteed at compile time, not hoped for at runtime.
        </p>
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", border: "1px solid var(--color-steel)", borderLeft: "none", borderTop: "none" }}>
        {cards.map((c) =>
        <div key={c.i} style={{ borderLeft: "1px solid var(--color-steel)", borderTop: "1px solid var(--color-steel)", display: "grid" }}>
            <DocsDS.Card title={c.t}>{c.b}</DocsDS.Card>
          </div>
        )}
      </div>
    </Section>);

}

function Docs() {
  return (
    <Page page="docs">
      <DocHeader />
      <GettingStarted />
      <Concepts />
      <Walkthrough />
      <ArchitectureBlock />
    </Page>);

}

ReactDOM.createRoot(document.getElementById("root")).render(<Docs />);
