{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://mergegrounds.chawax.chatgpt.site/schemas/decision-v1.schema.json",
  "title": "MergeGrounds admission decision v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema", "decision", "evaluated_at", "subject", "policy", "controls",
    "evidence_digests", "reason_codes", "issues", "decision_digest", "signed"
  ],
  "properties": {
    "schema": {"const": "https://mergegrounds.chawax.chatgpt.site/schemas/decision-v1.schema.json"},
    "decision": {"enum": ["admit", "deny"]},
    "evaluated_at": {"type": "string", "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d{1,6})?Z$"},
    "subject": {
      "type": "object", "additionalProperties": false,
      "required": ["repository", "commit", "tree", "base_commit", "canonical_diff_digest"],
      "properties": {
        "repository": {"type": "string"},
        "commit": {"type": "string", "pattern": "^(?:[0-9a-f]{40}|[0-9a-f]{64})$"},
        "tree": {"type": "string", "pattern": "^(?:[0-9a-f]{40}|[0-9a-f]{64})$"},
        "base_commit": {"type": "string", "pattern": "^(?:[0-9a-f]{40}|[0-9a-f]{64})$"},
        "canonical_diff_digest": {"type": "string", "pattern": "^sha256:[0-9a-f]{64}$"}
      }
    },
    "policy": {
      "type": "object", "additionalProperties": false,
      "required": ["policy_id", "version", "digest"],
      "properties": {
        "policy_id": {"type": "string"},
        "version": {"type": "string"},
        "digest": {"type": "string", "pattern": "^sha256:[0-9a-f]{64}$"}
      }
    },
    "controls": {
      "type": "array",
      "items": {
        "type": "object", "additionalProperties": false,
        "required": ["control_id", "state", "satisfied", "evidence_id", "evidence_digest"],
        "properties": {
          "control_id": {"type": "string"},
          "state": {"enum": ["pass", "fail", "not_evaluated", "waived"]},
          "satisfied": {"type": "boolean"},
          "evidence_id": {"type": ["string", "null"], "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"},
          "evidence_digest": {"type": ["string", "null"]}
        }
      }
    },
    "evidence_digests": {"type": "array", "uniqueItems": true, "items": {"type": "string", "pattern": "^sha256:[0-9a-f]{64}$"}},
    "reason_codes": {"type": "array", "uniqueItems": true, "items": {"type": "string"}},
    "issues": {
      "type": "array",
      "items": {
        "type": "object", "additionalProperties": false,
        "required": ["code", "detail", "control_id", "evidence_id"],
        "properties": {
          "code": {"type": "string"},
          "detail": {"type": "string"},
          "control_id": {"type": ["string", "null"]},
          "evidence_id": {"type": ["string", "null"]}
        }
      }
    },
    "decision_digest": {"type": "string", "pattern": "^sha256:[0-9a-f]{64}$"},
    "signed": {"type": "boolean"},
    "signature": {
      "type": "object", "additionalProperties": false,
      "required": ["algorithm", "key_id", "value"],
      "properties": {
        "algorithm": {"const": "ed25519"},
        "key_id": {"type": "string"},
        "value": {"type": "string", "pattern": "^[A-Za-z0-9_-]{86}$"}
      }
    }
  },
  "allOf": [
    {"if": {"properties": {"signed": {"const": true}}}, "then": {"required": ["signature"]}},
    {"if": {"properties": {"signed": {"const": false}}}, "then": {"not": {"required": ["signature"]}}}
  ]
}
