{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.hxtory.app/hxtory-json/v1.schema.json",
  "title": "Hxtory JSON, version 1",
  "description": "Cases and entries of a personal medical history, prepared by the patient's own AI from their documents and imported into Hxtory. No personal details: Hxtory keeps those separately. Hxtory ignores fields this schema doesn't list.",
  "type": "object",
  "required": ["format", "version", "entries"],
  "additionalProperties": false,
  "properties": {
    "format": { "const": "hxtory-json" },
    "version": { "const": 1 },
    "generator": { "type": "string", "description": "The AI that prepared the file, e.g. its model name." },
    "language": { "type": "string", "description": "BCP-47 tag of the documents' main language, e.g. uk." },
    "cases": {
      "type": "array",
      "description": "The stories the entries belong to. A few, not one per diagnosis.",
      "items": {
        "type": "object",
        "required": ["ref", "title"],
        "additionalProperties": false,
        "properties": {
          "ref": { "type": "string", "description": "Unique in this file, e.g. c1. Entries point to it." },
          "title": { "type": "string" },
          "type": { "enum": ["chronic-condition", "acute-event", "investigation", "routine", "congenital"] },
          "rationale": { "type": "string", "description": "Why these entries belong together. Never what they mean." }
        }
      }
    },
    "sources": {
      "type": "array",
      "description": "The documents the entries were read from. Only described: no file, no text.",
      "items": {
        "type": "object",
        "required": ["ref", "title"],
        "additionalProperties": false,
        "properties": {
          "ref": { "type": "string", "description": "Unique in this file, e.g. s1. Entries point to it." },
          "title": { "type": "string", "description": "What the document is, e.g. Біохімічний аналіз крові." },
          "document_type": {
            "enum": ["discharge_summary", "lab_report", "imaging_report", "prescription", "referral",
                     "consultation_note", "vaccination_record", "operative_report", "other"]
          },
          "document_date": { "$ref": "#/$defs/date" },
          "facility": { "type": "string" }
        }
      }
    },
    "entries": {
      "type": "array",
      "maxItems": 5000,
      "items": {
        "type": "object",
        "required": ["kind", "text"],
        "additionalProperties": false,
        "properties": {
          "kind": { "enum": ["condition", "observation", "medication", "procedure", "encounter", "allergy", "immunization"] },
          "text": { "type": "string", "description": "The name only, as the document words it, e.g. Креатинін. No value, unit or flag." },
          "value": { "type": "string", "description": "Exactly as printed, with its unit and qualifiers, e.g. 145 мкмоль/л." },
          "reference_range": { "type": "string", "description": "Only as printed." },
          "source_flagged": { "type": "boolean", "description": "True only when the document itself marks the value (H, L, *, an arrow, bold)." },
          "date": { "$ref": "#/$defs/date" },
          "date_as_written": { "type": "string", "description": "The date as printed, e.g. 12.04.2019." },
          "code": {
            "type": "object",
            "required": ["system", "code", "display"],
            "additionalProperties": false,
            "properties": {
              "system": { "enum": ["icd-10", "loinc", "atc"] },
              "code": { "type": "string" },
              "display": { "type": "string" }
            }
          },
          "condition_status": { "enum": ["active", "recurrence", "remission", "resolved"], "description": "For a condition, only when the document says." },
          "medication_status": { "enum": ["active", "stopped"], "description": "For a medication, only when the document says." },
          "no_known_allergies": { "type": "boolean", "description": "For an allergy entry that states no known allergies." },
          "cases": { "type": "array", "items": { "type": "string" }, "description": "Refs of the cases it belongs to. Empty or missing: in no case." },
          "source": { "type": "string", "description": "Ref of the source it was read from. Missing: told, not read." }
        }
      }
    }
  },
  "$defs": {
    "date": {
      "type": "string",
      "pattern": "^\\d{4}(-\\d{2}(-\\d{2})?)?$",
      "description": "YYYY, YYYY-MM or YYYY-MM-DD: never more precise than the document."
    }
  }
}
