{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://crapwatch.invalid/schemas/event.schema.json",
  "title": "Crapwatch wastewater event",
  "type": "object",
  "required": ["schema_version", "event_id", "asset_id", "state", "observed_at", "source"],
  "properties": {
    "schema_version": {"const": "1.0.0"},
    "event_id": {"type": "string", "minLength": 1},
    "asset_id": {"type": "string", "minLength": 1},
    "asset_class": {"enum": ["treatment_plant", "pump_station", "emergency_relief_structure", "storm_overflow", "network", "receiving_water_monitor", "other", "unknown"]},
    "event_class": {"enum": ["treatment_bypass", "sewage_spill", "emergency_outfall_discharge", "potential_discharge", "telemetry_fault", "other", "unknown"]},
    "event_class_raw": {"type": ["string", "null"]},
    "classification_authority": {"enum": ["source_reported", "regulator_assigned", "operator_assigned", "derived", "manual_review", "unknown"]},
    "classification_confidence": {"enum": ["high", "medium", "low", "unknown"]},
    "treatment_state": {"enum": ["untreated", "partially_treated", "treated", "mixed", "not_applicable", "unknown"]},
    "cause": {
      "type": ["object", "null"],
      "required": ["category", "raw"],
      "properties": {
        "category": {"enum": ["hydraulic_overload", "blockage", "mechanical_failure", "power_failure", "telemetry_failure", "planned_work", "other", "unknown"]},
        "raw": {"type": ["string", "null"]},
        "authority": {"enum": ["source_reported", "derived", "manual_review", "unknown"]}
      },
      "additionalProperties": false
    },
    "receiving_environment": {
      "type": ["object", "null"],
      "properties": {
        "name": {"type": ["string", "null"]},
        "type": {"enum": ["marine", "estuary", "river", "stream", "lake", "groundwater", "land", "stormwater", "other", "unknown"]},
        "identifier": {"type": ["string", "null"]},
        "source_raw": {"type": ["string", "null"]},
        "verification_status": {"enum": ["source_reported", "independently_verified", "inferred", "unknown"]}
      },
      "additionalProperties": false
    },
    "regulatory_classification": {
      "type": ["object", "null"],
      "required": ["scheme", "value", "authority"],
      "properties": {
        "scheme": {"type": "string", "minLength": 1},
        "value": {"type": ["string", "null"]},
        "authority": {"type": ["string", "null"]},
        "source_raw": {"type": ["string", "null"]}
      },
      "additionalProperties": false
    },
    "verification_status": {"enum": ["unverified", "source_verified", "cross_source_verified", "disputed", "unknown"]},
    "state": {"enum": ["active", "ended", "unknown", "offline"]},
    "observed_at": {"type": "string", "format": "date-time"},
    "event_started": {"type": ["string", "null"], "format": "date-time"},
    "event_ended": {"type": ["string", "null"], "format": "date-time"},
    "volume": {
      "type": ["object", "null"],
      "required": ["value", "unit", "method"],
      "properties": {
        "value": {"type": ["number", "null"], "minimum": 0},
        "unit": {"enum": ["L", "kL", "ML", "unknown"]},
        "method": {"enum": ["measured", "estimated", "unknown"]},
        "raw": {"type": ["string", "null"]}
      },
      "additionalProperties": false
    },
    "monitor": {
      "type": "object",
      "required": ["coverage", "liveness", "last_seen"],
      "properties": {
        "coverage": {"enum": ["monitored", "inferred", "not_monitored", "unknown"]},
        "liveness": {"enum": ["online", "offline", "degraded", "unknown"]},
        "last_seen": {"type": ["string", "null"], "format": "date-time"},
        "heartbeat_interval_seconds": {"type": ["integer", "null"], "minimum": 1},
        "operability_percent": {"type": ["number", "null"], "minimum": 0, "maximum": 100},
        "outage_reason": {"type": ["string", "null"]}
      },
      "additionalProperties": false
    },
    "sequence": {
      "type": "object",
      "properties": {
        "number": {"type": ["integer", "null"], "minimum": 0},
        "gap_detected": {"type": "boolean"}
      },
      "additionalProperties": false
    },
    "revision": {
      "type": "object",
      "required": ["number", "revised_at"],
      "properties": {
        "number": {"type": "integer", "minimum": 0},
        "revised_at": {"type": "string", "format": "date-time"},
        "reason": {"type": ["string", "null"]},
        "supersedes": {"type": ["string", "null"]}
      },
      "additionalProperties": false
    },
    "source": {"$ref": "provenance.schema.json"}
  },
  "allOf": [
    {"if": {"properties": {"state": {"const": "ended"}}}, "then": {"required": ["event_started", "event_ended"], "properties": {"event_started": {"type": "string", "format": "date-time"}, "event_ended": {"type": "string", "format": "date-time"}}}},
    {"if": {"properties": {"state": {"const": "offline"}}}, "then": {"required": ["monitor"]}}
  ],
  "additionalProperties": false
}
