{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "trinityaccord.record-chain-preflight-response.v1",
  "title": "Record-Chain Preflight Response",
  "description": "Schema for the response from POST /record-chain/preflight. Preflight validates without writing.",
  "oneOf": [
    {
      "$ref": "#/$defs/success"
    },
    {
      "$ref": "#/$defs/failure"
    }
  ],
  "$defs": {
    "diagnostic": {
      "type": "object",
      "required": [
        "code",
        "severity",
        "message"
      ],
      "properties": {
        "code": {
          "type": "string"
        },
        "severity": {
          "type": "string",
          "enum": [
            "error",
            "warning",
            "info"
          ]
        },
        "field": {
          "type": "string"
        },
        "message": {
          "type": "string"
        }
      }
    },
    "success": {
      "type": "object",
      "required": [
        "accepted",
        "preflight",
        "route_detected",
        "record_type",
        "submission_sha256",
        "received_raw_body_sha256",
        "diagnostics",
        "warnings",
        "gateway_runtime",
        "gateway_schema",
        "boundary"
      ],
      "properties": {
        "accepted": {
          "const": true
        },
        "preflight": {
          "type": "boolean",
          "const": true
        },
        "route_detected": {
          "type": "string"
        },
        "record_type": {
          "type": "string"
        },
        "submission_sha256": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        },
        "received_raw_body_sha256": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        },
        "diagnostics": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/diagnostic"
          }
        },
        "warnings": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "gateway_runtime": {
          "$ref": "#/$defs/gateway_runtime"
        },
        "gateway_schema": {
          "$ref": "#/$defs/gateway_schema"
        },
        "boundary": {
          "$ref": "#/$defs/boundary"
        }
      },
      "additionalProperties": true
    },
    "failure": {
      "type": "object",
      "required": [
        "accepted",
        "preflight",
        "received_raw_body_sha256",
        "diagnostics"
      ],
      "properties": {
        "accepted": {
          "const": false
        },
        "preflight": {
          "type": "boolean",
          "const": true
        },
        "route_detected": {
          "type": "string"
        },
        "record_type": {
          "type": "string"
        },
        "submission_sha256": {
          "type": "string"
        },
        "received_raw_body_sha256": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        },
        "diagnostics": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/diagnostic"
          },
          "minItems": 1
        },
        "warnings": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "gateway_runtime": {
          "$ref": "#/$defs/gateway_runtime"
        },
        "gateway_schema": {
          "$ref": "#/$defs/gateway_schema"
        },
        "boundary": {
          "$ref": "#/$defs/boundary"
        }
      },
      "additionalProperties": true
    },
    "gateway_runtime": {
      "type": "object",
      "properties": {
        "service": {
          "type": "string"
        },
        "version": {
          "type": "string"
        },
        "deployed_at": {
          "type": "string"
        },
        "write_mode": {
          "type": "string"
        },
        "max_submission_bytes": {
          "type": "integer"
        },
        "record_draft_max_bytes": {
          "type": "integer"
        },
        "max_text_field_chars": {
          "type": "integer"
        },
        "protection_layer_active": {
          "type": "boolean"
        },
        "protection_entrypoint": {
          "type": "string"
        },
        "global_acceptance_cooldown_seconds": {
          "type": ["object", "null"]
        },
        "base_url": {
          "type": "string"
        }
      }
    },
    "gateway_schema": {
      "type": "object",
      "properties": {
        "submission_schema": {
          "type": "string"
        },
        "preflight_response_schema": {
          "type": "string"
        },
        "submit_response_schema": {
          "type": "string"
        }
      }
    },
    "boundary": {
      "type": "object",
      "required": [
        "preflight_is_not_submission",
        "not_authority",
        "not_attestation",
        "not_amendment"
      ],
      "properties": {
        "preflight_is_not_submission": {
          "const": true
        },
        "not_authority": {
          "const": true
        },
        "not_attestation": {
          "const": true
        },
        "not_amendment": {
          "const": true
        }
      }
    }
  },
  "properties": {
    "accepted": {
      "type": "boolean"
    },
    "preflight": {
      "type": "boolean",
      "const": true
    },
    "route_detected": {
      "type": "string"
    },
    "record_type": {
      "type": "string"
    },
    "submission_sha256": {
      "type": "string",
      "pattern": "^[a-f0-9]{64}$"
    },
    "received_raw_body_sha256": {
      "type": "string",
      "pattern": "^[a-f0-9]{64}$"
    },
    "diagnostics": {
      "type": "array"
    },
    "warnings": {
      "type": "array"
    },
    "gateway_runtime": {
      "type": "object"
    },
    "gateway_schema": {
      "type": "object"
    },
    "boundary": {
      "type": "object"
    }
  },
  "required": [
    "accepted",
    "preflight",
    "route_detected",
    "record_type",
    "submission_sha256",
    "received_raw_body_sha256",
    "diagnostics",
    "warnings",
    "gateway_runtime",
    "gateway_schema",
    "boundary"
  ]
}
