let s = import "../schema.ncl" in

# Mode: typedialog-release
# Builds, packages, and announces a new typedialog release across all backends.
# Cross-compiles for Linux/macOS/Windows, generates SBOMs, creates distribution
# packages, runs the full CI pipeline, and publishes the ecosystem event.
#
# Required params:
#   {version}          — semver tag (e.g. "0.2.0")
#   {typedialog_dir}   — absolute path to the typedialog checkout
#   {ontoref_dir}  — absolute path to the ontoref checkout
{
  id      = "typedialog_release",
  trigger = "Cut a new typedialog release: build, cross-compile, package, and announce",

  preconditions = [
    "git is available in PATH and working tree is clean",
    "just is available in PATH",
    "cargo is available (with cross or cross-compilation toolchains)",
    "nickel is available for schema validation",
    "nats CLI available (publish_ecosystem_event continues on failure)",
    "{typedialog_dir}/justfile exists",
    "GITHUB_TOKEN set (or Gitea token for Forgejo releases)",
    "{version} matches semver pattern vMAJOR.MINOR.PATCH",
  ],

  steps = [
    {
      id         = "ci_full",
      action     = "run_full_ci_pipeline",
      actor      = 'Agent,
      cmd        = "just -f {typedialog_dir}/justfile ci::full",
      depends_on = [],
      on_error   = { strategy = 'Stop },
      note       = "Runs fmt, clippy -D warnings, all tests, cargo audit, cargo deny — must pass before any release artifact is produced",
    },
    {
      id         = "build_release",
      action     = "build_release_binaries",
      actor      = 'Agent,
      cmd        = "just -f {typedialog_dir}/justfile build::release",
      depends_on = [{ step = "ci_full", kind = 'OnSuccess }],
      on_error   = { strategy = 'Stop },
    },
    {
      id         = "cross_compile",
      action     = "cross_compile_all_targets",
      actor      = 'Agent,
      cmd        = "just -f {typedialog_dir}/justfile distro::cross",
      depends_on = [{ step = "build_release", kind = 'OnSuccess }],
      on_error   = { strategy = 'Continue },
      note       = "Cross-compiles for linux-x86_64, linux-aarch64, darwin-x86_64, darwin-aarch64, windows-x86_64; continues on partial failure",
    },
    {
      id         = "generate_sbom",
      action     = "generate_dependency_sbom",
      actor      = 'Agent,
      cmd        = "just -f {typedialog_dir}/justfile distro::generate-sbom",
      depends_on = [{ step = "build_release", kind = 'OnSuccess }],
      on_error   = { strategy = 'Continue },
      note       = "Generates SBOM.spdx.json (ISO/IEC 5962) and SBOM.cyclonedx.json (ECMA) for license compliance",
    },
    {
      id         = "create_package",
      action     = "create_distribution_package",
      actor      = 'Agent,
      cmd        = "just -f {typedialog_dir}/justfile distro::create-package",
      depends_on = [
        { step = "cross_compile",  kind = 'Always },
        { step = "generate_sbom",  kind = 'Always },
      ],
      on_error   = { strategy = 'Stop },
      note       = "Packages all binaries (typedialog, typedialog-tui, typedialog-web, typedialog-ai, typedialog-ag, typedialog-prov-gen), configs, and install scripts",
    },
    {
      id         = "create_checksums",
      action     = "generate_release_checksums",
      actor      = 'Agent,
      cmd        = "just -f {typedialog_dir}/justfile distro::create-checksums",
      depends_on = [{ step = "create_package", kind = 'OnSuccess }],
      on_error   = { strategy = 'Stop },
    },
    {
      id         = "tag_release",
      action     = "tag_git_release",
      actor      = 'Human,
      depends_on = [{ step = "create_checksums", kind = 'OnSuccess }],
      on_error   = { strategy = 'Stop },
      note       = "Human task: git tag -s v{version} -m 'release: v{version}' && git push origin v{version}",
      verify     = "git -C {typedialog_dir} tag --list 'v{version}'",
    },
    {
      id         = "publish_ecosystem_event",
      action     = "announce_release_on_nats",
      actor      = 'Agent,
      cmd        = "nats pub ecosystem.typedialog.release '{\"version\": \"{version}\", \"backends\": [\"cli\", \"tui\", \"web\", \"ai\", \"agent\", \"prov-gen\"]}'",
      depends_on = [{ step = "tag_release", kind = 'OnSuccess }],
      on_error   = { strategy = 'Continue },
      note       = "Announces the release to the ecosystem; stratum-orchestrator may trigger dependent workflows (e.g. provisioning schema updates)",
    },
  ],

  postconditions = [
    "Full CI pipeline passes for commit at {version}",
    "Release artifacts in {typedialog_dir}/target/release/ and distro packages",
    "SBOM.spdx.json and SBOM.cyclonedx.json regenerated",
    "Distribution package contains all 6 backend binaries and install scripts",
    "SHA256 checksums file created alongside the package",
    "git tag v{version} pushed to origin",
    "ecosystem.typedialog.release published to NATS (best-effort)",
  ],
} | (s.Mode String)
