{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://crapwatch.example/schemas/release_event.schema.json",
  "title": "Crapwatch candidate evidence-preserving release/incident collection",
  "type": "object",
  "additionalProperties": false,
  "required": ["dataset_status", "record_count", "bypass_table_row_count", "narrative_incident_row_count", "records"],
  "properties": {
    "dataset_status": {"const": "CANDIDATE_EVIDENCE_PRESERVING_NOT_CANONICAL"},
    "record_count": {"type": "integer", "minimum": 0},
    "bypass_table_row_count": {"type": "integer", "minimum": 0},
    "narrative_incident_row_count": {"type": "integer", "minimum": 0},
    "records": {"type": "array", "items": {"$ref": "#/$defs/record"}}
  },
  "$defs": {
    "sourceStatus": {"enum": ["SOURCE_STATED", "NOT_STATED", "SOURCE_UNKNOWN"]},
    "record": {
      "type": "object",
      "additionalProperties": false,
      "required": ["event_id", "plant", "reporting_year", "source_document", "source_sha256", "source_page", "source_section", "record_kind", "source_event_label", "raw_source_text", "normalized_summary", "source_text", "source_classification", "canonical_class", "interpretation_status", "classification_basis", "classification_confidence", "event_disposition", "start_raw", "date_confidence", "end_raw", "end_status", "volume_value", "volume_unit", "volume_value_status", "volume_method", "treatment_state_source_stated", "treatment_state_status", "receiving_environment_source_stated", "receiving_environment_status", "cause_source_stated", "cause_status", "regulatory_classification_source_stated", "regulatory_classification_status", "source_asset_reference_raw", "source_asset_reference_status", "human_verification_state", "uncertainty"],
      "properties": {
        "event_id": {"type": "string", "pattern": "^(BYP-[0-9]{4}|NAR-[A-Z0-9-]+)$"},
        "plant": {"type": "string", "minLength": 1},
        "reporting_year": {"const": "2024-25"},
        "source_document": {"type": "string", "pattern": "\\.pdf$"},
        "source_sha256": {"type": "string", "pattern": "^[0-9a-f]{64}$"},
        "source_page": {"type": "integer", "minimum": 1},
        "source_section": {"type": "string", "minLength": 1},
        "record_kind": {"enum": ["BYPASS_TABLE_ROW", "NARRATIVE_INCIDENT_ROW"]},
        "source_event_label": {"type": "string", "minLength": 1},
        "raw_source_text": {"type": "string", "minLength": 1},
        "normalized_summary": {"type": "string", "minLength": 1},
        "source_text": {"type": "string", "minLength": 1},
        "source_classification": {"type": "string", "minLength": 1},
        "canonical_class": {"enum": ["ABNORMAL_LAGOON_EFFLUENT_DISCHARGE", "CHEMICAL_INCIDENT_NO_ENVIRONMENTAL_RELEASE", "COMPLAINT_WITH_SOURCE_COUNTEREVIDENCE", "CONTROLLED_TREATMENT_INCIDENT_DATE_UNRESOLVED", "EFFLUENT_EMERGENCY_OUTFALL_RELEASE", "EFFLUENT_QUALITY_INCIDENT_DURING_DISCHARGE", "EMERGENCY_LAGOON_RELEASE", "HYDRAULIC_INCIDENT_RELEASE_NOT_STATED", "INFLUENT_PROCESS_INCIDENT_RELEASE_NOT_STATED", "LAGOON_EFFLUENT_RELEASE", "LIQUID_SLUDGE_RELEASE", "LOWER_QUALITY_EFFLUENT_OUTFALL_RELEASE", "NON_CHLORINATED_LAGOON_SPILLWAY_RELEASE", "PARTIALLY_TREATED_EFFLUENT_RELEASE", "POWER_INCIDENT_DATE_UNRESOLVED", "POWER_INCIDENT_RELEASE_NOT_STATED", "PREVENTED_EFFLUENT_RELEASE", "PREVENTED_NETWORK_OVERFLOW", "PROCESS_INCIDENT_RELEASE_NOT_STATED", "REDIRECTED_EFFLUENT_DISCHARGE", "REUSE_LAGOON_OVERFLOW", "REUSE_SYSTEM_EFFLUENT_RELEASE", "STP_BYPASS_EVENT", "TREATED_EFFLUENT_RELEASE", "TREATMENT_DEGRADATION_RELEASE_NOT_STATED", "TREATMENT_INCIDENT_RELEASE_NOT_STATED", "TREATMENT_UPSET_NO_SEPARATE_RELEASE_STATED"]},
        "interpretation_status": {"const": "CANDIDATE_NOT_CANONICAL"},
        "classification_basis": {"enum": ["SOURCE_SECTION_AND_EVENT_ROW", "INFERRED_FROM_SOURCE_TEXT"]},
        "classification_confidence": {"enum": ["HIGH", "MEDIUM", "LOW"]},
        "event_disposition": {"enum": ["RELEASE_OR_INTERNAL_BYPASS_AS_SOURCE_DEFINED", "RELEASE_OCCURRED", "RELEASE_PREVENTED", "NO_RELEASE_EXPLICIT", "RELEASE_NOT_STATED", "RELEASE_STATUS_UNRESOLVED", "NO_SEPARATE_RELEASE_STATED", "RELEASE_NOT_ESTABLISHED", "ABNORMAL_DISCHARGE_STATE_STATED"]},
        "start_raw": {"type": ["string", "null"]},
        "date_confidence": {"enum": ["SOURCE_DATE_EXACT", "SOURCE_DATE_OUTSIDE_REPORTING_PERIOD", "MULTIPLE_SOURCE_DATES"]},
        "end_raw": {"type": ["string", "null"]},
        "end_status": {"$ref": "#/$defs/sourceStatus"},
        "volume_value": {"type": ["number", "null"], "minimum": 0},
        "volume_unit": {"type": ["string", "null"], "enum": ["L", "kL", "ML", null]},
        "volume_value_status": {"$ref": "#/$defs/sourceStatus"},
        "volume_method": {"enum": ["SOURCE_EVENT_TABLE_ESTIMATE", "SOURCE_STATED", "SOURCE_UNKNOWN", "NOT_STATED"]},
        "treatment_state_source_stated": {"type": ["string", "null"]},
        "treatment_state_status": {"$ref": "#/$defs/sourceStatus"},
        "receiving_environment_source_stated": {"type": ["string", "null"]},
        "receiving_environment_status": {"$ref": "#/$defs/sourceStatus"},
        "cause_source_stated": {"type": ["string", "null"]},
        "cause_status": {"$ref": "#/$defs/sourceStatus"},
        "regulatory_classification_source_stated": {"type": ["string", "null"]},
        "regulatory_classification_status": {"$ref": "#/$defs/sourceStatus"},
        "source_asset_reference_raw": {"type": ["string", "null"]},
        "source_asset_reference_status": {"enum": ["SOURCE_STATED_DESCRIPTION", "SOURCE_STATED_FORMAL_IDENTIFIER", "NOT_STATED", "SOURCE_UNKNOWN"]},
        "human_verification_state": {"enum": ["PARSER_OUTPUT_NOT_INDIVIDUALLY_PAGE_CHECKED", "MANUAL_SOURCE_PAGE_REVIEW_RECORDED"]},
        "uncertainty": {"type": ["string", "null"]}
      },
      "allOf": [
        {"if": {"properties": {"record_kind": {"const": "BYPASS_TABLE_ROW"}}}, "then": {"properties": {"classification_basis": {"const": "SOURCE_SECTION_AND_EVENT_ROW"}, "human_verification_state": {"const": "PARSER_OUTPUT_NOT_INDIVIDUALLY_PAGE_CHECKED"}}}},
        {"if": {"properties": {"volume_value_status": {"const": "SOURCE_STATED"}}}, "then": {"properties": {"volume_value": {"type": "number"}, "volume_unit": {"enum": ["L", "kL", "ML"]}}}},
        {"if": {"properties": {"volume_value_status": {"enum": ["NOT_STATED", "SOURCE_UNKNOWN"]}}}, "then": {"properties": {"volume_value": {"type": "null"}}}},
        {"if": {"properties": {"end_status": {"const": "SOURCE_STATED"}}}, "then": {"properties": {"end_raw": {"type": "string", "minLength": 1}}}},
        {"if": {"properties": {"end_status": {"enum": ["NOT_STATED", "SOURCE_UNKNOWN"]}}}, "then": {"properties": {"end_raw": {"type": "null"}}}},
        {"if": {"properties": {"treatment_state_status": {"const": "SOURCE_STATED"}}}, "then": {"properties": {"treatment_state_source_stated": {"type": "string", "minLength": 1}}}},
        {"if": {"properties": {"treatment_state_status": {"enum": ["NOT_STATED", "SOURCE_UNKNOWN"]}}}, "then": {"properties": {"treatment_state_source_stated": {"type": "null"}}}},
        {"if": {"properties": {"receiving_environment_status": {"const": "SOURCE_STATED"}}}, "then": {"properties": {"receiving_environment_source_stated": {"type": "string", "minLength": 1}}}},
        {"if": {"properties": {"receiving_environment_status": {"enum": ["NOT_STATED", "SOURCE_UNKNOWN"]}}}, "then": {"properties": {"receiving_environment_source_stated": {"type": "null"}}}},
        {"if": {"properties": {"cause_status": {"const": "SOURCE_STATED"}}}, "then": {"properties": {"cause_source_stated": {"type": "string", "minLength": 1}}}},
        {"if": {"properties": {"cause_status": {"enum": ["NOT_STATED", "SOURCE_UNKNOWN"]}}}, "then": {"properties": {"cause_source_stated": {"type": "null"}}}},
        {"if": {"properties": {"regulatory_classification_status": {"const": "SOURCE_STATED"}}}, "then": {"properties": {"regulatory_classification_source_stated": {"type": "string", "minLength": 1}}}},
        {"if": {"properties": {"regulatory_classification_status": {"enum": ["NOT_STATED", "SOURCE_UNKNOWN"]}}}, "then": {"properties": {"regulatory_classification_source_stated": {"type": "null"}}}},
        {"if": {"properties": {"source_asset_reference_status": {"enum": ["SOURCE_STATED_DESCRIPTION", "SOURCE_STATED_FORMAL_IDENTIFIER"]}}}, "then": {"properties": {"source_asset_reference_raw": {"type": "string", "minLength": 1}}}},
        {"if": {"properties": {"source_asset_reference_status": {"enum": ["NOT_STATED", "SOURCE_UNKNOWN"]}}}, "then": {"properties": {"source_asset_reference_raw": {"type": "null"}}}}
      ]
    }
  }
}
