{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Bounded requirement extraction record",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "source",
    "stage",
    "page_count",
    "pages_without_text",
    "fields"
  ],
  "properties": {
    "schema_version": {
      "const": "1.0"
    },
    "source": {
      "type": "object",
      "required": [
        "url",
        "sha256",
        "bytes"
      ],
      "properties": {
        "url": {
          "type": "string"
        },
        "sha256": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        },
        "bytes": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "stage": {
      "enum": [
        "automatic",
        "reviewed"
      ]
    },
    "page_count": {
      "type": "integer",
      "minimum": 1
    },
    "pages_without_text": {
      "type": "array",
      "items": {
        "type": "integer",
        "minimum": 1
      }
    },
    "fields": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "field",
          "value",
          "status",
          "evidence",
          "reviewed_pages",
          "note"
        ],
        "properties": {
          "field": {
            "type": "string"
          },
          "value": {
            "type": [
              "string",
              "null"
            ]
          },
          "status": {
            "enum": [
              "extracted",
              "needs_review",
              "ambiguous",
              "not_found"
            ]
          },
          "note": {
            "type": "string"
          },
          "reviewed_pages": {
            "type": "array",
            "uniqueItems": true,
            "items": {
              "type": "integer",
              "minimum": 1
            }
          },
          "evidence": {
            "type": "array",
            "items": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "page",
                "quote",
                "method"
              ],
              "properties": {
                "page": {
                  "type": "integer",
                  "minimum": 1
                },
                "quote": {
                  "type": "string",
                  "minLength": 1
                },
                "method": {
                  "enum": [
                    "pypdf-text",
                    "visual-transcription"
                  ]
                }
              }
            }
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "status": {
                  "enum": [
                    "not_found",
                    "needs_review"
                  ]
                }
              }
            },
            "then": {
              "properties": {
                "value": {
                  "type": "null"
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "status": {
                  "enum": [
                    "extracted",
                    "ambiguous"
                  ]
                }
              }
            },
            "then": {
              "properties": {
                "value": {
                  "type": "string",
                  "minLength": 1
                },
                "evidence": {
                  "minItems": 1
                }
              }
            }
          }
        ]
      }
    }
  }
}
