#!/usr/bin/env nu
# Generated by: ontoref form reflection/forms/adopt_ontoref.ncl
# Onboards {{ project_name }} at {{ project_dir }} into the ontoref protocol.
# All steps are additive — existing files are NOT overwritten.

let project_name = "{{ project_name }}"
let project_dir  = "{{ project_dir }}"
let ontoref_dir  = "{{ ontoref_dir }}"

def print_skip [label: string] {
  print $"  skip  ($label) \(already exists\)"
}

def print_ok [label: string] {
  print $"  ok    ($label)"
}

def print_fail [label: string, reason: string] {
  print $"  FAIL  ($label): ($reason)"
}

# ── 1. .ontoref/ directory structure ─────────────────────────────────────────

mkdir $"($project_dir)/.ontoref/logs"
mkdir $"($project_dir)/.ontoref/locks"
print_ok ".ontoref/logs and .ontoref/locks created"

# ── 2. .ontoref/config.ncl ────────────────────────────────────────────────────

{% if install_config %}
let config_dest = $"($project_dir)/.ontoref/config.ncl"
if ($config_dest | path exists) {
  print_skip ".ontoref/config.ncl"
} else {
  let config_src = $"($ontoref_dir)/templates/ontoref-config.ncl"
  let content = open --raw $config_src | str replace --all "{{ project_name }}" $project_name
  $content | save $config_dest
  print_ok ".ontoref/config.ncl"
}
{% endif %}

# ── 3. .ontology/ stubs ───────────────────────────────────────────────────────

{% if install_ontology_stubs %}
mkdir $"($project_dir)/.ontology"

for stub in ["core.ncl", "state.ncl", "gate.ncl"] {
  let dest = $"($project_dir)/.ontology/($stub)"
  if ($dest | path exists) {
    print_skip $".ontology/($stub)"
  } else {
    let src = $"($ontoref_dir)/templates/ontology/($stub)"
    let content = open --raw $src | str replace --all "{{ project_name }}" $project_name
    $content | save $dest
    print_ok $".ontology/($stub)"
  }
}
{% endif %}

# ── 4. scripts/ontoref wrapper ────────────────────────────────────────────────

{% if install_scripts_wrapper %}
mkdir $"($project_dir)/scripts"
let wrapper_dest = $"($project_dir)/scripts/ontoref"
if ($wrapper_dest | path exists) {
  print_skip "scripts/ontoref"
} else {
  let wrapper_src = $"($ontoref_dir)/templates/scripts-ontoref"
  let content = open --raw $wrapper_src | str replace --all "{{ ontoref_dir }}" $ontoref_dir
  $content | save $wrapper_dest
  do { chmod +x $wrapper_dest } | complete | ignore
  print_ok "scripts/ontoref (executable)"
}
{% endif %}

# ── 5. Validate .ontology/ ────────────────────────────────────────────────────

{% if validate_after %}
print ""
print "Validating .ontology/ files..."

for stub in ["core.ncl", "state.ncl", "gate.ncl"] {
  let file = $"($project_dir)/.ontology/($stub)"
  if not ($file | path exists) {
    print_skip $"validate .ontology/($stub) \(not installed\)"
    continue
  }
  let result = do { ^nickel export $file } | complete
  if $result.exit_code == 0 {
    print_ok $"nickel export .ontology/($stub)"
  } else {
    print_fail $"nickel export .ontology/($stub)" $result.stderr
  }
}
{% endif %}

# ── Summary ───────────────────────────────────────────────────────────────────

print ""
print $"Onboarding complete: ($project_name) at ($project_dir)"
print ""
print "Next steps:"
print "  1. Fill in .ontology/core.ncl — replace stub nodes with real invariants and tensions"
print "  2. Fill in .ontology/state.ncl — set current_state to where the project actually is"
print "  3. Review .ontoref/config.ncl — adjust log level, NATS settings if needed"
print $"  4. Add 'alias ontoref=\"./scripts/ontoref\"' to your shell profile"
print "  5. Run: ./scripts/ontoref describe project"
