{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.deixei.com/aes/v0.1.0/cab_decision.json",
  "title": "CAB Decision Record",
  "description": "Output artefact returned by `ops cab evaluate`. Carries the verdict, conditions, reasoning, and evidence consulted. May stand alone or be embedded in an AES record's outcome.output for kind=cab.evaluate.",

  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "decision",
    "policy_template",
    "policy_template_version",
    "change_ref",
    "conditions",
    "reasoning",
    "evidence_consulted",
    "approvers_required"
  ],
  "properties": {
    "schema_version": {
      "type": "string",
      "pattern": "^\\d+\\.\\d+\\.\\d+$"
    },
    "decision": {
      "type": "string",
      "enum": ["go", "go-with-conditions", "no-go", "needs-review", "superseded"]
    },
    "policy_template":         { "type": "string", "minLength": 1 },
    "policy_template_version": { "type": "string", "minLength": 1 },
    "change_ref":              { "type": "string", "minLength": 1 },
    "conditions": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["id", "description", "must_complete_before", "evidence_required"],
        "properties": {
          "id":                   { "type": "string", "minLength": 1 },
          "description":          { "type": "string", "minLength": 1 },
          "must_complete_before": { "type": "string", "enum": ["deploy", "promote", "continue", "other"] },
          "evidence_required":    { "type": "string", "minLength": 1 }
        }
      }
    },
    "reasoning": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["rule_id", "passed", "rationale"],
        "properties": {
          "rule_id":   { "type": "string", "minLength": 1 },
          "passed":    { "type": "boolean" },
          "rationale": { "type": "string", "minLength": 1 }
        }
      }
    },
    "evidence_consulted": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["type", "ref"],
        "properties": {
          "type": { "type": "string", "minLength": 1 },
          "ref":  { "type": "string", "minLength": 1 }
        }
      }
    },
    "approvers_required": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["role", "fulfilled"],
        "properties": {
          "role":                { "type": "string", "minLength": 1 },
          "upn":                 { "type": "string" },
          "fulfilled":           { "type": "boolean" },
          "approval_record_ref": { "type": "string" }
        },
        "allOf": [
          {
            "if":   { "properties": { "fulfilled": { "const": true } } },
            "then": { "required": ["upn", "approval_record_ref"] }
          }
        ]
      }
    },
    "expires_at":    { "type": "string", "format": "date-time" },
    "superseded_by": { "type": "string", "description": "Reference to a later cab_decision that replaces this one. Required when decision=superseded." }
  },

  "allOf": [
    {
      "if":   { "properties": { "decision": { "const": "go-with-conditions" } } },
      "then": { "properties": { "conditions": { "minItems": 1 } } }
    },
    {
      "if":   { "properties": { "decision": { "const": "superseded" } } },
      "then": { "required": ["superseded_by"] }
    }
  ]
}
