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

# Mode: new_project
# Creates a new project in the ecosystem with ontology, kogral, syntaxis, and NATS wiring.
#
# Required params (substituted in cmd via {param}):
#   {project_name}     — identifier used for kogral init and syntaxis project create
#   {project_dir}      — absolute path where the repository is created
#   {ontoref_dir}  — absolute path to the ontoref checkout (for ontology template)
{
  id      = "new_project",
  trigger = "Initialize a new project in the ontoref ecosystem",

  preconditions = [
    "git is available in PATH",
    "nickel is available in PATH",
    "{project_dir} parent directory exists and is writable",
    "kogral CLI available (optional — init_kogral continues on failure)",
    "syntaxis CLI available (optional — create_syntaxis_project continues on failure)",
    "nats CLI available (optional — configure_nats and publish_ecosystem_event continue on failure)",
    "{ontoref_dir}/.ontology/ or templates/ontology/ exists",
  ],

  steps = [
    {
      id         = "init_repo",
      action     = "initialize_git_repository",
      actor      = 'Both,
      cmd        = "nu {ontoref_dir}/reflection/bin/init-repo.nu {project_dir}",
      depends_on = [],
      on_error   = { strategy = 'Stop },
    },
    {
      id         = "copy_ontology_template",
      action     = "scaffold_ontology_directory",
      actor      = 'Agent,
      cmd        = "cp -r {ontoref_dir}/templates/ontology {project_dir}/.ontology",
      depends_on = [{ step = "init_repo", kind = 'OnSuccess }],
      on_error   = { strategy = 'Stop },
      note       = "Copies core.ncl, state.ncl, gate.ncl stubs — project owner fills in axioms",
    },
    {
      id         = "init_kogral",
      action     = "initialize_kogral_graph",
      actor      = 'Agent,
      cmd        = "kogral init --name {project_name} --dir {project_dir}/.kogral",
      depends_on = [{ step = "init_repo", kind = 'OnSuccess }],
      on_error   = { strategy = 'Continue },
      note       = "kogral is optional — ecosystem functions without it; execution continues on failure",
    },
    {
      id         = "create_syntaxis_project",
      action     = "register_project_in_syntaxis",
      actor      = 'Agent,
      cmd        = "syntaxis project create --name {project_name} --path {project_dir}",
      depends_on = [{ step = "init_repo", kind = 'OnSuccess }],
      on_error   = { strategy = 'Continue },
      note       = "syntaxis is optional — execution continues on failure",
    },
    {
      id         = "configure_nats",
      action     = "create_nats_consumer_for_project",
      actor      = 'Agent,
      cmd        = "^nats stream add ECOSYSTEM --subjects 'ecosystem.{project_name}.>' --no-headers-only --defaults err> /dev/null",
      depends_on = [{ step = "init_repo", kind = 'OnSuccess }],
      on_error   = { strategy = 'Continue },
      note       = "NATS stream creation is idempotent and optional",
    },
    {
      id     = "publish_ecosystem_event",
      action = "announce_project_created",
      actor  = 'Agent,
      cmd    = "nats pub ecosystem.project.created '{\"project_name\": \"{project_name}\", \"project_dir\": \"{project_dir}\"}'",
      depends_on = [
        { step = "copy_ontology_template",   kind = 'OnSuccess },
        { step = "init_kogral",              kind = 'Always },
        { step = "create_syntaxis_project",  kind = 'Always },
        { step = "configure_nats",           kind = 'Always },
      ],
      on_error = { strategy = 'Continue },
      note     = "Final announcement — best-effort, does not block project creation",
    },
  ],

  postconditions = [
    "{project_dir} is an initialized git repository",
    "{project_dir}/.ontology/ contains core.ncl, state.ncl, gate.ncl from templates",
    "ecosystem.project.created published to NATS (best-effort)",
  ],
} | (s.Mode String)
