#!/usr/bin/env nu
# reflection/hooks/git-event.nu — called by post-merge and post-checkout hooks.
#
# Delegates entirely to the ontoref dispatcher so all mode detection,
# transition, and sync logic lives in one place.
# Never fails the git operation — all errors are best-effort.

def main [event: string = ""] {
  let ontoref_root = ($env.ONTOREF_ROOT? | default "")
  if ($ontoref_root | is-empty) { return }

  let wrapper = $"($ontoref_root)/ontoref"
  if not ($wrapper | path exists) { return }

  # Trigger mode detection + transition. Sync runs inside on-enter-daemon if mode changed.
  do { ^nu $wrapper mode-detect } | complete | null

  # If daemon mode is active, push ontology for current git event.
  let lock_file = $"($ontoref_root)/.ontoref/mode.lock"
  if not ($lock_file | path exists) { return }

  let lock = (do { open $lock_file | from json } | complete | get -o stdout | default { mode: "local" })
  if $lock.mode == "daemon" {
    let store = $"($ontoref_root)/reflection/modules/store.nu"
    let r = (do { ^nu -c $"use ($store) *; store sync-push" } | complete)
    if $r.exit_code == 0 {
      print $"  (ansi green)ontoref(ansi reset) ontology synced to daemon"
    }
  }
}
