{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://probeward.dev/schemas/webhook-payload-v1.json",
  "title": "Probeward notification payload, schema_version 1",
  "description": "The JSON body Probeward posts to webhooks. The same document is stored with every notification event and rendered for email, Discord and Telegram. Fields may be added in a later minor release; receivers should ignore fields they do not know. A breaking change raises schema_version.",
  "type": "object",
  "required": [
    "schema_version",
    "event_id",
    "event_type",
    "simulated",
    "test",
    "occurred_at",
    "summary",
    "group_path",
    "dependency_path",
    "suppressed",
    "recent_changes",
    "links"
  ],
  "additionalProperties": false,
  "properties": {
    "schema_version": { "const": 1 },
    "event_id": {
      "$ref": "#/$defs/uuid",
      "description": "Identifies this notification. Identical on every retry; deduplicate on it."
    },
    "event_type": {
      "enum": ["opened", "repeated", "recovered", "test", "simulated"]
    },
    "simulated_event": {
      "enum": ["opened", "repeated", "recovered"],
      "description": "Present only when event_type is simulated: what the simulated notification stands for."
    },
    "simulated": {
      "type": "boolean",
      "description": "True for anything caused by Simulate Error. Nothing real is wrong."
    },
    "test": {
      "type": "boolean",
      "description": "True for a test notification. Nothing is wrong."
    },
    "delayed": {
      "const": true,
      "description": "Present only on an opening sent late: the destination's notification schedule opened while the incident was already open. The summary ends with \"(delayed, open for ...)\"."
    },
    "occurred_at": { "$ref": "#/$defs/timestamp" },
    "summary": {
      "type": "string",
      "minLength": 1,
      "description": "One line for humans, for example \"DOWN web1 / HTTP — connection refused\". Starts with SIMULATED or TEST for those events."
    },
    "incident": { "$ref": "#/$defs/incident" },
    "probe": { "$ref": "#/$defs/named" },
    "device": { "$ref": "#/$defs/device" },
    "group_path": {
      "type": "array",
      "items": { "$ref": "#/$defs/named" },
      "description": "Outermost group first."
    },
    "sensor": { "$ref": "#/$defs/sensor" },
    "status": { "$ref": "#/$defs/status" },
    "trigger": { "$ref": "#/$defs/trigger" },
    "last_good": { "$ref": "#/$defs/last_good" },
    "dependency_path": {
      "type": "array",
      "items": { "$ref": "#/$defs/dependency" },
      "description": "The checks this sensor depends on, nearest first."
    },
    "suppressed": {
      "type": "array",
      "items": { "$ref": "#/$defs/suppressed" },
      "description": "Checks held back because this one is failing; each is not paged on its own."
    },
    "recent_changes": {
      "type": "array",
      "items": { "$ref": "#/$defs/change" },
      "description": "Recent state changes on the device, oldest first."
    },
    "links": { "$ref": "#/$defs/links" }
  },
  "allOf": [
    {
      "if": { "properties": { "event_type": { "const": "simulated" } } },
      "then": {
        "required": ["simulated_event"],
        "properties": { "simulated": { "const": true }, "test": { "const": false } }
      },
      "else": { "not": { "required": ["simulated_event"] } }
    },
    {
      "if": { "properties": { "event_type": { "const": "test" } } },
      "then": { "properties": { "test": { "const": true }, "simulated": { "const": false } } },
      "else": { "required": ["incident", "probe", "trigger"], "properties": { "test": { "const": false } } }
    },
    {
      "if": { "properties": { "event_type": { "enum": ["opened", "repeated", "recovered"] } } },
      "then": { "properties": { "simulated": { "const": false } } }
    }
  ],
  "$defs": {
    "uuid": { "type": "string", "format": "uuid" },
    "timestamp": { "type": "string", "format": "date-time" },
    "named": {
      "type": "object",
      "required": ["id", "name"],
      "additionalProperties": false,
      "properties": { "id": { "$ref": "#/$defs/uuid" }, "name": { "type": "string" } }
    },
    "device": {
      "type": "object",
      "required": ["id", "name"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/uuid" },
        "name": { "type": "string" },
        "address": {
          "type": "string",
          "anyOf": [{ "format": "ipv4" }, { "format": "ipv6" }]
        }
      }
    },
    "sensor": {
      "type": "object",
      "required": ["id", "name", "kind"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/uuid" },
        "name": { "type": "string" },
        "kind": { "type": "string", "description": "Sensor kind, for example ping, http_status or snmp_cpu." }
      }
    },
    "health": { "enum": ["up", "warning", "down"] },
    "severity": { "enum": ["warning", "down"] },
    "incident": {
      "type": "object",
      "required": ["id", "severity", "opened_at", "duration_secs", "repeat_number"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/uuid" },
        "severity": { "$ref": "#/$defs/severity" },
        "opened_at": { "$ref": "#/$defs/timestamp" },
        "closed_at": {
          "$ref": "#/$defs/timestamp",
          "description": "Present on recovered."
        },
        "duration_secs": {
          "type": "integer",
          "minimum": 0,
          "description": "From opening to occurred_at; on recovered, the incident's whole duration."
        },
        "repeat_number": {
          "type": "integer",
          "minimum": 0,
          "description": "1 for the first reminder, 2 for the second; 0 otherwise."
        },
        "acknowledged": { "$ref": "#/$defs/ack" }
      }
    },
    "ack": {
      "type": "object",
      "required": ["by_user_id", "at"],
      "additionalProperties": false,
      "properties": {
        "by_user_id": { "$ref": "#/$defs/uuid" },
        "by_name": { "type": "string" },
        "at": { "$ref": "#/$defs/timestamp" },
        "note": { "type": "string" }
      }
    },
    "unknown_reason": {
      "type": "object",
      "required": ["reason"],
      "properties": {
        "reason": {
          "enum": [
            "never_sampled",
            "stale",
            "probe_not_reporting",
            "awaiting_poll_after_pause",
            "awaiting_poll_after_reconnect",
            "awaiting_poll_after_schedule",
            "awaiting_poll_after_maintenance",
            "sources_unknown"
          ]
        },
        "last_result": { "anyOf": [{ "$ref": "#/$defs/timestamp" }, { "type": "null" }] },
        "probe": { "$ref": "#/$defs/uuid" }
      },
      "additionalProperties": false
    },
    "status": {
      "type": "object",
      "required": ["freshness", "failing", "handling"],
      "additionalProperties": false,
      "properties": {
        "health": {
          "$ref": "#/$defs/health",
          "description": "From the last successful poll; absent until one succeeds."
        },
        "freshness": { "enum": ["fresh", "collection_failure", "unknown"] },
        "unknown_reason": { "$ref": "#/$defs/unknown_reason" },
        "failing": { "type": "boolean" },
        "handling": {
          "type": "object",
          "required": ["acknowledged", "paused", "simulated"],
          "additionalProperties": false,
          "properties": {
            "acknowledged": { "type": "boolean" },
            "paused": { "type": "boolean" },
            "simulated": { "type": "boolean" },
            "suppressed_by_sensor_id": { "$ref": "#/$defs/uuid" },
            "awaiting_dependency_sensor_id": { "$ref": "#/$defs/uuid" },
            "maintenance": {
              "type": "object",
              "description": "The maintenance window the sensor is in. Of overlapping windows, the one that ends last.",
              "required": ["window_id", "name", "until", "stop_polling"],
              "additionalProperties": false,
              "properties": {
                "window_id": { "$ref": "#/$defs/uuid" },
                "name": { "type": "string" },
                "until": { "$ref": "#/$defs/timestamp" },
                "stop_polling": { "type": "boolean" }
              }
            },
            "outside_schedule": {
              "type": "object",
              "description": "The sensor's monitoring schedule is closed: it is not polled.",
              "required": ["schedule_id", "name"],
              "additionalProperties": false,
              "properties": {
                "schedule_id": { "$ref": "#/$defs/uuid" },
                "name": { "type": "string" },
                "until": {
                  "$ref": "#/$defs/timestamp",
                  "description": "When the schedule next opens; absent if it never does."
                }
              }
            }
          }
        }
      }
    },
    "channel": {
      "type": "object",
      "required": ["key", "name", "unit"],
      "additionalProperties": false,
      "properties": {
        "key": { "type": "string" },
        "name": { "type": "string" },
        "unit": { "type": "string", "description": "Empty for a plain number." }
      }
    },
    "trigger": {
      "type": "object",
      "required": ["type"],
      "oneOf": [
        {
          "properties": {
            "type": { "const": "limit" },
            "channel": { "$ref": "#/$defs/channel" },
            "value": { "type": "number" },
            "value_label": { "type": "string", "description": "What the value stands for on a channel that reports a state, when the channel has a label for it." },
            "limit": { "type": "number" },
            "bound": { "enum": ["error_max", "error_min", "warn_max", "warn_min"] },
            "health": { "$ref": "#/$defs/health" }
          },
          "required": ["channel", "value", "limit", "bound", "health"],
          "additionalProperties": false
        },
        {
          "properties": {
            "type": { "const": "failure" },
            "kind": { "enum": ["timeout", "unreachable", "auth_failed", "protocol", "config", "internal"] },
            "message": { "type": "string" }
          },
          "required": ["kind", "message"],
          "additionalProperties": false
        },
        {
          "properties": {
            "type": { "const": "verdict" },
            "health": { "$ref": "#/$defs/health" },
            "message": { "type": "string" }
          },
          "required": ["health"],
          "additionalProperties": false
        },
        {
          "properties": {
            "type": { "const": "simulated" },
            "by_user_id": { "$ref": "#/$defs/uuid" },
            "by_name": { "type": "string" }
          },
          "required": ["by_user_id"],
          "additionalProperties": false
        },
        {
          "properties": { "type": { "const": "probe_not_reporting" } },
          "additionalProperties": false
        }
      ]
    },
    "value": {
      "type": "object",
      "required": ["key", "name", "unit", "value"],
      "additionalProperties": false,
      "properties": {
        "key": { "type": "string" },
        "name": { "type": "string" },
        "unit": { "type": "string" },
        "value": {
          "type": ["number", "null"],
          "description": "null when unknown for that poll, for example a rate just after a counter reset."
        },
        "label": { "type": "string", "description": "What the value stands for on a channel that reports a state, when the channel has a label for it." }
      }
    },
    "last_good": {
      "type": "object",
      "required": ["started", "finished", "age_secs", "health", "channels"],
      "additionalProperties": false,
      "properties": {
        "started": { "$ref": "#/$defs/timestamp" },
        "finished": { "$ref": "#/$defs/timestamp" },
        "age_secs": { "type": "integer", "minimum": 0 },
        "health": { "$ref": "#/$defs/health" },
        "message": { "type": "string" },
        "channels": { "type": "array", "items": { "$ref": "#/$defs/value" } }
      }
    },
    "dependency": {
      "type": "object",
      "required": ["sensor", "device", "kind", "status"],
      "additionalProperties": false,
      "properties": {
        "sensor": { "$ref": "#/$defs/sensor" },
        "device": { "$ref": "#/$defs/named" },
        "kind": { "enum": ["master_check", "device", "sensor"] },
        "status": { "$ref": "#/$defs/status" }
      }
    },
    "suppressed": {
      "type": "object",
      "required": ["sensor", "device", "by_sensor_id", "reason"],
      "additionalProperties": false,
      "properties": {
        "sensor": { "$ref": "#/$defs/sensor" },
        "device": { "$ref": "#/$defs/named" },
        "by_sensor_id": { "$ref": "#/$defs/uuid" },
        "reason": { "type": "string" }
      }
    },
    "change": {
      "type": "object",
      "required": ["at", "sensor_id", "sensor_name", "before", "after"],
      "additionalProperties": false,
      "properties": {
        "at": { "$ref": "#/$defs/timestamp" },
        "sensor_id": { "$ref": "#/$defs/uuid" },
        "sensor_name": { "type": "string" },
        "before": { "$ref": "#/$defs/status" },
        "after": { "$ref": "#/$defs/status" }
      }
    },
    "links": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "ui_incident": { "type": "string", "format": "uri" },
        "ui_sensor": { "type": "string", "format": "uri" },
        "api_incident": { "type": "string", "format": "uri" },
        "api_sensor": { "type": "string", "format": "uri" },
        "api_deliveries": { "type": "string", "format": "uri" }
      }
    }
  }
}
