#!/usr/bin/env nu
# onto-onboard.nu — stamp ontoref onboarding into a generated site.
#
#   nu onto-onboard.nu --level full    --root <site> --project <id> --languages "en,es"
#   nu onto-onboard.nu --level minimal --root <site> --project <id>
#   nu onto-onboard.nu --level none    --root <site> --project <id>
#
# Levels:
#   none     — do nothing. The site is not governed by ontoref.
#   minimal  — write .ontoref/{config.ncl, card.ncl} + schemas + a `just onto::init`
#              hint. The owner runs `ontoref setup` later to scaffold the rest.
#   full     — minimal + delegate to `ontoref setup` (the tool's own onboarding,
#              which scaffolds ontology/reflection/adrs from its bundled templates).
#
# The site's own .ontoref/ governs the SITE's project. The rustelo DOMAIN ontology
# is referenced (not copied) via .rustelo.ontoref → framework. Domain authoring lives
# elsewhere; this helper only writes a pointer.

def stamp [content: string, dest: string, project: string, languages: string, profile: string] {
  $content
  | str replace --all "{{ project_name }}" $project
  | str replace --all "{{project_name}}" $project
  | str replace --all "{{ languages }}" $languages
  | str replace --all "{{ render_profile }}" $profile
  | save -f $dest
}

def main [
  --level: string = "minimal"   # none | minimal | full
  --root: string                # generated site root (required)
  --project: string = "website" # project slug/id
  --languages: string = "en,es"
  --profile: string = "htmx-ssr"
] {
  if ($root | is-empty) { error make { msg: "--root is required" } }
  if ($level not-in ["none" "minimal" "full"]) { error make { msg: $"bad --level ($level)" } }
  let root = ($root | path expand)
  if $level == "none" {
    print "[onto] level=none — skipping ontoref onboarding"
    return
  }

  let skel = ($env.FILE_PWD | path join ".." ".." "templates" "_ontoref-skeleton" | path expand)
  let onto = ($root | path join ".ontoref")
  mkdir $onto
  mkdir ($onto | path join "reflection" "schemas")
  mkdir ($onto | path join "ontology" "schemas")

  # config + card + their schemas (minimal floor)
  stamp (open --raw ($skel | path join "minimal" "config.ncl") | decode utf-8) ($onto | path join "config.ncl") $project $languages $profile
  stamp (open --raw ($skel | path join "minimal" "card.ncl") | decode utf-8) ($onto | path join "card.ncl") $project $languages $profile
  cp ($skel | path join "minimal" "schemas" "project-card.ncl") ($onto | path join "ontology" "schemas" "project-card.ncl")
  cp ($skel | path join "minimal" "schemas" "backlog.ncl") ($onto | path join "reflection" "schemas" "backlog.ncl")

  # domain reference (pointer only — do not author the domain layer here)
  let domain_ref = ($root | path join ".rustelo.ontoref")
  mkdir $domain_ref
  "# .rustelo.ontoref — reference to the Rustelo domain ontology.\n# The domain layer is authored in the framework, not here. This marker tells\n# ontoref where the framework's domain ontology lives for cross-project browsing.\n# Set DOMAIN_ROOT to your rustelo checkout (sibling by default).\n" | save -f ($domain_ref | path join "README.md")

  print $"[onto] minimal skeleton stamped at ($onto)"

  if $level == "full" {
    # delegate ontology/reflection/adrs scaffolding to the tool's own onboarding.
    let ok = (do { ^ontoref setup } | complete)
    if $ok.exit_code == 0 {
      print "[onto] full — ontoref setup completed"
    } else {
      print "[onto] full — `ontoref setup` unavailable here; run it in the site root to finish onboarding"
    }
  }
}
