(defisland
  ()
  (let
    ((count (signal 0))
      (name (signal "world"))
      (cid-display (signal ""))
      (cid-input (signal ""))
      (cid-history (signal (list)))
      (status (signal "")))
    (freeze-scope
      "ca-demo"
      (fn () (freeze-signal "count" count) (freeze-signal "name" name)))
    (div
      (~tw :tokens "space-y-4")
      (div
        (~tw :tokens "flex gap-4 items-center")
        (div
          (~tw :tokens "flex items-center gap-2")
          (button
            :on-click (fn (e) (swap! count dec))
            (~tw
              :tokens "px-2 py-1 rounded bg-stone-200 text-stone-700 hover:bg-stone-300")
            "-")
          (span
            (~tw :tokens "font-mono text-lg font-bold w-8 text-center")
            (deref count))
          (button
            :on-click (fn (e) (swap! count inc))
            (~tw
              :tokens "px-2 py-1 rounded bg-stone-200 text-stone-700 hover:bg-stone-300")
            "+"))
        (input
          :type "text"
          :bind name
          (~tw
            :tokens "px-3 py-1 rounded border border-stone-300 font-mono text-sm")))
      (div
        (~tw
          :tokens "rounded bg-violet-50 border border-violet-200 p-3 text-violet-800")
        "Hello, "
        (deref name)
        "! Count = "
        (deref count))
      (div
        (~tw :tokens "flex gap-2")
        (button
          :on-click (fn
            (e)
            (let
              ((cid (freeze-to-cid "ca-demo")))
              (reset! cid-display cid)
              (reset! status (str "Stored as " cid))
              (swap! cid-history (fn (h) (append h (list cid))))))
          (~tw
            :tokens "px-3 py-1.5 rounded bg-stone-700 text-white text-sm hover:bg-stone-800")
          "Content-address"))
      (when
        (not (empty? (deref cid-display)))
        (div
          (~tw
            :tokens "font-mono text-sm bg-stone-50 rounded p-2 flex items-center gap-2")
          (span (~tw :tokens "text-stone-400") "CID:")
          (span
            (~tw :tokens "text-violet-700 font-bold")
            (deref cid-display))))
      (when
        (not (empty? (deref status)))
        (div (~tw :tokens "text-xs text-emerald-600") (deref status)))
      (div
        (~tw :tokens "flex gap-2 items-end")
        (div
          (~tw :tokens "flex-1")
          (label
            (~tw :tokens "text-xs text-stone-400 block mb-1")
            "Restore from CID")
          (input
            :type "text"
            :bind cid-input
            (~tw
              :tokens "w-full px-3 py-1 rounded border border-stone-300 font-mono text-sm")))
        (button
          :on-click (fn
            (e)
            (if
              (thaw-from-cid (deref cid-input))
              (reset! status (str "Restored from " (deref cid-input)))
              (reset! status (str "CID not found: " (deref cid-input)))))
          (~tw
            :tokens "px-3 py-1.5 rounded bg-emerald-600 text-white text-sm hover:bg-emerald-700")
          "Restore"))
      (when
        (not (empty? (deref cid-history)))
        (div
          (~tw :tokens "space-y-1")
          (label (~tw :tokens "text-xs text-stone-400 block") "History")
          (map
            (fn
              (cid)
              (button
                :on-click (fn
                  (e)
                  (if
                    (thaw-from-cid cid)
                    (reset! status (str "Restored from " cid))
                    (reset! status (str "CID not found: " cid))))
                (~tw
                  :tokens "block w-full text-left px-2 py-1 rounded bg-stone-50 hover:bg-stone-100 font-mono text-xs text-stone-600")
                cid))
            (deref cid-history)))))))
