; Blog app defpage declarations
; Pages kept as Python: home, index, post-detail (cache_page / complex branching)
; All helpers return data dicts — markup composition in SX.

; --- New post/page editors ---

(defpage new-post
  :path "/new/"
  :auth :admin
  :layout :blog
  :data (editor-data)
  :content (~editor/content
    :csrf csrf :title-placeholder title-placeholder
    :create-label create-label :css-href css-href
    :js-src js-src :sx-editor-js-src sx-editor-js-src
    :init-js init-js))

(defpage new-page
  :path "/new-page/"
  :auth :admin
  :layout :blog
  :data (editor-page-data)
  :content (~editor/content
    :csrf csrf :title-placeholder title-placeholder
    :create-label create-label :css-href css-href
    :js-src js-src :sx-editor-js-src sx-editor-js-src
    :init-js init-js))

; --- Post admin pages (absolute paths under /<slug>/admin/) ---

(defpage post-admin
  :path "/<slug>/admin/"
  :auth :admin
  :layout (:post-admin :selected "admin")
  :data (post-admin-data slug)
  :content (~admin/placeholder))

(defpage post-data
  :path "/<slug>/admin/data/"
  :auth :admin
  :layout (:post-admin :selected "data")
  :data (post-data-data slug)
  :content (~admin/data-table-content :tablename tablename :model-data model-data))

(defpage post-preview
  :path "/<slug>/admin/preview/"
  :auth :admin
  :layout (:post-admin :selected "preview")
  :data (post-preview-data slug)
  :content (~admin/preview-content
    :sx-pretty sx-pretty :json-pretty json-pretty
    :sx-rendered sx-rendered :lex-rendered lex-rendered))

(defpage post-entries
  :path "/<slug>/admin/entries/"
  :auth :admin
  :layout (:post-admin :selected "entries")
  :data (post-entries-data slug)
  :content (~settings/entries-browser-content
    :entries-panel (~settings/associated-entries-from-data :entries entries :csrf csrf)
    :calendars calendars))

(defpage post-settings
  :path "/<slug>/admin/settings/"
  :auth :post_author
  :layout (:post-admin :selected "settings")
  :data (post-settings-data slug)
  :content (~settings/form-content
    :csrf csrf :updated-at updated-at :is-page is-page
    :save-success save-success :slug settings-slug
    :published-at published-at :featured featured
    :visibility visibility :email-only email-only
    :tags tags :feature-image-alt feature-image-alt
    :meta-title meta-title :meta-description meta-description
    :canonical-url canonical-url :og-title og-title
    :og-description og-description :og-image og-image
    :twitter-title twitter-title :twitter-description twitter-description
    :twitter-image twitter-image :custom-template custom-template))

(defpage post-edit
  :path "/<slug>/admin/edit/"
  :auth :post_author
  :layout (:post-admin :selected "edit")
  :data (post-edit-data slug)
  :content (~editor/edit-content
    :csrf csrf :updated-at updated-at
    :title-val title-val :excerpt-val excerpt-val
    :feature-image feature-image :feature-image-caption feature-image-caption
    :sx-content-val sx-content-val :lexical-json lexical-json
    :has-sx has-sx :title-placeholder title-placeholder
    :status status :already-emailed already-emailed
    :newsletter-options (<>
      (option :value "" "Select newsletter\u2026")
      (map (fn (nl) (option :value (get nl "slug") (get nl "name"))) newsletters))
    :footer-extra (when badges
      (<> (map (fn (b) (span :class (get b "cls") (get b "text"))) badges)))
    :css-href css-href :js-src js-src
    :sx-editor-js-src sx-editor-js-src
    :init-js init-js :save-error save-error))

; --- Settings pages (absolute paths) ---

(defpage settings-home
  :path "/settings/"
  :auth :admin
  :layout :blog-settings
  :content (div :class "max-w-2xl mx-auto px-4 py-6"))

(defpage cache-page
  :path "/settings/cache/"
  :auth :admin
  :layout :blog-cache
  :data (service "blog-page" "cache-data")
  :content (~admin/cache-panel :clear-url clear-url :csrf csrf))

; --- Snippets ---

(defpage snippets-page
  :path "/settings/snippets/"
  :auth :login
  :layout :blog-snippets
  :data (service "blog-page" "snippets-data")
  :content (~admin/snippets-content
    :snippets snippets :is-admin is-admin :csrf csrf))

; --- Menu Items ---

(defpage menu-items-page
  :path "/settings/menu_items/"
  :auth :admin
  :layout :blog-menu-items
  :data (service "blog-page" "menu-items-data")
  :content (~admin/menu-items-content
    :menu-items menu-items :new-url new-url :csrf csrf))

; --- Tag Groups ---

(defpage tag-groups-page
  :path "/settings/tag-groups/"
  :auth :admin
  :layout :blog-tag-groups
  :data (service "blog-page" "tag-groups-data")
  :content (~admin/tag-groups-content
    :groups groups :unassigned-tags unassigned-tags
    :create-url create-url :csrf csrf))

(defpage tag-group-edit
  :path "/settings/tag-groups/<int:id>/"
  :auth :admin
  :layout :blog-tag-group-edit
  :data (service "blog-page" "tag-group-edit-data" :id id)
  :content (~admin/tag-group-edit-content
    :group group :all-tags all-tags
    :save-url save-url :delete-url delete-url :csrf csrf))
