{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://vibebase.me/contracts/events/0.3.0",
  "title": "MutationEvent",
  "type": "object",
  "additionalProperties": false,
  "required": ["actor", "resource", "at"],
  "properties": {
    "actor": {
      "type": "object",
      "additionalProperties": false,
      "required": ["subjectId", "kind"],
      "properties": {
        "subjectId": { "type": "string", "minLength": 1 },
        "kind": {
          "anyOf": [
            { "$ref": "https://vibebase.me/contracts/principal/0.2.0#/$defs/kind" },
            { "const": "service" }
          ]
        },
        "onBehalfOf": {
          "type": "object",
          "additionalProperties": false,
          "required": ["subjectId", "kind"],
          "properties": {
            "subjectId": { "type": "string", "minLength": 1 },
            "kind": { "$ref": "https://vibebase.me/contracts/principal/0.2.0#/$defs/kind" }
          }
        }
      },
      "if": { "properties": { "kind": { "const": "service" } }, "required": ["kind"] },
      "else": { "properties": { "onBehalfOf": false } }
    },
    "resource": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "id"],
      "properties": {
        "type": { "type": "string", "minLength": 1 },
        "id": { "type": "string", "minLength": 1 }
      }
    },
    "change": {
      "type": "object",
      "additionalProperties": false,
      "properties": { "before": {}, "after": {} }
    },
    "at": { "type": "string", "format": "date-time" },
    "changesetRef": { "type": "string" },
    "provenance": { "enum": ["human-intent", "observed-proposal"] },
    "outcome": { "enum": ["applied", "denied", "refused"] },
    "reason": { "type": "string", "minLength": 1 },
    "correlationId": { "type": "string", "minLength": 1 },
    "generation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["model"],
      "properties": {
        "model": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 }
      }
    }
  },
  "dependentRequired": { "reason": ["outcome"] },
  "if": { "properties": { "outcome": { "enum": ["denied", "refused"] } }, "required": ["outcome"] },
  "then": { "properties": { "reason": true }, "required": ["reason"] }
}
