{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://crapwatch.example/schemas/flow_observation.schema.json",
  "title": "Crapwatch flow observation CSV row",
  "type": "object",
  "additionalProperties": false,
  "required": ["plant", "reporting_year", "period_raw", "period_type", "flow_stream", "value", "unit", "value_raw", "value_status", "measurement_method", "measurement_method_raw", "measurement_method_detail_raw", "source_document", "source_sha256", "source_page", "source_table", "source_header_raw", "source_row_raw", "extraction_status"],
  "properties": {
    "plant": {"type": "string", "minLength": 1},
    "reporting_year": {"const": "2024-25"},
    "period_raw": {"type": "string", "minLength": 1},
    "period_type": {"enum": ["MONTHLY", "ANNUAL", "EVENT", "OTHER"]},
    "flow_stream": {"enum": ["INFLUENT", "EFFLUENT_DISCHARGE_TO_WATERS", "REUSE", "BYPASS", "EMERGENCY_OUTFALL", "OTHER", "UNKNOWN"]},
    "value": {"type": ["number", "null"], "minimum": 0},
    "unit": {"enum": ["kL/day", "ML"]},
    "value_raw": {"type": "string", "minLength": 1},
    "value_status": {"enum": ["OK", "SOURCE_DOUBLE_DASH", "SOURCE_SINGLE_DASH"]},
    "measurement_method": {"enum": ["METERED", "CALCULATED", "ESTIMATED", "MIXED", "NOT_STATED", "NOT_APPLICABLE"]},
    "measurement_method_raw": {"type": "string"},
    "measurement_method_detail_raw": {"type": "string"},
    "source_document": {"type": "string", "pattern": "\\.pdf$"},
    "source_sha256": {"type": "string", "pattern": "^[0-9a-f]{64}$"},
    "source_page": {"type": "integer", "minimum": 1},
    "source_table": {"const": "Annual flow and rainfall data (Table x-B)"},
    "source_header_raw": {"type": "string", "minLength": 1},
    "source_row_raw": {"type": "string", "minLength": 1},
    "extraction_status": {"enum": [
      "TABLE_GRID_AND_HEADERS_VERIFIED",
      "HEADER_GEOMETRY_VERIFIED",
      "TABLE_GRID_AND_HEADERS_VERIFIED;PERIOD_SOURCE_ANNOTATION_PRESERVED",
      "TABLE_GRID_AND_HEADERS_VERIFIED;PERIOD_MALFORMED_SOURCE_PRESERVED"
    ]}
  },
  "allOf": [
    {"if": {"properties": {"value_status": {"const": "OK"}}}, "then": {"properties": {"value": {"type": "number"}}}},
    {"if": {"properties": {"value_status": {"enum": ["SOURCE_DOUBLE_DASH", "SOURCE_SINGLE_DASH"]}}}, "then": {"properties": {"value": {"type": "null"}}}}
  ]
}
