# Form: Query active ADR constraints
# Drives the agent/developer through selecting what they need to validate.
# No roundtrip — output is a Nushell command to run.
#
# Agent query:
#   nickel export reflection/forms/query_constraints.ncl \
#     | get elements \
#     | where type != "section_header" \
#     | where type != "section" \
#     | select name prompt options

{
  name        = "Query ADR Constraints",
  description = "Select what you need and get the exact command to run. No ADR reading required.",

  elements = [

    { type = "section_header", name = "intent_header",
      title = "What do you need to validate?", border_top = true, border_bottom = true },

    { type = "select", name = "query_intent",
      prompt  = "Intent",
      required = true,
      options = [
        { value = "active_hard",    label = "Active Hard constraints — what I cannot violate right now" },
        { value = "active_all",     label = "Active constraint set — all Accepted ADRs" },
        { value = "point_in_time",  label = "Historical — what constraints were active at a given date" },
        { value = "supersession",   label = "Supersession chain — trace what replaced what" },
        { value = "invariants",     label = "Core invariants — what is unconditionally protected" },
      ] },

    { type = "text", name = "point_in_time_date",
      prompt   = "Date for historical query (YYYY-MM)",
      required = false,
      default  = "2026-01",
      when     = "query_intent == point_in_time",
      help     = "Returns the constraint set that was active on or before this date." },

    { type = "text", name = "adr_id_filter",
      prompt      = "Filter to specific ADR (e.g. adr-002, leave blank for all)",
      required    = false,
      placeholder = "adr-002",
      when        = "query_intent == active_hard || query_intent == active_all",
      help        = "Leave blank to query across all Accepted ADRs." },

    { type = "section_header", name = "output_header",
      title = "Command to run", border_top = true, border_bottom = true },

    { type = "section", name = "cmd_active_hard",
      when    = "query_intent == active_hard",
      content = "Run:\n  ls adrs/adr-*.ncl | each { |f| nickel export $f } | where status == 'Accepted | get constraints | flatten | where severity == 'Hard | select id claim check_hint" },

    { type = "section", name = "cmd_active_all",
      when    = "query_intent == active_all",
      content = "Run:\n  ls adrs/adr-*.ncl | each { |f| nickel export $f } | where status == 'Accepted | each { |a| { adr: $a.id, title: $a.title, constraints: $a.constraints } }" },

    { type = "section", name = "cmd_point_in_time",
      when    = "query_intent == point_in_time",
      content = "Run (replace YYYY-MM with your target date):\n  ls adrs/adr-*.ncl | each { |f| nickel export $f } | where (($it.status == 'Accepted or $it.status == 'Superseded) and $it.date <= \"YYYY-MM\") | where superseded_by? == null | get constraints | flatten" },

    { type = "section", name = "cmd_supersession",
      when    = "query_intent == supersession",
      content = "Run:\n  ls adrs/adr-*.ncl | each { |f| nickel export $f } | select id status supersedes superseded_by date | sort-by date" },

    { type = "section", name = "cmd_invariants",
      when    = "query_intent == invariants",
      content = "Run:\n  nickel export .ontology/core.ncl | get nodes | where invariant == true | select id name description" },

  ],
}
