;; Geography index — architecture overview
;; Describes the rendering pipeline: OCaml evaluator → wire formats → client

(defcomp () :affinity :server
  (div (~tw :tokens "max-w-4xl mx-auto px-6 pb-8 pt-4")
    (h2 (~tw :tokens "text-3xl font-bold text-stone-800 mb-4") "Geography")
    (p (~tw :tokens "text-lg text-stone-600 mb-8")
      "Where code runs and how it gets there. Geography maps the rendering pipeline from server-side evaluation through wire formats to client-side hydration.")

    ;; Architecture diagram
    (div (~tw :tokens "bg-stone-50 border border-stone-200 rounded-lg p-6 mb-8")
      (h3 (~tw :tokens "text-xl font-semibold text-stone-700 mb-4") "Rendering Pipeline")
      (div (~tw :tokens "space-y-4")
        ;; Server
        (div (~tw :tokens "flex items-start gap-4")
          (div (~tw :tokens "w-28 shrink-0 font-mono text-sm font-semibold text-sky-700 bg-sky-50 rounded px-2 py-1 text-center") "OCaml kernel")
          (div
            (p (~tw :tokens "text-stone-700")
              "The evaluator is a CEK machine written in SX and bootstrapped to OCaml. It evaluates page definitions, expands components, resolves IO (helpers, queries), and serializes the result as SX wire format.")
            (p (~tw :tokens "text-sm text-stone-500 mt-1")
              "spec/evaluator.sx → hosts/ocaml/ → aser-slot with batch IO")))

        ;; Wire format
        (div (~tw :tokens "flex items-start gap-4")
          (div (~tw :tokens "w-28 shrink-0 font-mono text-sm font-semibold text-amber-700 bg-amber-50 rounded px-2 py-1 text-center") "Wire format")
          (div
            (p (~tw :tokens "text-stone-700")
              "The aser (async-serialize) mode produces SX text — HTML tags and component calls serialized as s-expressions. Components with server affinity are expanded; client components stay as calls. The wire format is placed in a "
              (code (~tw :tokens "text-sm") "<script type=\"text/sx\">")
              " tag inside the HTML shell.")
            (p (~tw :tokens "text-sm text-stone-500 mt-1")
              "web/adapter-sx.sx → SxExpr values pass through serialize unquoted")))

        ;; Client
        (div (~tw :tokens "flex items-start gap-4")
          (div (~tw :tokens "w-28 shrink-0 font-mono text-sm font-semibold text-emerald-700 bg-emerald-50 rounded px-2 py-1 text-center") "sx-browser.js")
          (div
            (p (~tw :tokens "text-stone-700")
              "The client engine parses the SX wire format, evaluates component definitions, renders the DOM, and hydrates reactive islands. It includes the same CEK evaluator (transpiled from the spec), the parser, all web adapters, and the orchestration layer for fetch/swap/polling.")
            (p (~tw :tokens "text-sm text-stone-500 mt-1")
              "spec/ + web/ → hosts/javascript/cli.py → sx-browser.js (~400KB)")))))

    ;; What lives where
    (h3 (~tw :tokens "text-xl font-semibold text-stone-700 mb-4 mt-8") "What lives where")

    (div (~tw :tokens "grid md:grid-cols-2 gap-4 mb-8")
      ;; Spec
      (div (~tw :tokens "border border-stone-200 rounded-lg p-4")
        (h4 (~tw :tokens "font-semibold text-stone-700 mb-2") "Spec (shared)")
        (p (~tw :tokens "text-sm text-stone-600 mb-2") "The canonical SX language, bootstrapped identically to OCaml, JavaScript, and Python:")
        (ul (~tw :tokens "text-sm text-stone-600 space-y-1 list-disc ml-4")
          (li "CEK evaluator — frames, step function, call dispatch")
          (li "Parser — tokenizer, s-expression reader, serializer")
          (li "Primitives — ~80 built-in pure functions")
          (li "Render modes — HTML, SX wire, DOM")))

      ;; Web adapters
      (div (~tw :tokens "border border-stone-200 rounded-lg p-4")
        (h4 (~tw :tokens "font-semibold text-stone-700 mb-2") "Web Adapters")
        (p (~tw :tokens "text-sm text-stone-600 mb-2") "SX-defined modules that run on both server and client:")
        (ul (~tw :tokens "text-sm text-stone-600 space-y-1 list-disc ml-4")
          (li "adapter-sx.sx — aser wire format (server component expansion)")
          (li "adapter-html.sx — server HTML rendering")
          (li "adapter-dom.sx — client DOM rendering")
          (li "orchestration.sx — fetch, swap, polling, navigation")
          (li "engine.sx — trigger parsing, request building")))

      ;; OCaml kernel
      (div (~tw :tokens "border border-stone-200 rounded-lg p-4")
        (h4 (~tw :tokens "font-semibold text-stone-700 mb-2") "OCaml Kernel (server)")
        (p (~tw :tokens "text-sm text-stone-600 mb-2") "Persistent process connected via a binary pipe protocol:")
        (ul (~tw :tokens "text-sm text-stone-600 space-y-1 list-disc ml-4")
          (li "CEK evaluator + VM bytecode compiler")
          (li "Batch IO bridge — defers helper/query calls to Python")
          (li "Length-prefixed blob protocol — no string escaping")
          (li "Component hot-reload on .sx file changes")))

      ;; sx-browser.js
      (div (~tw :tokens "border border-stone-200 rounded-lg p-4")
        (h4 (~tw :tokens "font-semibold text-stone-700 mb-2") "sx-browser.js (client)")
        (p (~tw :tokens "text-sm text-stone-600 mb-2") "Single JS bundle transpiled from spec + web adapters:")
        (ul (~tw :tokens "text-sm text-stone-600 space-y-1 list-disc ml-4")
          (li "Parses SX wire format from script tags")
          (li "Renders component trees to DOM")
          (li "Hydrates reactive islands (signals, effects)")
          (li "Client-side routing with defpage")
          (li "HTMX-like fetch/swap orchestration"))))

    ;; Rendering modes
    (h3 (~tw :tokens "text-xl font-semibold text-stone-700 mb-4 mt-8") "Rendering Modes")

    (div (~tw :tokens "overflow-x-auto mb-8")
      (table (~tw :tokens "w-full text-sm")
        (thead
          (tr (~tw :tokens "border-b border-stone-200")
            (th (~tw :tokens "px-3 py-2 text-left font-medium text-stone-600") "Mode")
            (th (~tw :tokens "px-3 py-2 text-left font-medium text-stone-600") "Runs on")
            (th (~tw :tokens "px-3 py-2 text-left font-medium text-stone-600") "Components")
            (th (~tw :tokens "px-3 py-2 text-left font-medium text-stone-600") "Output")))
        (tbody
          (tr (~tw :tokens "border-b border-stone-100")
            (td (~tw :tokens "px-3 py-2 font-mono") "render-to-html")
            (td (~tw :tokens "px-3 py-2") "Server (OCaml)")
            (td (~tw :tokens "px-3 py-2") "Expanded recursively")
            (td (~tw :tokens "px-3 py-2") "HTML string"))
          (tr (~tw :tokens "border-b border-stone-100")
            (td (~tw :tokens "px-3 py-2 font-mono") "aser / aser-slot")
            (td (~tw :tokens "px-3 py-2") "Server (OCaml)")
            (td (~tw :tokens "px-3 py-2") "Server-affinity expanded; client preserved")
            (td (~tw :tokens "px-3 py-2") "SX wire format"))
          (tr (~tw :tokens "border-b border-stone-100")
            (td (~tw :tokens "px-3 py-2 font-mono") "render-to-dom")
            (td (~tw :tokens "px-3 py-2") "Client (sx-browser.js)")
            (td (~tw :tokens "px-3 py-2") "Expanded recursively")
            (td (~tw :tokens "px-3 py-2") "DOM nodes"))
          (tr (~tw :tokens "border-b border-stone-100")
            (td (~tw :tokens "px-3 py-2 font-mono") "client routing")
            (td (~tw :tokens "px-3 py-2") "Client (sx-browser.js)")
            (td (~tw :tokens "px-3 py-2") "defpage content evaluated locally")
            (td (~tw :tokens "px-3 py-2") "DOM swap")))))

    ;; Topics
    (h3 (~tw :tokens "text-xl font-semibold text-stone-700 mb-4 mt-8") "Topics")

    (div (~tw :tokens "grid md:grid-cols-2 gap-4")
      (a :href "/sx/(geography.(hypermedia))" (~tw :tokens "block border border-stone-200 rounded-lg p-4 hover:border-sky-300 hover:bg-sky-50 transition-colors")
        (h4 (~tw :tokens "font-semibold text-stone-700") "Hypermedia Lakes")
        (p (~tw :tokens "text-sm text-stone-500") "Server-driven UI with sx-get/post/put/delete — fetch, swap, and the request lifecycle."))

      (a :href "/sx/(geography.(reactive))" (~tw :tokens "block border border-stone-200 rounded-lg p-4 hover:border-sky-300 hover:bg-sky-50 transition-colors")
        (h4 (~tw :tokens "font-semibold text-stone-700") "Reactive Islands")
        (p (~tw :tokens "text-sm text-stone-500") "Client-side signals and effects hydrated from server-rendered HTML. defisland, deref, lakes."))

      (a :href "/sx/(geography.(marshes))" (~tw :tokens "block border border-stone-200 rounded-lg p-4 hover:border-sky-300 hover:bg-sky-50 transition-colors")
        (h4 (~tw :tokens "font-semibold text-stone-700") "Marshes")
        (p (~tw :tokens "text-sm text-stone-500") "Where reactivity and hypermedia interpenetrate — server writes to signals, reactive views reshape server content."))

      (a :href "/sx/(geography.(scopes))" (~tw :tokens "block border border-stone-200 rounded-lg p-4 hover:border-sky-300 hover:bg-sky-50 transition-colors")
        (h4 (~tw :tokens "font-semibold text-stone-700") "Scopes")
        (p (~tw :tokens "text-sm text-stone-500") "Render-time dynamic scope — the primitive beneath provide, collect!, spreads, and islands."))

      (a :href "/sx/(geography.(cek))" (~tw :tokens "block border border-stone-200 rounded-lg p-4 hover:border-sky-300 hover:bg-sky-50 transition-colors")
        (h4 (~tw :tokens "font-semibold text-stone-700") "CEK Machine")
        (p (~tw :tokens "text-sm text-stone-500") "The evaluator internals — frames, continuations, tail-call optimization, and the VM bytecode compiler."))

      (a :href "/sx/(geography.(isomorphism))" (~tw :tokens "block border border-stone-200 rounded-lg p-4 hover:border-sky-300 hover:bg-sky-50 transition-colors")
        (h4 (~tw :tokens "font-semibold text-stone-700") "Isomorphism")
        (p (~tw :tokens "text-sm text-stone-500") "One spec, multiple hosts — how the same SX code runs on OCaml, JavaScript, and Python.")))))
