{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "notarius.dnd.item_occurrences",
  "type": "object",
  "additionalProperties": false,
  "required": ["occurrences"],
  "properties": {
    "occurrences": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["item_id", "name", "kind", "source_refs"],
        "properties": {
          "item_id": {"type": "string", "minLength": 1},
          "name": {"type": "string", "minLength": 1},
          "kind": {"type": "string", "enum": ["discovered", "acquired", "lost", "consumed", "transferred"]},
          "quantity": {"type": "integer", "minimum": 1},
          "from": {"type": "string", "minLength": 1},
          "to": {"type": "string", "minLength": 1},
          "source_refs": {
            "type": "array",
            "minItems": 1,
            "items": {
              "type": "object",
              "additionalProperties": false,
              "required": ["source_id", "start_unit_id", "end_unit_id"],
              "properties": {
                "source_id": {"type": "string", "minLength": 1},
                "start_unit_id": {"type": "integer", "minimum": 1},
                "end_unit_id": {"type": "integer", "minimum": 1}
              }
            }
          }
        },
        "allOf": [
          {"if": {"properties": {"kind": {"const": "discovered"}}, "required": ["kind"]}, "then": {"not": {"anyOf": [{"required": ["from"]}, {"required": ["to"]}]}}},
          {"if": {"properties": {"kind": {"const": "acquired"}}, "required": ["kind"]}, "then": {"required": ["to"], "not": {"required": ["from"]}}},
          {"if": {"properties": {"kind": {"const": "lost"}}, "required": ["kind"]}, "then": {"required": ["from"], "not": {"required": ["to"]}}},
          {"if": {"properties": {"kind": {"const": "consumed"}}, "required": ["kind"]}, "then": {"required": ["from"], "not": {"required": ["to"]}}},
          {"if": {"properties": {"kind": {"const": "transferred"}}, "required": ["kind"]}, "then": {"required": ["from", "to"]}}
        ]
      }
    }
  }
}
