#!/usr/bin/env nu
# Generated by: typedialog form reflection/forms/new_project.ncl
# Project: {{ project_name }} — {{ project_description }}
# Type: {{ project_type }}

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

# ── 1. Git init ───────────────────────────────────────────────────────────────

mkdir $project_dir
git -C $project_dir init
git -C $project_dir checkout -b {{ default_branch }}
git -C $project_dir commit --allow-empty -m "chore: initial commit"

{% if git_remote_url and git_remote_url != "" %}
git -C $project_dir remote add origin "{{ git_remote_url }}"
{% endif %}

# ── 2. Ontology templates ─────────────────────────────────────────────────────

{% if copy_ontology_template %}
cp -r $"($ontoref_dir)/.ontology" $"($project_dir)/.ontology"
print $"✓ .ontology/ copied — fill in ($project_dir)/.ontology/core.ncl with ($project_name) invariants"
{% endif %}

# ── 3. NATS stream ───────────────────────────────────────────────────────────

{% if use_nats %}
do {
  nats --server "{{ nats_url }}" stream add ECOSYSTEM \
    --subjects $"ecosystem.($project_name).>" \
    --no-headers-only \
    --defaults
  print $"✓ NATS stream configured: ecosystem.($project_name).>"
} | complete | if $in.exit_code != 0 {
  print $"⚠ NATS stream setup skipped \(server not available\)"
}
{% endif %}

# ── 4. kogral ────────────────────────────────────────────────────────────────

{% if use_kogral %}
do {
  kogral init --name $project_name --dir $"($project_dir)/.kogral"
  print "✓ kogral initialized"
} | complete | if $in.exit_code != 0 {
  print "⚠ kogral init skipped (not installed or failed — non-blocking)"
}
{% endif %}

# ── 5. syntaxis ──────────────────────────────────────────────────────────────

{% if use_syntaxis %}
do {
  syntaxis project create --name $project_name --path $project_dir
  print "✓ registered in syntaxis"
} | complete | if $in.exit_code != 0 {
  print "⚠ syntaxis registration skipped (not installed or failed — non-blocking)"
}
{% endif %}

# ── 6. NATS announcement ─────────────────────────────────────────────────────

{% if use_nats %}
do {
  nats --server "{{ nats_url }}" pub ecosystem.project.created (
    { project_name: $project_name, project_dir: $project_dir, project_type: "{{ project_type }}" }
    | to json
  )
  print "✓ ecosystem.project.created published"
} | complete | if $in.exit_code != 0 {
  print "⚠ NATS announcement skipped (best-effort)"
}
{% endif %}

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

print ""
print $"Project ($project_name) initialized at ($project_dir)"
print ""
print "Next steps:"
print $"  1. Fill in ($project_dir)/.ontology/core.ncl — project invariants"
print $"  2. Fill in ($project_dir)/.ontology/state.ncl — current + desired state"
print $"  3. Create reflection/modes/ for project-specific operations"
{% if git_remote_url and git_remote_url != "" %}
print $"  4. git -C ($project_dir) push -u origin {{ default_branch }}"
{% endif %}
