# ~/.config/ontoref/config.ncl — ontoref-daemon runtime configuration
#
# Edit interactively:
#   ontoref config-edit           (typedialog-web roundtrip, browser form)
# Validate:
#   nickel typecheck ~/.config/ontoref/config.ncl
# Apply (NCL pipe bootstrap, ADR-004):
#   ontoref-daemon-boot
#   ontoref-daemon-boot --dry-run  # preview composed JSON

let Port = std.contract.from_predicate
  (fun p => std.is_number p && p > 1024 && p < 65536)
  in

let LogLevel  = [| 'trace, 'debug, 'info, 'warn, 'error |] in
let Rotation  = [| 'daily, 'hourly, 'never |] in
let Actor     = [| 'developer, 'agent, 'ci, 'admin |] in
let Severity  = [| 'Hard, 'Soft |] in

let KeyRole   = [| 'admin, 'viewer |] in
let KeyEntry  = { role | KeyRole, hash | String } in
let ProjectEntry = {
  slug                | String,
  root                | String,
  nickel_import_paths | Array String | default = [],
  keys                | Array KeyEntry | default = [],
  remote_url          | String | default = "",
  push_only           | Bool   | default = false,
} in

{
  # Nickel import paths used when exporting/typechecking configs that import schemas.
  # Relative paths are resolved from the config file's directory.
  nickel_import_paths | Array String = [],

  # Registered projects served by this daemon instance.
  # Local projects:  populated from projects.ncl (ontoref project-add /path/to/project)
  # Remote projects: populated from remote-projects.ncl (ontoref project-add-remote <url> <slug>)
  projects | Array ProjectEntry = (import "./projects.ncl") @ (import "./remote-projects.ncl"),

  daemon = {
    port | Port   = 7890,
    host | String = "127.0.0.1",
  },

  db = {
    enabled  | Bool   = false,
    url      | String = "http://localhost:8000/ontoref",
    pool_max | Number = 5,
  },

  nats_events = {
    enabled        | Bool         = false,
    url            | String       = "nats://localhost:4222",
    streams_config | String       = "",
    emit           | Array String = [],
    subscribe      | Array String = [],
    handlers_dir   | String       = "reflection/handlers",
  },

  log = {
    level     | LogLevel = 'info,
    rotation  | Rotation = 'daily,
    max_files | Number   = 7,
    compress  | Bool     = false,
    archive   | String   = "",
    # Resolved by daemon from platform conventions when empty:
    #   macOS: ~/Library/Logs/ontoref/   Linux: ~/.local/state/ontoref/logs/
    path | String = "",
  },

  cache = {
    # Resolved by daemon from platform conventions when empty:
    #   macOS: ~/Library/Caches/ontoref/   Linux: ~/.cache/ontoref/
    path | String = "",
  },

  ui = {
    # Resolved from platform data dir when empty:
    #   macOS: ~/Library/Application Support/ontoref/templates
    #   Linux: ~/.local/share/ontoref/templates
    templates_dir | String = "",
    public_dir    | String = "",
    logo          | String = "ontoref-logo.svg",
    tls_cert      | String = "",
    tls_key       | String = "",
  },

  mode_run = {
    rules | Array {
      when    | { mode_id | String | optional, actor | String | optional },
      allow   | Bool,
      reason  | String,
    } = [
      { when = { mode_id = "validate-ontology" }, allow = true,  reason = "validation always allowed" },
      { when = { actor   = "agent"             }, allow = true,  reason = "agent actor always allowed" },
      { when = { actor   = "ci"                }, allow = true,  reason = "ci actor always allowed" },
    ],
  },

  actor_init | Array {
    actor    | String,
    mode     | String,
    auto_run | Bool,
  } = [
    { actor = "agent",     mode = "describe capabilities", auto_run = true  },
    { actor = "developer", mode = "",                      auto_run = false },
    { actor = "ci",        mode = "",                      auto_run = false },
  ],

  quick_actions | Array {
    id       | String,
    label    | String,
    icon     | String,
    category | String,
    mode     | String,
    actors   | Array String,
  } = [
    {
      id       = "gen-docs",
      label    = "Generate documentation",
      icon     = "book-open",
      category = "docs",
      mode     = "generate-mdbook",
      actors   = ["developer", "agent"],
    },
    {
      id       = "sync-onto",
      label    = "Sync ontology",
      icon     = "refresh",
      category = "sync",
      mode     = "sync-ontology",
      actors   = ["developer", "ci", "agent"],
    },
    {
      id       = "coder-workflow",
      label    = "Coder workflow",
      icon     = "code",
      category = "process",
      mode     = "coder-workflow",
      actors   = ["developer", "agent"],
    },
  ],
}
