{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.helloorigo.com/chronicle/source-record-v2.schema.json",
  "title": "Chronicle Audio Source Record v2",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "id", "captureSource", "sourceCapturedAt", "createdAt", "preservation", "preservedBy", "fidelity", "audio"],
  "properties": {
    "schemaVersion": { "const": 2 },
    "id": { "$ref": "#/$defs/uuid" },
    "captureSource": { "const": "voice_audio" },
    "sourceCapturedAt": { "$ref": "#/$defs/timestamp" },
    "createdAt": { "$ref": "#/$defs/timestamp" },
    "preservation": { "const": "explicit_approval" },
    "preservedBy": { "$ref": "#/$defs/humanActor" },
    "fidelity": { "const": "original_audio" },
    "author": { "$ref": "#/$defs/humanActor" },
    "captureActor": { "$ref": "#/$defs/captureActor" },
    "custodyRef": { "type": "string", "minLength": 1 },
    "audio": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mediaType", "byteLength", "sha256", "encoding"],
      "properties": {
        "mediaType": { "const": "audio/ogg; codecs=opus" },
        "byteLength": { "type": "integer", "minimum": 1 },
        "sha256": { "$ref": "#/$defs/sha256" },
        "encoding": {
          "type": "object",
          "additionalProperties": false,
          "required": ["codec", "container", "sampleRateHz", "channels", "bitrateMode", "targetBitrateBps", "encoder"],
          "properties": {
            "codec": { "const": "opus" },
            "container": { "const": "ogg" },
            "sampleRateHz": { "type": "integer", "minimum": 8000 },
            "channels": { "type": "integer", "minimum": 1, "maximum": 8 },
            "bitrateMode": { "enum": ["vbr", "cbr"] },
            "targetBitrateBps": { "type": "integer", "minimum": 6000 },
            "encoder": {
              "type": "object",
              "additionalProperties": false,
              "required": ["name", "version"],
              "properties": {
                "name": { "type": "string", "minLength": 1 },
                "version": { "type": "string", "minLength": 1 }
              }
            }
          }
        }
      }
    }
  },
  "$defs": {
    "uuid": { "type": "string", "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" },
    "sha256": { "type": "string", "pattern": "^[0-9a-f]{64}$" },
    "timestamp": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}Z$" },
    "humanActor": {
      "type": "object", "additionalProperties": false, "required": ["actorType"],
      "properties": { "actorType": { "const": "human" }, "identityRef": { "type": "string", "minLength": 1 } }
    },
    "captureActor": {
      "type": "object", "additionalProperties": false, "required": ["actorType"],
      "properties": { "actorType": { "enum": ["human", "system", "device", "import"] }, "identityRef": { "type": "string", "minLength": 1 } }
    }
  }
}
