{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.local/skierowanie_mp_input.schema.json",
  "title": "Skierowanie MP v0.3 — dane wejściowe generatora",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "document",
    "patient",
    "author",
    "employer",
    "purpose",
    "position",
    "exposure_groups",
    "additional_exams"
  ],
  "properties": {
    "identifiers": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "document_id_root": { "type": "string" },
        "patient_hr_id_root": { "type": "string" },
        "pesel_root": { "type": "string" },
        "regon_root": { "type": "string" },
        "employer_internal_id_root": { "type": "string" },
        "position_code_system": { "type": "string" }
      }
    },
    "document": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id_extension", "effective_time"],
      "properties": {
        "id_extension": { "type": "string", "minLength": 1 },
        "effective_time": { "type": "string", "minLength": 1 },
        "version_number": { "type": "integer", "minimum": 1 },
        "set_id_extension": { "type": "string" }
      }
    },
    "patient": {
      "type": "object",
      "additionalProperties": false,
      "required": ["pesel", "given", "family", "gender", "birth_date", "address"],
      "properties": {
        "hr_id_extension": { "type": "string" },
        "pesel": { "type": "string", "pattern": "^[0-9]{11}$" },
        "given": { "type": "string" },
        "family": { "type": "string" },
        "gender": { "type": "string", "enum": ["M", "F"] },
        "birth_date": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" },
        "address": { "$ref": "#/$defs/postalAddressPatient" }
      }
    },
    "author": {
      "type": "object",
      "additionalProperties": false,
      "required": ["pesel", "given", "family"],
      "properties": {
        "pesel": { "type": "string", "pattern": "^[0-9]{11}$" },
        "prefix": { "type": "string" },
        "given": { "type": "string" },
        "family": { "type": "string" }
      }
    },
    "employer": {
      "type": "object",
      "additionalProperties": false,
      "required": ["regon", "internal_id", "name", "telecom", "address"],
      "properties": {
        "regon": { "type": "string" },
        "internal_id": { "type": "string" },
        "name": { "type": "string" },
        "telecom": { "type": "string" },
        "address": { "$ref": "#/$defs/postalAddressEmployer" }
      }
    },
    "purpose": {
      "type": "object",
      "additionalProperties": false,
      "required": ["exam_type_code", "exam_type_display"],
      "properties": {
        "exam_type_code": { "type": "string" },
        "exam_type_display": { "type": "string" },
        "last_exam_date": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" },
        "current_exam_valid_until": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" }
      }
    },
    "position": {
      "type": "object",
      "additionalProperties": false,
      "required": ["position_code", "position_display", "description"],
      "properties": {
        "position_code": { "type": "string" },
        "position_display": { "type": "string" },
        "description": { "type": "string" }
      }
    },
    "exposure_groups": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/exposureGroup" }
    },
    "additional_exams": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/additionalExam" }
    },
    "additional_info": { "type": ["string", "null"] }
  },
  "$defs": {
    "postalAddressPatient": {
      "type": "object",
      "additionalProperties": false,
      "required": ["city", "postal_code", "street_name", "house_number"],
      "properties": {
        "city": { "type": "string" },
        "postal_code": { "type": "string" },
        "street_name": { "type": "string" },
        "house_number": { "type": "string" },
        "unit_id": { "type": "string" }
      }
    },
    "postalAddressEmployer": {
      "type": "object",
      "additionalProperties": false,
      "required": ["country", "city", "postal_code", "street_name", "house_number"],
      "properties": {
        "country": { "type": "string" },
        "city": { "type": "string" },
        "postal_code": { "type": "string" },
        "street_name": { "type": "string" },
        "house_number": { "type": "string" }
      }
    },
    "quantity": {
      "type": "object",
      "additionalProperties": false,
      "required": ["value", "unit"],
      "properties": {
        "value": { "type": "number" },
        "unit": { "type": "string" }
      }
    },
    "exposureFactor": {
      "type": "object",
      "additionalProperties": false,
      "required": ["factor_code", "display_name"],
      "properties": {
        "factor_code": { "type": "string" },
        "display_name": { "type": "string" },
        "measurement_date": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" },
        "measured_display": { "type": "string" },
        "measured_value": {
          "oneOf": [{ "type": "null" }, { "$ref": "#/$defs/quantity" }]
        },
        "norm": {
          "oneOf": [{ "type": "null" }, { "$ref": "#/$defs/quantity" }]
        },
        "exposure_time": {
          "oneOf": [{ "type": "null" }, { "$ref": "#/$defs/quantity" }]
        },
        "extra_description": { "type": "string" }
      }
    },
    "exposureGroup": {
      "type": "object",
      "additionalProperties": false,
      "required": ["group_code", "group_display", "factors"],
      "properties": {
        "group_code": { "type": "string" },
        "group_display": { "type": "string" },
        "factors": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/exposureFactor" }
        }
      }
    },
    "additionalExam": {
      "type": "object",
      "additionalProperties": false,
      "required": ["icd9_code", "display_name", "narrative_line"],
      "properties": {
        "icd9_code": { "type": "string" },
        "display_name": { "type": "string" },
        "narrative_line": { "type": "string" }
      }
    }
  }
}
