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

# Mode: new_service
# Creates a new Rust microservice in the ecosystem: ontology, kogral, syntaxis,
# NATS subject definition, infra provisioning, and ecosystem registration.
#
# Required params:
#   {project_name}     — service identifier (used in NATS subjects, syntaxis, kogral)
#   {project_dir}      — absolute path for the new service repository
#   {ontoref_dir}  — absolute path to the ontoref checkout
#   {stack}            — technology stack identifier (e.g. "rust-axum", "rust-tonic")
{
  id      = "new_service",
  trigger = "Create a new Rust microservice in the ontoref ecosystem",

  preconditions = [
    "git is available in PATH",
    "nickel is available in PATH",
    "{project_dir} parent directory exists and is writable",
    "nats CLI available (define_nats_subjects continues on failure)",
    "kogral CLI available (init_kogral continues on failure)",
    "syntaxis CLI available (create_syntaxis_project continues on failure)",
    "provisioning system reachable (apply_infra continues on failure)",
    "{ontoref_dir}/templates/ontology/ exists",
  ],

  steps = [
    {
      id         = "init_repo",
      action     = "initialize_git_repository",
      actor      = 'Both,
      cmd        = "git -C {project_dir} init && git -C {project_dir} commit --allow-empty -m 'chore: initial commit'",
      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       = "Ontology is required for gate checks before feature acceptance",
    },
    {
      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 },
    },
    {
      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 },
    },
    {
      id         = "define_nats_subjects",
      action     = "declare_service_nats_subjects",
      actor      = 'Both,
      cmd        = "^nats stream add {project_name} --subjects '{project_name}.>' --defaults err> /dev/null",
      depends_on = [{ step = "init_repo", kind = 'OnSuccess }],
      on_error   = { strategy = 'Continue },
      note       = "NATS subjects must be defined before infra provisioning requests them",
    },
    {
      id         = "apply_infra",
      action     = "request_infrastructure_provisioning",
      actor      = 'Agent,
      cmd        = "nats pub ecosystem.provisioning.scaffold '{\"project_name\": \"{project_name}\", \"stack\": \"{stack}\"}'",
      depends_on = [{ step = "define_nats_subjects", kind = 'OnSuccess }],
      on_error   = { strategy = 'Continue },
      note       = "Publishes to NATS; provisioning service responds on ecosystem.provisioning.ready",
    },
    {
      id         = "register_in_ontoref",
      action     = "update_ontoref_project_registry",
      actor      = 'Human,
      depends_on = [{ step = "apply_infra", kind = 'Always }],
      on_error   = { strategy = 'Continue },
      note       = "Human task: add the new service to ontoref CLAUDE.md related-projects table",
      verify     = "grep '{project_name}' {ontoref_dir}/.claude/CLAUDE.md",
    },
    {
      id     = "publish_ecosystem_event",
      action = "announce_service_created",
      actor  = 'Agent,
      cmd    = "nats pub ecosystem.project.created '{\"project_name\": \"{project_name}\", \"stack\": \"{stack}\", \"type\": \"service\"}'",
      depends_on = [
        { step = "register_in_ontoref", kind = 'Always },
        { step = "copy_ontology_template",  kind = 'OnSuccess },
        { step = "init_kogral",             kind = 'Always },
        { step = "create_syntaxis_project", kind = 'Always },
      ],
      on_error = { strategy = 'Continue },
    },
  ],

  postconditions = [
    "{project_dir} is an initialized git repository",
    "{project_dir}/.ontology/ populated from templates",
    "NATS stream '{project_name}' created (best-effort)",
    "provisioning request published to ecosystem.provisioning.scaffold",
    "ecosystem.project.created published",
  ],
} | (s.Mode String)
