{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://law.arxo.io/schema/population-materialization.schema.json",
  "title": "Arxo population materialization manifest 0.1",
  "description": "Materialization manifest of the population-bind adapter: pins of the binding, the model, and the source files, the recorded case package's files with hashes, the ready bank rows (rowId, case, weight, legalTime), and the refused rows with codes. Canonical bytes, self-pinned contentHash excluding the contentHash field; it contains no time or host, so the bytes are equal across runners. rows are ordered by rowId bytes, package.files by path bytes, refused by table and key.",
  "$comment": "DECISION-0264 §6; SPEC §208",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "binding",
    "dataClass",
    "model",
    "sources",
    "package",
    "rows",
    "refused",
    "summary",
    "contentHash"
  ],
  "properties": {
    "schemaVersion": {
      "const": "law.population.materialization/0.1"
    },
    "binding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "contentHash"],
      "properties": {
        "id": {
          "$ref": "screen-binding.schema.json#/$defs/StableId"
        },
        "contentHash": {
          "$ref": "screen-binding.schema.json#/$defs/Digest"
        }
      }
    },
    "dataClass": {
      "enum": ["synthetic", "real"]
    },
    "model": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "version", "semanticHash"],
      "properties": {
        "name": {
          "$ref": "population-binding.schema.json#/$defs/PackageName"
        },
        "version": {
          "$ref": "population-binding.schema.json#/$defs/Version"
        },
        "semanticHash": {
          "$ref": "screen-binding.schema.json#/$defs/Digest"
        }
      }
    },
    "sources": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["table", "file", "contentHash", "rows"],
        "properties": {
          "table": {
            "$ref": "population-binding.schema.json#/$defs/TableName"
          },
          "file": {
            "type": "string",
            "minLength": 1
          },
          "contentHash": {
            "description": "sha256 of the exact bytes of the source file.",
            "$ref": "screen-binding.schema.json#/$defs/Digest"
          },
          "rows": {
            "description": "Number of data rows, excluding the header.",
            "type": "integer",
            "minimum": 0
          }
        }
      }
    },
    "package": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "version", "namespace", "files"],
      "properties": {
        "name": {
          "$ref": "population-binding.schema.json#/$defs/PackageName"
        },
        "version": {
          "$ref": "population-binding.schema.json#/$defs/Version"
        },
        "namespace": {
          "type": "string",
          "minLength": 1
        },
        "files": {
          "description": "All recorded case-package files: law.toml, package.law, and each case; path relative to the package directory.",
          "type": "array",
          "minItems": 2,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["path", "contentHash"],
            "properties": {
              "path": {
                "type": "string",
                "minLength": 1
              },
              "contentHash": {
                "$ref": "screen-binding.schema.json#/$defs/Digest"
              }
            }
          }
        }
      }
    },
    "rows": {
      "description": "Accepted rows — ready law.population.bank/0.1 bank rows plus the case's legalTime.",
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["rowId", "case", "weight", "legalTime"],
        "properties": {
          "rowId": {
            "$ref": "population-bank.schema.json#/$defs/Name"
          },
          "case": {
            "$ref": "population-bank.schema.json#/$defs/CaseName"
          },
          "weight": {
            "$ref": "population-bank.schema.json#/$defs/Weight"
          },
          "legalTime": {
            "type": "string",
            "minLength": 1
          }
        }
      }
    },
    "refused": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["table", "key", "code", "detail"],
        "properties": {
          "table": {
            "$ref": "population-binding.schema.json#/$defs/TableName"
          },
          "key": {
            "description": "Raw values of the key columns of the source row that caused the refusal.",
            "type": "array",
            "minItems": 1,
            "items": {
              "type": "string"
            }
          },
          "rowId": {
            "description": "rowId of the case that was not materialized; present if the template could be built.",
            "type": "string"
          },
          "code": {
            "enum": [
              "ROW_KEY_NULL",
              "ROW_REQUIRED_NULL",
              "ROW_TYPE_INVALID",
              "ROW_ENUM_UNKNOWN",
              "ROW_WEIGHT_INVALID",
              "ROW_DATE_INVALID",
              "ROW_FACT_LIMIT",
              "ROW_CARDINALITY"
            ]
          },
          "detail": {
            "type": "string",
            "minLength": 1
          }
        }
      }
    },
    "summary": {
      "type": "object",
      "additionalProperties": false,
      "required": ["cases", "facts", "refusedRows", "weightTotal"],
      "properties": {
        "cases": {
          "type": "integer",
          "minimum": 0
        },
        "facts": {
          "type": "integer",
          "minimum": 0
        },
        "refusedRows": {
          "type": "integer",
          "minimum": 0
        },
        "weightTotal": {
          "description": "Sum of the accepted rows' weights (exact decimal).",
          "$ref": "population-bank.schema.json#/$defs/Weight"
        }
      }
    },
    "contentHash": {
      "$ref": "screen-binding.schema.json#/$defs/Digest"
    }
  }
}
