{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://prepaire.example/contracts/kg-evidence-bundle/3.0.0",
  "title": "PREPAIRE-KG-EVIDENCE-BUNDLE@3.0.0",
  "type": "object",
  "additionalProperties": false,
  "required": ["contract", "bundleId", "subjectRef", "runId", "observations", "crossAxisSignals", "provenance"],
  "properties": {
    "contract": { "const": "PREPAIRE-KG-EVIDENCE-BUNDLE@3.0.0" },
    "bundleId": { "type": "string", "format": "uuid" },
    "subjectRef": {
      "type": "string",
      "minLength": 1,
      "description": "A pseudonymous subject identifier. Direct identifiers are prohibited."
    },
    "runId": { "type": "string", "pattern": "^RUN-" },
    "observations": {
      "type": "array",
      "items": { "$ref": "#/$defs/observation" }
    },
    "crossAxisSignals": {
      "type": "array",
      "items": { "$ref": "#/$defs/crossAxisSignal" }
    },
    "provenance": { "$ref": "#/$defs/provenance" },
    "typedEvidence": { "$ref": "typed-evidence.schema.json" }
  },
  "$defs": {
    "codedConcept": {
      "type": "object",
      "additionalProperties": false,
      "required": ["system", "code", "display", "version"],
      "properties": {
        "system": { "type": "string", "format": "uri" },
        "code": { "type": "string", "minLength": 1 },
        "display": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 }
      }
    },
    "interval": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "source", "version", "confidence"],
      "properties": {
        "kind": { "enum": ["population", "personal_baseline", "homeostatic_target"] },
        "low": { "type": ["number", "null"] },
        "high": { "type": ["number", "null"] },
        "unit": { "type": ["string", "null"] },
        "source": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 },
        "confidence": { "type": "number", "minimum": 0, "maximum": 1 },
        "applicability": { "type": "string" }
      }
    },
    "observation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "observationId",
        "code",
        "specimen",
        "effectiveAt",
        "sourceArtifactDrsId",
        "qualityState",
        "referenceIntervals"
      ],
      "properties": {
        "observationId": { "type": "string", "format": "uuid" },
        "code": { "$ref": "#/$defs/codedConcept" },
        "localCode": { "type": "string" },
        "valueQuantity": {
          "type": "object",
          "additionalProperties": false,
          "required": ["value", "unit", "unitSystem"],
          "properties": {
            "value": { "type": "number" },
            "unit": { "type": "string", "minLength": 1 },
            "unitSystem": { "const": "http://unitsofmeasure.org" }
          }
        },
        "valueCode": { "$ref": "#/$defs/codedConcept" },
        "specimen": { "$ref": "#/$defs/codedConcept" },
        "method": { "$ref": "#/$defs/codedConcept" },
        "effectiveAt": { "type": "string", "format": "date-time" },
        "sourceArtifactDrsId": { "type": "string", "minLength": 1 },
        "qualityState": { "enum": ["accepted", "qualified", "rejected"] },
        "flags": { "type": "array", "items": { "type": "string" } },
        "referenceIntervals": {
          "type": "array",
          "items": { "$ref": "#/$defs/interval" }
        }
      },
      "oneOf": [
        { "required": ["valueQuantity"] },
        { "required": ["valueCode"] }
      ]
    },
    "crossAxisSignal": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "signalId",
        "label",
        "inputObservationIds",
        "model",
        "intendedUse",
        "confidence",
        "explanation",
        "causalStatus"
      ],
      "properties": {
        "signalId": { "type": "string", "format": "uuid" },
        "label": { "type": "string", "minLength": 1 },
        "inputObservationIds": {
          "type": "array",
          "minItems": 2,
          "uniqueItems": true,
          "items": { "type": "string", "format": "uuid" }
        },
        "model": {
          "type": "object",
          "additionalProperties": false,
          "required": ["name", "version", "ociDigest", "validationDecisionId"],
          "properties": {
            "name": { "type": "string" },
            "version": { "type": "string" },
            "ociDigest": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" },
            "validationDecisionId": { "type": "string", "format": "uuid" }
          }
        },
        "intendedUse": { "enum": ["clinical_support", "research", "informational"] },
        "confidence": { "type": "number", "minimum": 0, "maximum": 1 },
        "explanation": { "type": "string", "minLength": 1 },
        "causalStatus": {
          "enum": ["association_only", "mechanistically_supported", "clinically_validated"],
          "description": "The default is association_only. Causal language requires explicit governed evidence."
        }
      }
    },
    "provenance": {
      "type": "object",
      "additionalProperties": false,
      "required": ["instrumentAssetId", "instrumentSerial", "pipelineDefinitionSha256", "softwareBuilds", "terminologyVersions"],
      "properties": {
        "instrumentAssetId": { "type": "string" },
        "instrumentSerial": { "type": "string" },
        "pipelineDefinitionSha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "softwareBuilds": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["packageKey", "version", "imageDigest"],
            "properties": {
              "packageKey": { "type": "string" },
              "version": { "type": "string" },
              "imageDigest": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" }
            }
          }
        },
        "terminologyVersions": {
          "type": "object",
          "required": ["loinc", "ucum"],
          "properties": {
            "loinc": { "type": "string" },
            "ucum": { "type": "string" },
            "snomedCt": { "type": "string" }
          }
        }
      }
    }
  }
}
