;; Data test page — exercises Phase 4 client-side data rendering + caching.
;;
;; This page has a :data expression. When navigated to:
;; - Full page load: server evaluates data + renders content (normal path)
;; - Client route (1st): client fetches /sx/data/data-test, caches, renders
;; - Client route (2nd within 30s): client uses cached data, renders instantly
;;
;; Open browser console and look for:
;;   "sx:route client+data"  — cache miss, fetched from server
;;   "sx:route client+cache" — cache hit, rendered from cached data

(defcomp (&key (server-time :as string) (items :as list) (phase :as string) (transport :as string))
  (div (~tw :tokens "space-y-8")
    (div (~tw :tokens "border-b border-stone-200 pb-6")
      (h1 (~tw :tokens "text-2xl font-bold text-stone-900") "Data Test")
      (p (~tw :tokens "mt-2 text-stone-600")
        "This page tests the Phase 4 data endpoint and client-side data cache. "
        "The content you see was rendered using data from the server, but the "
        "rendering itself may have happened client-side."))

    ;; Server-provided metadata
    (div (~tw :tokens "rounded-lg border border-stone-200 bg-white p-6 space-y-3")
      (h2 (~tw :tokens "text-lg font-semibold text-stone-800") "Data from server")
      (dl (~tw :tokens "grid grid-cols-2 gap-2 text-sm")
        (dt (~tw :tokens "font-medium text-stone-600") "Phase")
        (dd (~tw :tokens "text-stone-900") phase)
        (dt (~tw :tokens "font-medium text-stone-600") "Transport")
        (dd (~tw :tokens "text-stone-900") transport)
        (dt (~tw :tokens "font-medium text-stone-600") "Server time")
        (dd (~tw :tokens "font-mono text-stone-900") server-time)))

    ;; Pipeline steps from data
    (div (~tw :tokens "space-y-3")
      (h2 (~tw :tokens "text-lg font-semibold text-stone-800") "Pipeline steps")
      (div (~tw :tokens "space-y-2")
        (map-indexed
          (fn (i item)
            (div (~tw :tokens "flex items-start gap-3 rounded border border-stone-100 bg-white p-3")
              (span (~tw :tokens "flex-none rounded-full bg-violet-100 text-violet-700 w-6 h-6 flex items-center justify-center text-xs font-bold")
                (str (+ i 1)))
              (div
                (div (~tw :tokens "font-medium text-stone-900") (get item "label"))
                (div (~tw :tokens "text-sm text-stone-500") (get item "detail")))))
          items)))

    ;; How to verify — updated with cache instructions
    (div (~tw :tokens "rounded-lg border border-amber-200 bg-amber-50 p-4 text-sm space-y-2")
      (p (~tw :tokens "font-semibold text-amber-800") "How to verify client-side rendering + caching")
      (ol (~tw :tokens "list-decimal list-inside text-amber-700 space-y-1")
        (li "Open the browser console (F12)")
        (li "Navigate to this page from another page using a link")
        (li "Look for: " (code (~tw :tokens "bg-amber-100 px-1 rounded") "sx:route client+data /isomorphism/data-test"))
        (li "Navigate away, then back within 30 seconds")
        (li "Look for: " (code (~tw :tokens "bg-amber-100 px-1 rounded") "sx:route client+cache /isomorphism/data-test"))
        (li "The server-time value should be the same (cached data)")
        (li "Wait 30+ seconds, navigate back again — new fetch, updated time")))))
