{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://api.decide.fyi/schemas/rulebook-v1.schema.json",
  "title": "Decide Rulebook v1",
  "description": "Closed declarative rulebook contract for deterministic Decide production verdicts.",
  "type": "object",
  "required": [
    "schema_version",
    "rulebook_id",
    "version",
    "rules",
    "default_outcome"
  ],
  "properties": {
    "schema_version": {
      "const": "rulebook_v1"
    },
    "rulebook_id": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_.:-]{1,119}$"
    },
    "version": {
      "type": "string",
      "minLength": 1,
      "maxLength": 80
    },
    "input_schema": {
      "$ref": "#/$defs/input_schema"
    },
    "rules": {
      "type": "array",
      "minItems": 1,
      "maxItems": 100,
      "items": {
        "$ref": "#/$defs/rule"
      }
    },
    "default_outcome": {
      "$ref": "#/$defs/outcome"
    }
  },
  "additionalProperties": false,
  "x-decide-evaluator-version": "decide_rulebook_v1",
  "x-decide-condition-limits": {
    "max_depth": 8,
    "max_nodes": 256,
    "max_children_per_combinator": 32
  },
  "$defs": {
    "input_schema": {
      "type": "object",
      "properties": {
        "required": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[A-Za-z_][A-Za-z0-9_.-]{0,159}$"
          }
        },
        "properties": {
          "type": "object",
          "additionalProperties": {
            "type": "object",
            "required": [
              "type"
            ],
            "properties": {
              "type": {
                "type": "string",
                "enum": [
                  "array",
                  "object",
                  "number",
                  "integer",
                  "string",
                  "boolean",
                  "null"
                ]
              }
            },
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": false
    },
    "rule": {
      "type": "object",
      "required": [
        "rule_id",
        "condition",
        "outcome"
      ],
      "properties": {
        "rule_id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_.:-]{1,119}$"
        },
        "priority": {
          "type": "integer",
          "minimum": -1000,
          "maximum": 1000,
          "default": 0
        },
        "condition": {
          "$ref": "#/$defs/condition"
        },
        "outcome": {
          "$ref": "#/$defs/outcome"
        }
      },
      "additionalProperties": false
    },
    "condition": {
      "oneOf": [
        {
          "type": "object",
          "required": [
            "all"
          ],
          "properties": {
            "all": {
              "type": "array",
              "minItems": 1,
              "maxItems": 32,
              "items": {
                "$ref": "#/$defs/condition"
              }
            }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": [
            "any"
          ],
          "properties": {
            "any": {
              "type": "array",
              "minItems": 1,
              "maxItems": 32,
              "items": {
                "$ref": "#/$defs/condition"
              }
            }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": [
            "not"
          ],
          "properties": {
            "not": {
              "$ref": "#/$defs/condition"
            }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": [
            "field",
            "operator"
          ],
          "properties": {
            "field": {
              "type": "string",
              "pattern": "^[A-Za-z_][A-Za-z0-9_.-]{0,159}$"
            },
            "operator": {
              "type": "string",
              "enum": [
                "exists",
                "not_exists",
                "eq",
                "neq",
                "gt",
                "gte",
                "lt",
                "lte",
                "in",
                "not_in",
                "contains",
                "not_contains"
              ]
            },
            "value": true
          },
          "additionalProperties": false
        }
      ]
    },
    "outcome": {
      "type": "object",
      "required": [
        "decision",
        "verdict",
        "action",
        "reason_code"
      ],
      "properties": {
        "decision": {
          "type": "string",
          "enum": [
            "yes",
            "no",
            "review"
          ]
        },
        "verdict": {
          "type": "string",
          "pattern": "^[A-Z][A-Z0-9_]{1,79}$"
        },
        "action": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "reason_code": {
          "type": "string",
          "pattern": "^[A-Z][A-Z0-9_]{1,79}$"
        }
      },
      "additionalProperties": false
    }
  }
}
