{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://law.arxo.io/schema/screen-binding.schema.json",
  "title": "Arxo population screening source binding 0.1",
  "description": "Pinned mapping from exact case-assertable relations to a source schema: which tables and columns back each relation used in population screening.",
  "$comment": "DECISION-0120",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "id",
    "packageSemanticHash",
    "source",
    "population",
    "assertableContract",
    "relations",
    "contentHash"
  ],
  "properties": {
    "schemaVersion": {
      "const": "law.screen.binding/0.1"
    },
    "id": {
      "$ref": "#/$defs/StableId"
    },
    "packageSemanticHash": {
      "$ref": "#/$defs/Digest"
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schemaId",
        "schemaContentHash",
        "snapshotPolicy",
        "timezone",
        "collation"
      ],
      "properties": {
        "schemaId": {
          "$ref": "#/$defs/StableId"
        },
        "schemaContentHash": {
          "$ref": "#/$defs/Digest"
        },
        "snapshotPolicy": {
          "const": "same_snapshot_for_screen_and_evaluate"
        },
        "timezone": {
          "type": "string",
          "minLength": 1
        },
        "collation": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "population": {
      "type": "object",
      "additionalProperties": false,
      "required": ["table", "subjectColumn", "subjectType", "goalArgument"],
      "properties": {
        "table": {
          "$ref": "#/$defs/SqlIdentifier"
        },
        "subjectColumn": {
          "$ref": "#/$defs/SqlIdentifier"
        },
        "subjectType": {
          "$ref": "legal-ir.schema.json#/$defs/TypeRef"
        },
        "goalArgument": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "assertableContract": {
      "type": "object",
      "additionalProperties": false,
      "required": ["coverage", "nullSemantics"],
      "properties": {
        "coverage": {
          "const": "exact_relations"
        },
        "nullSemantics": {
          "const": "unknown_pass"
        }
      }
    },
    "relations": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/RelationBinding"
      }
    },
    "contentHash": {
      "$ref": "#/$defs/Digest"
    }
  },
  "$defs": {
    "StableId": {
      "type": "string",
      "minLength": 1
    },
    "Digest": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "SqlIdentifier": {
      "type": "string",
      "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
    },
    "RelationBinding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["predicate", "table", "arguments", "presence"],
      "properties": {
        "predicate": {
          "$ref": "#/$defs/StableId"
        },
        "table": {
          "$ref": "#/$defs/SqlIdentifier"
        },
        "arguments": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/ArgumentBinding"
          }
        },
        "presence": {
          "oneOf": [
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["kind"],
              "properties": {
                "kind": {
                  "const": "row"
                }
              }
            },
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["kind", "column", "trueValue"],
              "properties": {
                "kind": {
                  "const": "boolean_column"
                },
                "column": {
                  "$ref": "#/$defs/SqlIdentifier"
                },
                "trueValue": {
                  "type": ["boolean", "integer", "string"]
                }
              }
            }
          ]
        }
      }
    },
    "ArgumentBinding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["position", "column", "type", "unit"],
      "properties": {
        "position": {
          "type": "integer",
          "minimum": 0
        },
        "column": {
          "$ref": "#/$defs/SqlIdentifier"
        },
        "type": {
          "$ref": "legal-ir.schema.json#/$defs/TypeRef"
        },
        "unit": {
          "type": ["string", "null"]
        }
      }
    }
  }
}
