;; Extension — document metadata (title/slug/tags) + Ghost title plumbing.

(st-bootstrap-classes!)
(content/bootstrap!)

(define d (doc-empty "post"))

;; ── defaults ──
(content-test "default title nil" (doc-title d) nil)
(content-test "default slug nil" (doc-slug d) nil)
(content-test "default tags empty" (doc-tags d) (list))

;; ── copy-on-write setters ──
(define d2 (doc-with-title d "Hello World"))
(content-test "with-title" (doc-title d2) "Hello World")
(content-test "with-title immutable" (doc-title d) nil)
(content-test "with-title keeps id" (doc-id d2) "post")

(define d3 (doc-with-slug (doc-with-title d "T") "my-slug"))
(content-test "with-slug" (doc-slug d3) "my-slug")
(content-test "title preserved with slug" (doc-title d3) "T")

(define d4 (doc-with-tags d (list "a" "b")))
(content-test "with-tags" (doc-tags d4) (list "a" "b"))
(content-test "add-tag" (doc-tags (doc-add-tag d4 "c")) (list "a" "b" "c"))
(content-test
  "add-tag from empty"
  (doc-tags (doc-add-tag d "x"))
  (list "x"))

;; ── batch + dict ──
(define d5 (doc-with-meta d {:slug "s" :title "T" :tags (list "t1")}))
(content-test "with-meta title" (doc-title d5) "T")
(content-test "with-meta slug" (doc-slug d5) "s")
(content-test "with-meta tags" (doc-tags d5) (list "t1"))
(content-test
  "with-meta partial leaves title"
  (doc-title (doc-with-meta d {:slug "only"}))
  nil)
(content-test "doc-meta dict" (doc-meta d5) {:slug "s" :id "post" :title "T" :tags (list "t1")})

;; ── constructor with metadata ──
(define d6 (doc-new-meta "p2" (list (mk-text "x" "hi")) {:title "Post 2"}))
(content-test "new-meta title" (doc-title d6) "Post 2")
(content-test "new-meta blocks" (doc-ids d6) (list "x"))

;; ── facade aliases ──
(content-test "content/title" (content/title d5) "T")
(content-test
  "content/with-title"
  (content/title (content/with-title d "Z"))
  "Z")
(content-test "content/meta" (content/meta d5) (doc-meta d5))

;; ── metadata coexists with block ops ──
(define
  d7
  (doc-append
    (doc-with-title (doc-empty "x") "Titled")
    (mk-text "p" "body")))
(content-test "meta + blocks coexist" (doc-ids d7) (list "p"))
(content-test "meta survives append" (doc-title d7) "Titled")
(content-test
  "meta survives edit"
  (doc-title (doc-update d7 "p" "text" "changed"))
  "Titled")

;; ── Ghost adapter now carries title ──
(define post {:sections (list {:id "h" :text "Hi" :kind "heading" :level 1}) :title "My Post"})
(define gd (content/import ghost-adapter post "post"))
(content-test "ghost import title" (doc-title gd) "My Post")
(content-test
  "ghost export title"
  (get (content/export ghost-adapter gd) :title)
  "My Post")
(content-test
  "ghost title round-trip"
  (doc-title (content/round-trip ghost-adapter gd))
  "My Post")
