{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.driftframe.org/site-reading-governance/v2.0/data-reconciliation.schema.json",
  "title": "Database Data Reconciliation",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "reconciliation_id",
    "migration_receipt_id",
    "migration_plan_id",
    "data_contract_id",
    "scope",
    "evidence_mode",
    "assurance_stage",
    "cohort",
    "criteria",
    "exceptions",
    "overall_outcome",
    "performed_at",
    "performer",
    "reviewer",
    "retest_triggers",
    "metadata"
  ],
  "properties": {
    "reconciliation_id": {
      "$ref": "common.schema.json#/$defs/identifier"
    },
    "migration_receipt_id": {
      "$ref": "common.schema.json#/$defs/identifier"
    },
    "migration_plan_id": {
      "$ref": "common.schema.json#/$defs/identifier"
    },
    "data_contract_id": {
      "$ref": "common.schema.json#/$defs/identifier"
    },
    "scope": {
      "type": "string",
      "minLength": 20,
      "maxLength": 1200
    },
    "evidence_mode": {
      "$ref": "common.schema.json#/$defs/evidenceMode"
    },
    "assurance_stage": {
      "$ref": "common.schema.json#/$defs/assuranceStage"
    },
    "cohort": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "cohort_id",
        "query_digest",
        "fixture_digest",
        "selection_basis",
        "record_count"
      ],
      "properties": {
        "cohort_id": {
          "$ref": "common.schema.json#/$defs/identifier"
        },
        "query_digest": {
          "$ref": "common.schema.json#/$defs/digest"
        },
        "fixture_digest": {
          "$ref": "common.schema.json#/$defs/digest"
        },
        "selection_basis": {
          "type": "string",
          "minLength": 10,
          "maxLength": 700
        },
        "record_count": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "criteria": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "criterion_id",
          "dimension",
          "comparison",
          "value_type",
          "expected_value",
          "observed_value",
          "tolerance",
          "unit",
          "outcome",
          "evidence"
        ],
        "properties": {
          "criterion_id": {
            "$ref": "common.schema.json#/$defs/identifier"
          },
          "dimension": {
            "type": "string",
            "enum": [
              "row_count",
              "checksum",
              "null_count",
              "duplicate_count",
              "orphan_count",
              "domain_invariant",
              "unit_invariant",
              "temporal_invariant",
              "scientific_invariant"
            ]
          },
          "comparison": {
            "type": "string",
            "enum": [
              "exactly",
              "at_least",
              "at_most",
              "within_absolute_tolerance"
            ]
          },
          "value_type": {
            "type": "string",
            "enum": [
              "integer",
              "exact_decimal",
              "digest",
              "boolean",
              "controlled_text"
            ]
          },
          "expected_value": {
            "type": "string",
            "minLength": 1,
            "maxLength": 200
          },
          "observed_value": {
            "type": "string",
            "minLength": 1,
            "maxLength": 200
          },
          "tolerance": {
            "type": "string",
            "pattern": "^(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?$",
            "maxLength": 120
          },
          "unit": {
            "type": "string",
            "minLength": 1,
            "maxLength": 80
          },
          "outcome": {
            "type": "string",
            "enum": [
              "passed",
              "failed",
              "inconclusive"
            ]
          },
          "evidence": {
            "$ref": "common.schema.json#/$defs/artifact"
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "value_type": {
                  "enum": [
                    "integer",
                    "exact_decimal"
                  ]
                }
              },
              "required": [
                "value_type"
              ]
            },
            "then": {
              "properties": {
                "expected_value": {
                  "pattern": "^-?(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?$"
                },
                "observed_value": {
                  "pattern": "^-?(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?$"
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "value_type": {
                  "const": "digest"
                }
              },
              "required": [
                "value_type"
              ]
            },
            "then": {
              "properties": {
                "expected_value": {
                  "pattern": "^sha256:[a-f0-9]{64}$"
                },
                "observed_value": {
                  "pattern": "^sha256:[a-f0-9]{64}$"
                },
                "comparison": {
                  "const": "exactly"
                },
                "tolerance": {
                  "const": "0"
                }
              }
            }
          }
        ]
      }
    },
    "exceptions": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "exception_id",
          "criterion_id",
          "record_locator",
          "disposition",
          "owner_role"
        ],
        "properties": {
          "exception_id": {
            "$ref": "common.schema.json#/$defs/identifier"
          },
          "criterion_id": {
            "$ref": "common.schema.json#/$defs/identifier"
          },
          "record_locator": {
            "$ref": "common.schema.json#/$defs/governedLocator"
          },
          "disposition": {
            "type": "string",
            "enum": [
              "corrected",
              "accepted_with_control",
              "successor_migration_planned"
            ]
          },
          "owner_role": {
            "type": "string",
            "minLength": 2,
            "maxLength": 120
          }
        }
      }
    },
    "overall_outcome": {
      "type": "string",
      "enum": [
        "passed",
        "failed",
        "inconclusive"
      ]
    },
    "performed_at": {
      "type": "string",
      "format": "date-time",
      "pattern": "Z$"
    },
    "performer": {
      "$ref": "common.schema.json#/$defs/actor"
    },
    "reviewer": {
      "$ref": "common.schema.json#/$defs/actor"
    },
    "retest_triggers": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": {
        "type": "string",
        "minLength": 5,
        "maxLength": 300
      }
    },
    "metadata": {
      "$ref": "common.schema.json#/$defs/recordMetadata"
    }
  }
}
