-- allium: 1
-- bDS Script Editor View
-- Scope: UI content area — script editing surface
-- Distilled from: ScriptEditor.tsx

-- Describes the layout and behaviour of the script editor.
-- See script.allium for entity model.

use "./script.allium" as script
use "./i18n.allium" as i18n

-- ─── Script editor view ──────────────────────────────────────

value ScriptEditorView {
    script_id: String
    title: String                       -- editable text input
    slug: String                        -- editable text input, auto-generated from title
    kind: String                        -- select: utility | macro | transform
    entrypoint: String                  -- select: discovered Lua functions available as entrypoints
    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 ScriptEditorSurface {
    context editor: ScriptEditorView

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

    provides:
        ScriptSaveRequested(editor.script_id)
        ScriptPublishRequested(editor.script_id)
            when editor.can_publish
        ScriptRunRequested(editor.script_id)
        ScriptCheckSyntaxRequested(editor.script_id)
        ScriptDeleteRequested(editor.script_id)
        ScriptDeleteConfirmed(editor.script_id)

    @guarantee HeaderLayout
        -- Header bar with script title tab.
        -- Actions (right side): Save button, Publish button (shown only when
        --   can_publish, i.e. status = draft), Run button,
        --   Check Syntax 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: utility/macro/transform),
        --   Entrypoint (select: discovered Lua functions exposed by the script),
        --   Enabled (checkbox).

    @guarantee EditorBody
        -- Code editor with Lua syntax highlighting.
        -- Toolbar: "Content" label.
        -- Syntax errors shown as inline markers in editor gutter.

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

-- ─── Script editor actions ──────────────────────────────────

rule ScriptSave {
    when: ScriptSaveRequested(script_id)
    let target = script/Script{id: script_id}
    let syntax = ValidateScript(editor_content(script_id))
        -- editor_content: the current code editor buffer
    ensures:
        if syntax = valid:
            UpdateScriptRequested(target, editor_field_changes(script_id))
            -- Version bump + DB write + published-file rewrite:
            -- script.allium UpdateScript. The entrypoint select is
            -- re-populated from functions discovered in the Lua source.
        else:
            ScriptSyntaxErrorsShown(script_id, syntax)
            -- Save blocked; errors shown inline in the editor gutter
}

rule ScriptPublish {
    when: ScriptPublishRequested(script_id)
    let target = script/Script{id: script_id}
    requires: target.status = draft
        -- can_publish gate on the button
    let syntax = ValidateScript(editor_content(script_id))
    ensures:
        if syntax = valid:
            UpdateScriptRequested(target, editor_field_changes(script_id))
            PublishScriptRequested(target)
            -- status -> published + file write: script.allium PublishScript
        else:
            ScriptSyntaxErrorsShown(script_id, syntax)
}

rule ScriptCheckSyntax {
    when: ScriptCheckSyntaxRequested(script_id)
    -- Validation only, never saves
    let syntax = ValidateScript(editor_content(script_id))
    ensures:
        if syntax = valid:
            ScriptSyntaxOkShown(script_id)
            -- toast / status indicator
        else:
            ScriptSyntaxErrorsShown(script_id, syntax)
}

rule ScriptRun {
    when: ScriptRunRequested(script_id)
    let target = script/Script{id: script_id}
    ensures: ExecuteScriptRequested(target, editor_entrypoint(script_id),
        empty_args(), output_panel_sink())
        -- Runs the current buffer's entrypoint in the sandboxed Lua
        -- runtime (script.allium ScriptRuntimeSurface)
    ensures: OutputPanelOpened()
        -- stdout/stderr and errors (with line numbers) stream to the
        -- Output panel tab, which auto-opens when not visible
}

rule ScriptDeleteAction {
    when: ScriptDeleteRequested(script_id)
    let target = script/Script{id: script_id}
    ensures: ConfirmDeleteModalOpened(script_id)
        -- custom modal (not a native dialog): script title, no
        -- reference list; Cancel + Delete (destructive red style)
        -- — action_patterns.allium
        -- confirmation_assignments: script_delete
}

rule ScriptDeleteExecute {
    when: ScriptDeleteConfirmed(script_id)
    let target = script/Script{id: script_id}
    ensures: DeleteScriptRequested(target)
        -- DB record + published script file: script.allium DeleteScript
    ensures: closeTab(script_id)
        -- sidebar removes the item reactively
}
