{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://oildebt.ca/data/monitoring-schema.json",
  "title": "OilDebt.ca monitoring evidence snapshot",
  "description": "Schema for monitoring-evidence.json. It records positive source evidence and preserves unknowns; absence from an index is not evidence of absence.",
  "type": "object",
  "required": [
    "schemaVersion",
    "generatedAt",
    "sourceSnapshot",
    "datasetCoverage",
    "counts",
    "wells",
    "integrityEvidence",
    "integritySources",
    "integrityCoverage"
  ],
  "properties": {
    "schemaVersion": { "const": 1 },
    "generatedAt": { "type": "string", "format": "date-time" },
    "sourceSnapshot": {
      "type": "object",
      "required": ["name", "publishedAt", "inputFile", "url"],
      "properties": {
        "name": { "type": "string" },
        "publishedAt": { "type": "string", "format": "date" },
        "inputFile": { "type": "string" },
        "url": { "type": "string", "format": "uri" }
      },
      "additionalProperties": false
    },
    "datasetCoverage": {
      "type": "object",
      "additionalProperties": { "type": "string" }
    },
    "counts": {
      "type": "object",
      "required": ["owaManagedWellLicences"],
      "properties": {
        "owaManagedWellLicences": { "type": "integer", "minimum": 0 }
      },
      "additionalProperties": false
    },
    "wells": {
      "type": "object",
      "propertyNames": { "pattern": "^[0-9]{7}$" },
      "additionalProperties": { "$ref": "#/$defs/managedWell" }
    },
    "integrityEvidence": {
      "type": "object",
      "propertyNames": { "pattern": "^[0-9]{7}$" },
      "additionalProperties": { "$ref": "#/$defs/integrityRecord" }
    },
    "integritySources": {
      "type": "object",
      "properties": {
        "casingFailure": { "$ref": "#/$defs/integritySource" },
        "ventFlowGasMigration": { "$ref": "#/$defs/integritySource" }
      },
      "additionalProperties": false
    },
    "integrityCoverage": {
      "type": "object",
      "required": ["licencesWithAnyIntegrityReport", "owaManagedLicencesWithAnyIntegrityReport", "note"],
      "properties": {
        "licencesWithAnyIntegrityReport": { "type": "integer", "minimum": 0 },
        "owaManagedLicencesWithAnyIntegrityReport": { "type": "integer", "minimum": 0 },
        "note": { "type": "string" }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false,
  "$defs": {
    "managedWell": {
      "type": "object",
      "required": ["owaManagement", "designation", "source", "sourceDate", "site", "operator"],
      "properties": {
        "owaManagement": { "const": "confirmed" },
        "designation": { "const": "orphan_or_wipa" },
        "source": { "type": "string" },
        "sourceDate": { "type": "string", "format": "date" },
        "site": { "type": "string" },
        "operator": { "type": "string" }
      },
      "additionalProperties": false
    },
    "casingFailure": {
      "type": "object",
      "required": ["status", "reportDate", "resolutionDate", "failureType", "source"],
      "properties": {
        "status": { "$ref": "#/$defs/reportStatus" },
        "reportDate": { "type": ["string", "null"] },
        "resolutionDate": { "type": ["string", "null"] },
        "failureType": { "type": ["string", "null"] },
        "source": { "type": "string" }
      },
      "additionalProperties": false
    },
    "ventFlowGasMigration": {
      "type": "object",
      "required": ["status", "type", "classification", "reportDate", "resolutionDate", "reportedResolution", "flowSubstance", "flowRateM3Day", "source"],
      "properties": {
        "status": { "$ref": "#/$defs/reportStatus" },
        "type": { "type": ["string", "null"] },
        "classification": { "type": ["string", "null"] },
        "reportDate": { "type": ["string", "null"] },
        "resolutionDate": { "type": ["string", "null"] },
        "reportedResolution": { "type": ["string", "null"] },
        "flowSubstance": { "type": ["string", "null"] },
        "flowRateM3Day": { "type": ["string", "null"] },
        "source": { "type": "string" }
      },
      "additionalProperties": false
    },
    "integrityRecord": {
      "type": "object",
      "properties": {
        "casingFailures": {
          "type": "array",
          "items": { "$ref": "#/$defs/casingFailure" }
        },
        "ventFlowGasMigration": {
          "type": "array",
          "items": { "$ref": "#/$defs/ventFlowGasMigration" }
        }
      },
      "additionalProperties": false,
      "minProperties": 1
    },
    "integritySource": {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "retrievedAt": { "type": "string", "format": "date" },
        "url": { "type": "string", "format": "uri" },
        "exportUrl": { "type": "string", "format": "uri" },
        "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "inputBytes": { "type": "integer", "minimum": 0 },
        "inputRows": { "type": "integer", "minimum": 0 },
        "rowsWithLicence": { "type": "integer", "minimum": 0 },
        "rowsWithoutLicence": { "type": "integer", "minimum": 0 },
        "uniqueLicences": { "type": "integer", "minimum": 0 },
        "openOrUnresolvedReports": { "type": "integer", "minimum": 0 },
        "resolvedReports": { "type": "integer", "minimum": 0 },
        "joinedOpenOrUnresolvedReports": { "type": "integer", "minimum": 0 },
        "joinedResolvedReports": { "type": "integer", "minimum": 0 },
        "duplicateLicenceRows": { "type": "integer", "minimum": 0 },
        "exactDuplicateRows": { "type": "integer", "minimum": 0 },
        "st37SnapshotUniqueLicences": { "type": "integer", "minimum": 0 },
        "uniqueLicencesMatchedToSt37": { "type": "integer", "minimum": 0 },
        "uniqueLicencesUnmatchedToSt37": { "type": "integer", "minimum": 0 },
        "owaSnapshotUniqueLicences": { "type": "integer", "minimum": 0 },
        "uniqueLicencesMatchedToOwa": { "type": "integer", "minimum": 0 },
        "uniqueLicencesUnmatchedToOwa": { "type": "integer", "minimum": 0 }
      },
      "additionalProperties": false
    },
    "reportStatus": {
      "enum": ["reported_open_or_unresolved", "reported_resolved"]
    }
  }
}
