# Form: ontoref-daemon runtime configuration
#
# Roundtrip (browser form → updated config.ncl):
#   typedialog-web nickel-roundtrip \
#     --output ~/.config/ontoref/config.ncl \
#     --ncl-template reflection/forms/config.ncl.j2 \
#     --open \
#     ~/.config/ontoref/config.ncl \
#     reflection/forms/config.ncl
#
# Or via dispatcher:
#   ./ontoref config-edit

{
  name         = "ontoref-daemon configuration",
  description  = "Runtime configuration for the ontoref daemon. Edits ~/.config/ontoref/config.ncl in place.",
  display_mode = "complete",

  elements = [

    # ── Daemon ───────────────────────────────────────────────────────────────
    { type = "section_header", name = "daemon_header",
      title = "Daemon", border_top = true, border_bottom = true },

    { type = "text", name = "daemon_host",
      prompt      = "Bind host",
      default     = "127.0.0.1",
      help        = "Use 0.0.0.0 to expose on all interfaces.",
      nickel_path = ["daemon", "host"] },

    { type = "text", name = "daemon_port",
      prompt      = "Port",
      default     = "7890",
      help        = "Ports 1025–65535.",
      nickel_path = ["daemon", "port"] },

    # ── Database ─────────────────────────────────────────────────────────────
    { type = "section_header", name = "db_header",
      title = "Database (SurrealDB)", border_top = true, border_bottom = true },

    { type = "confirm", name = "db_enabled",
      prompt      = "Enable database persistence?",
      default     = false,
      help        = "Requires a running SurrealDB instance.",
      nickel_path = ["db", "enabled"] },

    { type = "text", name = "db_url",
      prompt      = "SurrealDB URL",
      default     = "http://localhost:8000/ontoref",
      when        = "db_enabled == true",
      nickel_path = ["db", "url"] },

    { type = "text", name = "db_pool_max",
      prompt      = "Connection pool size",
      default     = "5",
      when        = "db_enabled == true",
      nickel_path = ["db", "pool_max"] },

    # ── NATS Events ──────────────────────────────────────────────────────────
    { type = "section_header", name = "nats_header",
      title = "NATS Events", border_top = true, border_bottom = true },

    { type = "confirm", name = "nats_enabled",
      prompt      = "Enable NATS event bus?",
      default     = false,
      help        = "Requires a running NATS server.",
      nickel_path = ["nats_events", "enabled"] },

    { type = "text", name = "nats_url",
      prompt      = "NATS server URL",
      default     = "nats://localhost:4222",
      when        = "nats_enabled == true",
      nickel_path = ["nats_events", "url"] },

    { type = "text", name = "nats_streams_config",
      prompt      = "Stream topology file (leave empty for global default)",
      default     = "",
      when        = "nats_enabled == true",
      help        = "JSON file with JetStream streams/consumers (relative to project root). Empty = use ~/.config/ontoref/streams.json via NATS_STREAMS_CONFIG.",
      nickel_path = ["nats_events", "streams_config"] },

    { type = "text", name = "nats_handlers_dir",
      prompt      = "Handlers directory",
      default     = "reflection/handlers",
      when        = "nats_enabled == true",
      help        = "Path to Nu scripts that handle incoming NATS events.",
      nickel_path = ["nats_events", "handlers_dir"] },

    # ── Logging ──────────────────────────────────────────────────────────────
    { type = "section_header", name = "log_header",
      title = "Logging", border_top = true, border_bottom = true },

    { type = "select", name = "log_level",
      prompt  = "Log level",
      default = "info",
      options = [
        { value = "trace", label = "trace — very verbose, development only" },
        { value = "debug", label = "debug — debug output" },
        { value = "info",  label = "info  — normal operation (default)" },
        { value = "warn",  label = "warn  — warnings and errors only" },
        { value = "error", label = "error — errors only" },
      ],
      nickel_path = ["log", "level"] },

    { type = "select", name = "log_rotation",
      prompt  = "Log rotation",
      default = "daily",
      options = [
        { value = "daily",  label = "daily  — rotate every day" },
        { value = "hourly", label = "hourly — rotate every hour" },
        { value = "never",  label = "never  — single file, no rotation" },
      ],
      nickel_path = ["log", "rotation"] },

    { type = "text", name = "log_max_files",
      prompt      = "Max rotated log files to keep",
      default     = "7",
      nickel_path = ["log", "max_files"] },

    { type = "confirm", name = "log_compress",
      prompt      = "Compress rotated log files?",
      default     = false,
      nickel_path = ["log", "compress"] },

    { type = "text", name = "log_archive",
      prompt      = "Archive directory for old logs (leave empty to disable)",
      default     = "",
      nickel_path = ["log", "archive"] },

    { type = "text", name = "log_path",
      prompt      = "Log directory (leave empty for platform default)",
      default     = "",
      help        = "macOS default: ~/Library/Logs/ontoref/  Linux: ~/.local/state/ontoref/logs/",
      nickel_path = ["log", "path"] },

    # ── Cache ─────────────────────────────────────────────────────────────────
    { type = "section_header", name = "cache_header",
      title = "Cache", border_top = true, border_bottom = true },

    { type = "text", name = "cache_path",
      prompt      = "Cache directory (leave empty for platform default)",
      default     = "",
      help        = "macOS default: ~/Library/Caches/ontoref/  Linux: ~/.cache/ontoref/",
      nickel_path = ["cache", "path"] },

    # ── UI Assets ─────────────────────────────────────────────────────────────
    { type = "section_header", name = "ui_header",
      title = "UI Assets", border_top = true, border_bottom = true },

    { type = "section", name = "ui_note",
      content = "Leave empty to use platform data dir (set during install)." },

    { type = "text", name = "ui_templates_dir",
      prompt      = "Templates directory (leave empty for platform default)",
      default     = "",
      nickel_path = ["ui", "templates_dir"] },

    { type = "text", name = "ui_public_dir",
      prompt      = "Public/static directory (leave empty for platform default)",
      default     = "",
      nickel_path = ["ui", "public_dir"] },

    { type = "text", name = "ui_tls_cert",
      prompt      = "TLS certificate path (leave empty to disable TLS)",
      default     = "",
      nickel_path = ["ui", "tls_cert"] },

    { type = "text", name = "ui_tls_key",
      prompt      = "TLS key path",
      default     = "",
      when        = "ui_tls_cert != \"\"",
      nickel_path = ["ui", "tls_key"] },

    # ── Mode Rules ────────────────────────────────────────────────────────────
    { type = "section_header", name = "mode_run_header",
      title = "Mode Execution Rules", border_top = true, border_bottom = true },

    { type = "section", name = "mode_run_note",
      content = "Rules evaluated in order — first match wins. The template preserves the full rules array; these toggles control the three default entries." },

    { type = "confirm", name = "allow_validate_ontology",
      prompt  = "Allow validate-ontology mode for all actors?",
      default = true },

    { type = "confirm", name = "allow_agent_actor",
      prompt  = "Allow all modes for agent actor?",
      default = true },

    { type = "confirm", name = "allow_ci_actor",
      prompt  = "Allow all modes for ci actor?",
      default = true },

    # ── Actor Init ───────────────────────────────────────────────────────────
    { type = "section_header", name = "actor_init_header",
      title = "Actor Initialisation", border_top = true, border_bottom = true },

    { type = "text", name = "actor_init_agent_mode",
      prompt  = "Agent: auto-run mode on start",
      default = "describe capabilities",
      help    = "Runs automatically when actor=agent initialises." },

    { type = "confirm", name = "actor_init_agent_auto",
      prompt  = "Agent: auto-run enabled?",
      default = true },

    { type = "text", name = "actor_init_developer_mode",
      prompt  = "Developer: auto-run mode on start (leave empty to disable)",
      default = "" },

    # ── Nickel Import Paths ──────────────────────────────────────────────────
    { type = "section_header", name = "nickel_header",
      title = "Nickel Import Paths", border_top = true, border_bottom = true },

    { type = "section", name = "nickel_note",
      content = "Extra paths added to NICKEL_IMPORT_PATH. The config dir and platform data dir are always included automatically." },

    { type = "text", name = "nickel_import_paths",
      prompt      = "Additional import paths (comma-separated, leave empty for none)",
      default     = "",
      help        = "Example: /opt/ontoref/schemas,~/shared-schemas" },

    # ── Review ────────────────────────────────────────────────────────────────
    { type = "section_header", name = "review_header",
      title = "Review & Save", border_top = true },

    { type = "section", name = "review_note",
      content = "After saving, run `nu install/config-setup.nu` to validate paths and probe external services." },

    { type = "confirm", name = "ready_to_save",
      prompt  = "Save configuration?",
      default = true },
  ],
}
