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

# Mode: adopt_ontoref
# Onboards an EXISTING project into the ontoref protocol.
# Creates missing pieces without overwriting existing files.
#
# Required params (substituted in cmd via {param}):
#   {project_name}   — identifier for this project (kebab-case)
#   {project_dir}    — absolute path to the existing project root
#   {ontoref_dir}    — absolute path to the ontoref checkout

{
  id      = "adopt_ontoref",
  trigger = "Onboard an existing project into the ontoref protocol",

  preconditions = [
    "{project_dir} exists and is a directory",
    "nickel is available in PATH",
    "nu is available in PATH (>= 0.110.0)",
    "{ontoref_dir}/templates/ontology/ exists (contains core.ncl, state.ncl, gate.ncl, manifest.ncl, connections.ncl stubs)",
    "{ontoref_dir}/templates/ontoref-config.ncl exists",
    "{ontoref_dir}/templates/scripts-ontoref exists",
  ],

  steps = [

    {
      id         = "create_ontoref_dir",
      action     = "create_config_directory",
      actor      = 'Agent,
      cmd        = "mkdir -p {project_dir}/.ontoref/logs {project_dir}/.ontoref/locks",
      depends_on = [],
      on_error   = { strategy = 'Stop },
      note       = "Creates .ontoref/ directory structure. mkdir -p is idempotent.",
    },

    {
      id         = "copy_ontoref_config",
      action     = "copy_config_template",
      actor      = 'Agent,
      cmd        = "test -f {project_dir}/.ontoref/config.ncl || sed 's/{{ project_name }}/{project_name}/g' {ontoref_dir}/templates/ontoref-config.ncl > {project_dir}/.ontoref/config.ncl",
      depends_on = [{ step = "create_ontoref_dir", kind = 'OnSuccess }],
      on_error   = { strategy = 'Stop },
      note       = "Copies config template only if .ontoref/config.ncl does not already exist.",
    },

    {
      id         = "create_ontology_dir",
      action     = "create_ontology_directory",
      actor      = 'Agent,
      cmd        = "mkdir -p {project_dir}/.ontology",
      depends_on = [{ step = "create_ontoref_dir", kind = 'OnSuccess }],
      on_error   = { strategy = 'Stop },
      note       = "Creates .ontology/ directory. Idempotent.",
    },

    {
      id         = "copy_ontology_core",
      action     = "copy_ontology_core_stub",
      actor      = 'Agent,
      cmd        = "test -f {project_dir}/.ontology/core.ncl || sed 's/{{ project_name }}/{project_name}/g' {ontoref_dir}/templates/ontology/core.ncl > {project_dir}/.ontology/core.ncl",
      depends_on = [{ step = "create_ontology_dir", kind = 'OnSuccess }],
      on_error   = { strategy = 'Stop },
      note       = "Copies core.ncl stub. Skipped if file already exists.",
    },

    {
      id         = "copy_ontology_state",
      action     = "copy_ontology_state_stub",
      actor      = 'Agent,
      cmd        = "test -f {project_dir}/.ontology/state.ncl || sed 's/{{ project_name }}/{project_name}/g' {ontoref_dir}/templates/ontology/state.ncl > {project_dir}/.ontology/state.ncl",
      depends_on = [{ step = "create_ontology_dir", kind = 'OnSuccess }],
      on_error   = { strategy = 'Stop },
      note       = "Copies state.ncl stub. Skipped if file already exists.",
    },

    {
      id         = "copy_ontology_gate",
      action     = "copy_ontology_gate_stub",
      actor      = 'Agent,
      cmd        = "test -f {project_dir}/.ontology/gate.ncl || sed 's/{{ project_name }}/{project_name}/g' {ontoref_dir}/templates/ontology/gate.ncl > {project_dir}/.ontology/gate.ncl",
      depends_on = [{ step = "create_ontology_dir", kind = 'OnSuccess }],
      on_error   = { strategy = 'Stop },
      note       = "Copies gate.ncl stub. Skipped if file already exists.",
    },

    {
      id         = "copy_ontology_manifest",
      action     = "copy_ontology_manifest_stub",
      actor      = 'Agent,
      cmd        = "test -f {project_dir}/.ontology/manifest.ncl || sed 's/{{ project_name }}/{project_name}/g' {ontoref_dir}/templates/ontology/manifest.ncl > {project_dir}/.ontology/manifest.ncl",
      depends_on = [{ step = "create_ontology_dir", kind = 'OnSuccess }],
      on_error   = { strategy = 'Continue },
      note       = "Copies manifest.ncl stub for content assets and templates declaration. Skipped if file already exists.",
    },

    {
      id         = "copy_ontology_connections",
      action     = "copy_ontology_connections_stub",
      actor      = 'Agent,
      cmd        = "test -f {project_dir}/.ontology/connections.ncl || sed 's/{{ project_name }}/{project_name}/g' {ontoref_dir}/templates/ontology/connections.ncl > {project_dir}/.ontology/connections.ncl",
      depends_on = [{ step = "create_ontology_dir", kind = 'OnSuccess }],
      on_error   = { strategy = 'Continue },
      note       = "Copies connections.ncl stub for cross-project federation addressing. Skipped if file already exists.",
    },

    {
      id         = "install_scripts_wrapper",
      action     = "install_consumer_entry_point",
      actor      = 'Agent,
      cmd        = "mkdir -p {project_dir}/scripts && test -f {project_dir}/scripts/ontoref || (sed 's|{{ ontoref_dir }}|{ontoref_dir}|g' {ontoref_dir}/templates/scripts-ontoref > {project_dir}/scripts/ontoref && chmod +x {project_dir}/scripts/ontoref)",
      depends_on = [{ step = "create_ontoref_dir", kind = 'OnSuccess }],
      on_error   = { strategy = 'Stop },
      note       = "Installs scripts/ontoref thin wrapper. Skipped if already present.",
    },

    {
      id         = "validate_ontology",
      action     = "nickel_typecheck_ontology",
      actor      = 'Agent,
      cmd        = "cd {project_dir} && nickel export .ontology/core.ncl > /dev/null && nickel export .ontology/state.ncl > /dev/null && nickel export .ontology/gate.ncl > /dev/null",
      depends_on = [
        { step = "copy_ontology_core",        kind = 'OnSuccess },
        { step = "copy_ontology_state",       kind = 'OnSuccess },
        { step = "copy_ontology_gate",        kind = 'OnSuccess },
        { step = "copy_ontology_manifest",    kind = 'Always },
        { step = "copy_ontology_connections", kind = 'Always },
      ],
      on_error   = { strategy = 'Stop },
      note       = "Validates all three .ontology/ files parse without errors.",
    },

  ],

  postconditions = [
    "{project_dir}/.ontoref/config.ncl exists and is valid Nickel",
    "{project_dir}/.ontology/core.ncl, state.ncl, gate.ncl exist and parse",
    "{project_dir}/.ontology/manifest.ncl exists (content assets + templates declaration)",
    "{project_dir}/.ontology/connections.ncl exists (cross-project federation stub)",
    "{project_dir}/scripts/ontoref exists and is executable",
    "No existing files were overwritten",
  ],
} | (s.Mode String)
