# Contracts for .ontoref/config.ncl sections.
#
# Applied in config.ncl with `section | C.SectionContract = { ... }`.
# Consumed by the daemon coherence / quickref tooling via the
# config_surface.sections[].contract field in .ontology/manifest.ncl.

let contract = std.contract in

# ── Primitive contracts ──────────────────────────────────────────────────────

let LogLevel = contract.from_validator (fun value =>
  if std.array.elem value ["error", "warn", "info", "debug", "trace"] then
    'Ok
  else
    'Error { message = "log.level must be one of: error, warn, info, debug, trace" }
) in

let LogRotation = contract.from_validator (fun value =>
  if std.array.elem value ["daily", "hourly", "never"] then
    'Ok
  else
    'Error { message = "log.rotation must be one of: daily, hourly, never" }
) in

let PositiveInt = contract.from_validator (fun value =>
  if std.is_number value && value > 0 then
    'Ok
  else
    'Error { message = "value must be a positive integer (> 0)" }
) in

let Port = contract.from_validator (fun value =>
  if std.is_number value && value >= 1 && value <= 65535 then
    'Ok
  else
    'Error { message = "port must be a number between 1 and 65535" }
) in

# ── Section contracts ────────────────────────────────────────────────────────

{
  LogConfig = {
    level     | LogLevel    | default = "info",
    path      | String      | default = "logs",
    rotation  | LogRotation | default = "daily",
    compress  | Bool        | default = false,
    archive   | String      | default = "logs-archive",
    max_files | PositiveInt | default = 7,
  },

  # All daemon fields are optional — they override CLI defaults only when set.
  # Absent fields fall back to the daemon's built-in defaults (see Cli struct).
  DaemonConfig = {
    port                      | Port        | optional,
    idle_timeout              | PositiveInt | optional,
    invalidation_interval     | PositiveInt | optional,
    actor_sweep_interval      | PositiveInt | optional,
    actor_stale_timeout       | PositiveInt | optional,
    max_notifications         | PositiveInt | optional,
    notification_ack_required | Array String | default = [],
  },
}
