{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://tattvabodha.com/schemas/tattvabodha-karma-event-v1.0.0.schema.json",
  "title": "Tattvabodha Karma Event",
  "description": "Portable JSON representation of an immutable event. Firestore stores occurredAt and server metadata as Timestamps.",
  "type": "object",
  "required": ["schemaVersion", "type", "source", "occurredAt", "subject", "effects"],
  "properties": {
    "schemaVersion": { "const": 1 },
    "type": { "type": "string", "minLength": 1 },
    "source": { "type": "string", "enum": ["loomer", "pinon", "tattvabodha", "hello-island-flower"] },
    "occurredAt": { "type": "string", "format": "date-time" },
    "sessionId": { "$ref": "#/$defs/StableId" },
    "subject": { "$ref": "#/$defs/Entity" },
    "data": { "type": "object", "additionalProperties": true },
    "effects": { "type": "array", "items": { "$ref": "#/$defs/Effect" }, "maxItems": 400 },
    "serverMetadata": {
      "type": "object",
      "properties": {
        "createdAt": { "type": "string", "format": "date-time" },
        "processedAt": { "type": "string", "format": "date-time" },
        "processingStatus": { "type": "string", "enum": ["pending", "processing", "processed", "failed"] }
      },
      "additionalProperties": false
    }
  },
  "$defs": {
    "StableId": { "type": "string", "minLength": 1, "maxLength": 200, "pattern": "^[A-Za-z0-9_-]+$" },
    "Entity": {
      "type": "object",
      "required": ["type", "id"],
      "properties": {
        "type": { "type": "string", "minLength": 1 },
        "id": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },
    "Effect": {
      "type": "object",
      "required": ["entity", "measure", "amount"],
      "properties": {
        "entity": { "type": "string", "pattern": "^[a-z][a-z0-9_-]*:.+$" },
        "measure": { "type": "string", "minLength": 1 },
        "amount": { "type": "number", "not": { "const": 0 } }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}
