{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://interchange.stellarview.net/schemas/event.schema.json",
  "title": "An event on the Interchange: one thing happened to one asset. Every listener receives it at once.",
  "allOf": [
    {
      "$ref": "common.schema.json#/$defs/provenance"
    }
  ],
  "type": "object",
  "required": [
    "kind",
    "event_id",
    "event_type",
    "event_class",
    "severity",
    "occurred_at",
    "time_basis",
    "asset",
    "source_sensor",
    "fields",
    "catalogue",
    "source",
    "read_at",
    "validated_at"
  ],
  "unevaluatedProperties": false,
  "properties": {
    "kind": {
      "const": "event"
    },
    "event_id": {
      "$ref": "common.schema.json#/$defs/uuid"
    },
    "event_type": {
      "$ref": "common.schema.json#/$defs/event_type_id"
    },
    "event_class": {
      "enum": [
        "state",
        "alarm",
        "fault",
        "reading",
        "count",
        "transaction",
        "document",
        "media"
      ]
    },
    "severity": {
      "$ref": "common.schema.json#/$defs/severity"
    },
    "occurred_at": {
      "$ref": "common.schema.json#/$defs/time"
    },
    "time_basis": {
      "enum": [
        "source",
        "received"
      ],
      "description": "source: the sender stated the time. received: the sender sent none, so it is the Collector's receipt time, and says so."
    },
    "seq": {
      "type": "integer",
      "minimum": 1,
      "description": "The Collector's arrival order."
    },
    "late": {
      "type": "boolean",
      "description": "It occurred before an event already seen for the same asset."
    },
    "asset": {
      "$ref": "common.schema.json#/$defs/asset_ref",
      "description": "The target: what the event is about (the carousel). For a sensor's own fault, the sensor asset."
    },
    "fields": {
      "type": "object",
      "description": "The event type's fields, checked against the catalogue."
    },
    "derived_from": {
      "type": "object",
      "required": [
        "rule",
        "event_ids"
      ],
      "additionalProperties": false,
      "description": "Set when an analysis rule raised it.",
      "properties": {
        "rule": {
          "type": "string"
        },
        "event_ids": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "common.schema.json#/$defs/uuid"
          }
        }
      }
    },
    "catalogue": {
      "$ref": "common.schema.json#/$defs/catalogue"
    },
    "source": true,
    "read_at": true,
    "validated_at": true,
    "source_sensor": {
      "$ref": "common.schema.json#/$defs/source_sensor"
    },
    "trapped_by": {
      "type": "object",
      "required": [
        "trigger",
        "kind",
        "why",
        "from_event_id"
      ],
      "additionalProperties": false,
      "description": "Set when a trigger trapped this event from an incoming one.",
      "properties": {
        "trigger": {
          "type": "string"
        },
        "kind": {
          "enum": [
            "time",
            "area",
            "value",
            "trend",
            "image",
            "ai",
            "other",
            "interval_trend"
          ]
        },
        "why": {
          "type": "string"
        },
        "from_event_id": {
          "oneOf": [
            {
              "$ref": "common.schema.json#/$defs/uuid"
            },
            {
              "const": "none"
            }
          ]
        },
        "path": {
          "type": "string",
          "description": "The path the trigger pins, if it names one."
        },
        "confirmed_by": {
          "type": "string"
        }
      }
    },
    "path_selection": {
      "type": "object",
      "required": [
        "precedence",
        "chosen",
        "considered"
      ],
      "additionalProperties": false,
      "description": "Which paths the event took and why: the evidence of rule path selection.",
      "properties": {
        "precedence": {
          "type": "string"
        },
        "chosen": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "path",
              "mode",
              "why"
            ],
            "additionalProperties": false,
            "properties": {
              "path": {
                "type": "string"
              },
              "mode": {
                "enum": [
                  "exclusive",
                  "additive"
                ]
              },
              "why": {
                "type": "string"
              },
              "policy": {
                "type": "object"
              }
            }
          }
        },
        "considered": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "path",
              "matched",
              "why"
            ],
            "additionalProperties": false,
            "properties": {
              "path": {
                "type": "string"
              },
              "matched": {
                "type": "boolean"
              },
              "why": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  }
}