-- allium: 1
-- bDS Template Editor View
-- Scope: UI content area — template editing surface
-- Distilled from: TemplateEditor.tsx

-- Describes the layout and behaviour of the template editor.
-- See template.allium for entity model and Liquid subset.

use "./template.allium" as template
use "./i18n.allium" as i18n

-- ─── Template editor view ─────────────────────────────────────

value TemplateEditorView {
    template_id: String
    title: String                       -- editable text input
    slug: String                        -- editable text input, auto-generated from title
    kind: String                        -- select: post | list | not_found | partial
    enabled: Boolean                    -- checkbox
    content: String                     -- code editor content
    can_publish: Boolean                -- true while status = draft
    created_at: String                  -- locale-formatted date
    updated_at: String                  -- locale-formatted date
}

surface TemplateEditorSurface {
    context editor: TemplateEditorView

    exposes:
        editor.title
        editor.slug
        editor.kind
        editor.enabled
        editor.created_at
        editor.updated_at

    provides:
        TemplateSaveRequested(editor.template_id)
        TemplatePublishRequested(editor.template_id)
            when editor.can_publish
        TemplateValidateRequested(editor.template_id)
        TemplateDeleteRequested(editor.template_id)

    @guarantee HeaderLayout
        -- Header bar with template title tab.
        -- Actions (right side): Save button, Publish button (shown only when
        --   can_publish, i.e. status = draft), Validate button,
        --   Delete button (danger style).

    @guarantee MetadataRow
        -- Two rows of metadata fields above the editor.
        -- Row 1: Title (text input), Slug (text input, auto-generated from title).
        -- Row 2: Kind (select: post/list/not-found/partial), Enabled (checkbox).

    @guarantee EditorBody
        -- Code editor with HTML/Liquid syntax highlighting.
        -- Toolbar: "Content" label.
        -- Syntax errors shown inline in editor on validation.

    @guarantee FooterLayout
        -- Created date and Updated date, locale-formatted.
}

-- ─── Template editor actions ────────────────────────────────

rule TemplateSave {
    when: TemplateSaveRequested(template_id)
    -- Liquid validation first (parse check for syntax errors)
    -- If invalid: blocks save, shows error inline in editor
    -- If valid: bumps version, saves to DB + rewrites .liquid file
    -- See engine_side_effects.allium UpdateTemplateSideEffects
}

rule TemplatePublish {
    when: TemplatePublishRequested(template_id)
    -- Only offered while the template is a draft (can_publish gate on the button)
    -- Validates Liquid first (publish gate); invalid source blocks publish
    -- Saves current draft fields, then publishes (status -> published)
    -- Writes the published .liquid file to disk
    -- See template.allium PublishTemplate
}

rule TemplateValidate {
    when: TemplateValidateRequested(template_id)
    -- Validates Liquid syntax without saving
    -- Shows errors inline in editor
    -- Success shown as toast or status indicator
}

rule TemplateDelete {
    when: TemplateDeleteRequested(template_id)
    -- Checks for references: posts using this template, tags with postTemplateSlug
    -- If references exist: system confirm dialog
    --   "This template is used by N posts and M tags. Force delete?"
    --   Force delete: nulls templateSlug on referencing posts,
    --     nulls postTemplateSlug on referencing tags
    -- If no references: deletes without confirmation
    -- Deletes DB record + .liquid file on disk
    -- Closes template tab, sidebar removes item
}
