# ~/.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

{
  {%- if nickel_import_paths and nickel_import_paths != "" %}
  nickel_import_paths = {{ nickel_import_paths | split(pat=",") | map(attribute="trim") | json }},
  {%- else %}
  nickel_import_paths | Array String = [],
  {%- endif %}

  daemon = {
    port | Port   = {{ daemon_port }},
    host | String = "{{ daemon_host }}",
  },

  db = {
    enabled  | Bool   = {{ db_enabled }},
    url      | String = "{{ db_url }}",
    pool_max | Number = {{ db_pool_max }},
  },

  nats_events = {
    enabled        | Bool         = {{ nats_enabled }},
    url            | String       = "{{ nats_url }}",
    streams_config | String       = "{{ nats_streams_config }}",
    emit           | Array String = [],
    subscribe      | Array String = [],
    handlers_dir   | String       = "{{ nats_handlers_dir }}",
  },

  log = {
    level     | LogLevel = '{{ log_level }},
    rotation  | Rotation = '{{ log_rotation }},
    max_files | Number   = {{ log_max_files }},
    compress  | Bool     = {{ log_compress }},
    archive   | String   = "{{ log_archive }}",
    path      | String   = "{{ log_path }}",
  },

  cache = {
    path | String = "{{ cache_path }}",
  },

  ui = {
    templates_dir | String = "{{ ui_templates_dir }}",
    public_dir    | String = "{{ ui_public_dir }}",
    logo          | String = "ontoref-logo.svg",
    tls_cert      | String = "{{ ui_tls_cert }}",
    tls_key       | String = "{{ ui_tls_key }}",
  },

  mode_run = {
    rules | Array {
      when    | { mode_id | String | optional, actor | String | optional },
      allow   | Bool,
      reason  | String,
    } = [
      {%- if allow_validate_ontology %}
      { when = { mode_id = "validate-ontology" }, allow = true,  reason = "validation always allowed" },
      {%- else %}
      { when = { mode_id = "validate-ontology" }, allow = false, reason = "validation disabled" },
      {%- endif %}
      {%- if allow_agent_actor %}
      { when = { actor = "agent" }, allow = true,  reason = "agent actor always allowed" },
      {%- else %}
      { when = { actor = "agent" }, allow = false, reason = "agent actor restricted" },
      {%- endif %}
      {%- if allow_ci_actor %}
      { when = { actor = "ci" }, allow = true,  reason = "ci actor always allowed" },
      {%- else %}
      { when = { actor = "ci" }, allow = false, reason = "ci actor restricted" },
      {%- endif %}
    ],
  },

  actor_init | Array {
    actor    | String,
    mode     | String,
    auto_run | Bool,
  } = [
    { actor = "agent",     mode = "{{ actor_init_agent_mode }}",     auto_run = {{ actor_init_agent_auto }} },
    { actor = "developer", mode = "{{ actor_init_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"],
    },
  ],
}
