{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://antaeus.io/contracts/v0alpha2/decision.schema.json",
  "title": "Antaeus Decision v0alpha2",
  "type": "object",
  "required": [
    "apiVersion",
    "kind",
    "outcome",
    "policy",
    "ruleResults",
    "reasonCodes"
  ],
  "properties": {
    "apiVersion": {
      "const": "decision.antaeus.io/v0alpha2"
    },
    "kind": {
      "const": "Decision"
    },
    "outcome": {
      "$ref": "../v0alpha1/common.schema.json#/$defs/decisionOutcome"
    },
    "policy": {
      "$ref": "../v0alpha1/common.schema.json#/$defs/policyIdentity"
    },
    "ruleResults": {
      "type": "array",
      "minItems": 1,
      "maxItems": 256,
      "items": {
        "$ref": "#/$defs/ruleResult"
      }
    },
    "reasonCodes": {
      "$ref": "../v0alpha1/common.schema.json#/$defs/reasonCodes"
    },
    "failure": {
      "$ref": "#/$defs/failure"
    },
    "evaluator": {
      "$ref": "#/$defs/evaluator"
    },
    "extensions": {
      "$ref": "../v0alpha1/common.schema.json#/$defs/extensions"
    }
  },
  "$defs": {
    "ruleResult": {
      "type": "object",
      "required": [
        "ruleId",
        "status",
        "reasonCodes"
      ],
      "properties": {
        "ruleId": {
          "$ref": "../v0alpha1/common.schema.json#/$defs/ruleId"
        },
        "status": {
          "$ref": "../v0alpha1/common.schema.json#/$defs/ruleStatus"
        },
        "outcome": {
          "$ref": "../v0alpha1/common.schema.json#/$defs/policyOutcome"
        },
        "confidence": {
          "type": "number",
          "minimum": 0,
          "maximum": 1
        },
        "reasonCodes": {
          "$ref": "../v0alpha1/common.schema.json#/$defs/reasonCodes"
        },
        "message": {
          "type": "string",
          "maxLength": 4096
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "status": {
                "const": "matched"
              }
            },
            "required": [
              "status"
            ]
          },
          "then": {
            "required": [
              "outcome"
            ]
          },
          "else": {
            "not": {
              "required": [
                "outcome"
              ]
            }
          }
        }
      ],
      "unevaluatedProperties": false
    },
    "failure": {
      "type": "object",
      "required": [
        "code",
        "stage",
        "retryable"
      ],
      "properties": {
        "code": {
          "$ref": "../v0alpha1/common.schema.json#/$defs/reasonCode"
        },
        "stage": {
          "type": "string",
          "enum": [
            "configuration",
            "policy_resolution",
            "evaluation",
            "reduction",
            "internal"
          ]
        },
        "retryable": {
          "type": "boolean"
        },
        "message": {
          "type": "string",
          "maxLength": 4096
        }
      },
      "unevaluatedProperties": false
    },
    "evaluator": {
      "type": "object",
      "required": [
        "profileDigest",
        "adapter",
        "adapterVersion",
        "mode",
        "synthetic",
        "route",
        "attempts"
      ],
      "properties": {
        "profileDigest": {
          "$ref": "../v0alpha1/common.schema.json#/$defs/digest"
        },
        "profileVersion": {
          "$ref": "../v0alpha1/common.schema.json#/$defs/version"
        },
        "adapter": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "adapterVersion": {
          "$ref": "../v0alpha1/common.schema.json#/$defs/version",
          "pattern": ".*\\S.*"
        },
        "mode": {
          "type": "string",
          "enum": [
            "semantic",
            "deterministic-fixture"
          ]
        },
        "synthetic": {
          "type": "boolean"
        },
        "provider": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "model": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        },
        "fixtureSet": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9._-]{0,63}$"
        },
        "fixtureVersion": {
          "$ref": "../v0alpha1/common.schema.json#/$defs/version",
          "pattern": ".*\\S.*"
        },
        "route": {
          "type": "array",
          "minItems": 1,
          "maxItems": 16,
          "items": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128
          }
        },
        "attempts": {
          "type": "integer",
          "minimum": 1,
          "maximum": 64
        },
        "fallback": {
          "type": "boolean"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "mode": {
                "const": "deterministic-fixture"
              }
            },
            "required": [
              "mode"
            ]
          },
          "then": {
            "properties": {
              "synthetic": {
                "const": true
              }
            },
            "required": [
              "fixtureSet",
              "fixtureVersion"
            ]
          },
          "else": {
            "properties": {
              "synthetic": {
                "const": false
              }
            },
            "not": {
              "anyOf": [
                {
                  "required": [
                    "fixtureSet"
                  ]
                },
                {
                  "required": [
                    "fixtureVersion"
                  ]
                }
              ]
            }
          }
        }
      ],
      "unevaluatedProperties": false
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "outcome": {
            "const": "failure"
          }
        },
        "required": [
          "outcome"
        ]
      },
      "then": {
        "required": [
          "failure"
        ]
      },
      "else": {
        "not": {
          "required": [
            "failure"
          ]
        }
      }
    }
  ],
  "unevaluatedProperties": false
}
