{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.helloorigo.com/chronicle/audio-entry-payload-v2.schema.json",
  "title": "Chronicle decrypted per-entry audio export payload v2",
  "type": "object",
  "additionalProperties": false,
  "required": ["exportFormatVersion", "exportId", "generatedAt", "producer", "custody", "entry", "sources", "audioFiles", "derivations", "integrityEvidence"],
  "properties": {
    "exportFormatVersion": { "const": 2 },
    "exportId": { "$ref": "#/$defs/uuid" },
    "generatedAt": { "$ref": "#/$defs/timestamp" },
    "producer": {
      "type": "object", "additionalProperties": false, "required": ["name", "version"],
      "properties": { "name": { "type": "string", "minLength": 1 }, "version": { "type": "string", "minLength": 1 } }
    },
    "custody": {
      "type": "object", "additionalProperties": false, "required": ["leavesRuntimeCustody", "containerProtection", "deletionReach", "statement"],
      "properties": {
        "leavesRuntimeCustody": { "const": true },
        "containerProtection": { "const": "password_encrypted_container" },
        "deletionReach": { "const": "does_not_reach_this_copy" },
        "statement": { "type": "string", "minLength": 1 }
      }
    },
    "entry": {
      "type": "object", "additionalProperties": false,
      "required": ["id", "sourceIds", "status", "transcriptStatus"],
      "properties": {
        "id": { "$ref": "#/$defs/uuid" },
        "sourceIds": { "type": "array", "minItems": 1, "uniqueItems": true, "items": { "$ref": "#/$defs/uuid" } },
        "status": { "enum": ["live", "erased"] },
        "transcriptStatus": { "enum": ["pending", "available", "unavailable"] }
      }
    },
    "sources": { "type": "array", "items": { "$ref": "source-record-v2.schema.json" } },
    "audioFiles": {
      "type": "array", "items": {
        "type": "object", "additionalProperties": false, "required": ["sourceId", "bytesBase64"],
        "properties": {
          "sourceId": { "$ref": "#/$defs/uuid" },
          "bytesBase64": { "type": "string", "minLength": 4, "pattern": "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$" }
        }
      }
    },
    "derivations": {
      "type": "array", "items": {
        "type": "object", "additionalProperties": false,
        "required": ["id", "kind", "sourceIds", "content", "createdAt", "provenance"],
        "properties": {
          "id": { "$ref": "#/$defs/uuid" },
          "kind": { "enum": ["transcript", "user_edited_transcript", "review_passage"] },
          "sourceIds": { "type": "array", "minItems": 1, "uniqueItems": true, "items": { "$ref": "#/$defs/uuid" } },
          "content": { "type": "string" },
          "createdAt": { "$ref": "#/$defs/timestamp" },
          "provenance": {
            "oneOf": [
              { "type": "object", "additionalProperties": false,
                "required": ["method", "recognizer", "modelId", "modelVersion", "executionPath"],
                "properties": {
                  "method": { "const": "speech_recognition" },
                  "recognizer": { "type": "string", "minLength": 1 },
                  "modelId": { "type": "string", "minLength": 1 },
                  "modelVersion": { "type": "string", "minLength": 1 },
                  "executionPath": { "enum": ["on_device", "server", "provider"] }
                }
              },
              { "type": "object", "additionalProperties": false,
                "required": ["method", "actorType"],
                "properties": { "method": { "enum": ["human_review", "human_edit"] }, "actorType": { "const": "human" } }
              }
            ]
          },
          "basedOnDerivationId": { "$ref": "#/$defs/uuid" }
        },
        "allOf": [
          { "if": { "properties": { "kind": { "const": "transcript" } }, "required": ["kind"] },
            "then": { "properties": { "provenance": { "properties": { "method": { "const": "speech_recognition" } } } } } },
          { "if": { "properties": { "kind": { "const": "review_passage" } }, "required": ["kind"] },
            "then": { "properties": { "provenance": { "properties": { "method": { "const": "human_review" } } } } } },
          { "if": { "properties": { "kind": { "const": "user_edited_transcript" } }, "required": ["kind"] },
            "then": { "required": ["basedOnDerivationId"], "properties": { "provenance": { "properties": { "method": { "const": "human_edit" } } } } },
            "else": { "not": { "required": ["basedOnDerivationId"] } } }
        ]
      }
    },
    "integrityEvidence": {
      "oneOf": [
        { "type": "null" },
        { "type": "object", "additionalProperties": false, "required": ["bundle", "export"],
          "properties": {
            "bundle": { "$ref": "witness-receipt-bundle-v2.schema.json" },
            "export": { "$ref": "integrity-evidence-export-v2.schema.json" }
          }
        }
      ]
    },
    "integrityEvidenceNote": { "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}$" },
    "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$" }
  },
  "allOf": [
    { "if": { "properties": { "integrityEvidence": { "type": "null" } }, "required": ["integrityEvidence"] },
      "then": { "required": ["integrityEvidenceNote"] },
      "else": { "not": { "required": ["integrityEvidenceNote"] } } },
    { "if": { "properties": { "entry": { "properties": { "status": { "const": "erased" } }, "required": ["status"] } }, "required": ["entry"] },
      "then": { "properties": { "sources": { "maxItems": 0 }, "audioFiles": { "maxItems": 0 }, "derivations": { "maxItems": 0 }, "entry": { "properties": { "transcriptStatus": { "const": "unavailable" } } } } } }
  ]
}
