{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://vetdatastandards.com/schemas/v0.1/document.schema.json",
  "title": "Document",
  "description": "A file in the export bundle. A URL-only pointer is not valid.",
  "type": "object",
  "additionalProperties": false,
  "required": ["id", "practiceId", "title", "documentType", "content", "source"],
  "properties": {
    "id": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/id" },
    "source": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/source" },
    "matchKeys": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/matchKeys" },
    "createdAt": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/dateTime" },
    "updatedAt": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/dateTime" },
    "deletedAt": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/dateTime" },
    "active": { "type": "boolean" },
    "extensions": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/extensions" },
    "practiceId": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/id" },
    "clientId": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/id" },
    "patientId": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/id" },
    "consultationId": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/id" },
    "invoiceId": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/id" },
    "prescriptionId": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/id" },
    "title": { "type": "string", "minLength": 1, "maxLength": 255 },
    "documentType": {
      "type": "string",
      "enum": [
        "clinical_record",
        "invoice",
        "prescription",
        "vaccination_certificate",
        "image",
        "consent",
        "lab_report",
        "other"
      ]
    },
    "documentTypeRaw": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/rawText" },
    "content": {
      "type": "object",
      "additionalProperties": false,
      "required": ["fileName", "mimeType"],
      "properties": {
        "fileName": { "type": "string", "minLength": 1, "maxLength": 255 },
        "mimeType": { "type": "string", "minLength": 1, "maxLength": 128 },
        "inlineBase64": {
          "type": "string",
          "minLength": 1,
          "contentEncoding": "base64"
        },
        "bundlePath": {
          "type": "string",
          "minLength": 1,
          "maxLength": 512,
          "description": "Relative path inside the export directory. No .., no absolute path, no URL."
        },
        "byteSize": { "type": "integer", "minimum": 0 },
        "sha256": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        }
      },
      "anyOf": [
        { "required": ["inlineBase64"] },
        { "required": ["bundlePath"] }
      ]
    }
  },
  "if": {
    "properties": { "documentType": { "const": "other" } },
    "required": ["documentType"]
  },
  "then": {
    "required": ["documentTypeRaw"]
  }
}
