{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "PDF tool-selection success or error response",
  "oneOf": [
    {
      "$ref": "#/$defs/report"
    },
    {
      "$ref": "#/$defs/error"
    }
  ],
  "$defs": {
    "source": {
      "type": "object",
      "required": [
        "title",
        "url",
        "checked_on"
      ],
      "properties": {
        "title": {
          "type": "string"
        },
        "url": {
          "type": "string",
          "format": "uri"
        },
        "checked_on": {
          "type": "string",
          "format": "date"
        }
      }
    },
    "reason": {
      "type": "object",
      "required": [
        "field",
        "text",
        "source_ids"
      ],
      "properties": {
        "field": {
          "type": "string"
        },
        "text": {
          "type": "string"
        },
        "source_ids": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "basis": {
          "type": "string"
        }
      }
    },
    "constraint": {
      "type": "object",
      "required": [
        "field",
        "value",
        "status",
        "source_ids",
        "note"
      ],
      "properties": {
        "field": {
          "type": "string"
        },
        "value": {
          "type": [
            "string",
            "boolean"
          ]
        },
        "status": {
          "enum": [
            "supported",
            "unsupported",
            "unknown",
            "not_required"
          ]
        },
        "source_ids": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "note": {
          "type": "string"
        },
        "basis": {
          "type": "string"
        }
      }
    },
    "tool": {
      "type": "object",
      "required": [
        "id",
        "name",
        "configuration",
        "status",
        "constraints",
        "reasons",
        "rejections",
        "unknowns",
        "code_license",
        "model_licenses",
        "caveats",
        "preflight",
        "start_here_url",
        "documentation_checked_on",
        "evidence_status"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "configuration": {
          "type": "string"
        },
        "status": {
          "enum": [
            "qualified",
            "needs_review",
            "rejected"
          ]
        },
        "constraints": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/constraint"
          },
          "minItems": 7,
          "maxItems": 7
        },
        "reasons": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/reason"
          }
        },
        "rejections": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/reason"
          }
        },
        "unknowns": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/reason"
          }
        },
        "code_license": {
          "type": [
            "object",
            "null"
          ]
        },
        "model_licenses": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "model",
              "license",
              "source_ids"
            ],
            "properties": {
              "model": {
                "type": "string"
              },
              "license": {
                "type": "string"
              },
              "source_ids": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            }
          }
        },
        "caveats": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "preflight": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "start_here_url": {
          "type": "string",
          "format": "uri"
        },
        "documentation_checked_on": {
          "type": "string",
          "format": "date"
        },
        "evidence_status": {
          "const": "documentation_reviewed_not_tested"
        },
        "rank": {
          "type": "integer",
          "minimum": 1,
          "maximum": 5
        },
        "ranking": {
          "type": "object",
          "required": [
            "method",
            "score",
            "reasons"
          ],
          "properties": {
            "method": {
              "type": "string"
            },
            "score": {
              "type": "number"
            },
            "reasons": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          }
        },
        "cost": {
          "type": [
            "object",
            "null"
          ]
        }
      }
    },
    "quote": {
      "type": "object",
      "required": [
        "currency",
        "amount_minor",
        "payment_required",
        "payments_enabled",
        "mode",
        "proposed_future_amount_minor",
        "commitment"
      ],
      "properties": {
        "currency": {
          "const": "USD"
        },
        "amount_minor": {
          "const": 0
        },
        "payment_required": {
          "const": false
        },
        "payments_enabled": {
          "const": false
        },
        "mode": {
          "const": "free_prototype"
        },
        "proposed_future_amount_minor": {
          "const": 100
        },
        "commitment": {
          "const": "none"
        }
      }
    },
    "report": {
      "type": "object",
      "required": [
        "schema_version",
        "service_version",
        "report_id",
        "dataset_version",
        "documentation_checked_on",
        "status",
        "summary",
        "input",
        "price_quote",
        "evidence_status",
        "counts",
        "candidates",
        "needs_review",
        "rejected",
        "sources",
        "ranking_policy",
        "limitations",
        "privacy"
      ],
      "properties": {
        "schema_version": {
          "const": "1.0.0"
        },
        "service_version": {
          "type": "string"
        },
        "report_id": {
          "type": "string",
          "pattern": "^pdfmatch_[0-9a-f]{64}$"
        },
        "dataset_version": {
          "type": "string"
        },
        "documentation_checked_on": {
          "type": "string",
          "format": "date"
        },
        "status": {
          "enum": [
            "matched",
            "abstained"
          ]
        },
        "summary": {
          "type": "string"
        },
        "input": {
          "type": "object"
        },
        "price_quote": {
          "$ref": "#/$defs/quote"
        },
        "evidence_status": {
          "const": "documentation_reviewed_not_tested"
        },
        "counts": {
          "type": "object",
          "required": [
            "reviewed",
            "qualified",
            "returned",
            "needs_review",
            "rejected"
          ],
          "additionalProperties": {
            "type": "integer",
            "minimum": 0
          }
        },
        "candidates": {
          "type": "array",
          "items": {
            "allOf": [
              {
                "$ref": "#/$defs/tool"
              },
              {
                "type": "object",
                "required": [
                  "rank",
                  "ranking"
                ],
                "properties": {
                  "status": {
                    "const": "qualified"
                  },
                  "rejections": {
                    "maxItems": 0
                  },
                  "unknowns": {
                    "maxItems": 0
                  }
                }
              }
            ]
          },
          "maxItems": 5
        },
        "needs_review": {
          "type": "array",
          "items": {
            "allOf": [
              {
                "$ref": "#/$defs/tool"
              },
              {
                "properties": {
                  "status": {
                    "const": "needs_review"
                  },
                  "unknowns": {
                    "minItems": 1
                  },
                  "rejections": {
                    "maxItems": 0
                  }
                }
              }
            ]
          }
        },
        "rejected": {
          "type": "array",
          "items": {
            "allOf": [
              {
                "$ref": "#/$defs/tool"
              },
              {
                "properties": {
                  "status": {
                    "const": "rejected"
                  },
                  "rejections": {
                    "minItems": 1
                  }
                }
              }
            ]
          }
        },
        "sources": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/source"
          }
        },
        "ranking_policy": {
          "type": "object"
        },
        "limitations": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "privacy": {
          "type": "object",
          "required": [
            "document_uploads",
            "model_calls",
            "application_storage",
            "analytics"
          ],
          "properties": {
            "document_uploads": {
              "const": false
            },
            "model_calls": {
              "const": false
            },
            "application_storage": {
              "const": false
            },
            "analytics": {
              "const": false
            }
          }
        }
      }
    },
    "error": {
      "type": "object",
      "required": [
        "error",
        "schema_version",
        "payment_taken"
      ],
      "properties": {
        "schema_version": {
          "const": "1.0.0"
        },
        "payment_taken": {
          "const": false
        },
        "error": {
          "type": "object",
          "required": [
            "code",
            "message",
            "details"
          ],
          "properties": {
            "code": {
              "type": "string"
            },
            "message": {
              "type": "string"
            },
            "details": {
              "type": "array",
              "items": {
                "type": "object"
              }
            }
          }
        }
      }
    }
  }
}
