{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/distribution/latest-compatible-response.v1.schema.json",
  "title": "RootBlocks Latest Compatible Response v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "requestId",
    "respondedAt",
    "bundleId",
    "result",
    "reasonCodes",
    "requiredActions"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.latest-compatible-response/v1"
    },
    "requestId": {
      "type": "string",
      "pattern": "^dist-request-[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "respondedAt": {
      "type": "string",
      "format": "date-time"
    },
    "bundleId": {
      "$ref": "#/$defs/id"
    },
    "result": {
      "enum": ["found", "not-found", "not-compatible", "unavailable", "stale-critical", "blocked"]
    },
    "selectedBundle": {
      "$ref": "#/$defs/selectedBundle"
    },
    "compatibilityDecision": {
      "$ref": "#/$defs/compatibilityDecision"
    },
    "criticalUpdate": {
      "$ref": "#/$defs/criticalUpdate"
    },
    "reasonCodes": {
      "type": "array",
      "minItems": 1,
      "items": {
        "enum": [
          "LATEST_COMPATIBLE_FOUND",
          "NO_COMPATIBLE_VERSION",
          "BUNDLE_NOT_FOUND",
          "DISTRIBUTION_UNAVAILABLE",
          "CRITICAL_UPDATE_AVAILABLE",
          "STALE_CRITICAL_BLOCKED",
          "UNSIGNED_RELEASE_BLOCKED",
          "LOCAL_VERIFICATION_REQUIRED"
        ]
      },
      "uniqueItems": true
    },
    "requiredActions": {
      "type": "array",
      "minItems": 1,
      "items": {
        "enum": [
          "download",
          "verify-signature",
          "verify-hashes",
          "refresh-cache",
          "use-current",
          "select-older-compatible",
          "upgrade-consumer",
          "retry-later",
          "block-operation"
        ]
      },
      "uniqueItems": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "result": {
            "const": "found"
          }
        },
        "required": ["result"]
      },
      "then": {
        "required": ["selectedBundle", "compatibilityDecision"],
        "properties": {
          "requiredActions": {
            "allOf": [
              { "contains": { "const": "download" } },
              { "contains": { "const": "verify-signature" } },
              { "contains": { "const": "verify-hashes" } }
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "result": {
            "enum": ["not-compatible", "stale-critical", "blocked"]
          }
        },
        "required": ["result"]
      },
      "then": {
        "properties": {
          "requiredActions": {
            "contains": {
              "const": "block-operation"
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "semver": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[A-Za-z0-9.-]+)?(\\+[A-Za-z0-9.-]+)?$"
    },
    "hash": {
      "type": "string",
      "pattern": "^[a-f0-9]{64,128}$"
    },
    "path": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_./@-]+$"
    },
    "selectedBundle": {
      "type": "object",
      "additionalProperties": false,
      "required": ["version", "releaseStatus", "manifestHash", "bundleHash", "downloadManifestPath"],
      "properties": {
        "version": {
          "$ref": "#/$defs/semver"
        },
        "releaseStatus": {
          "enum": ["stable", "deprecated"]
        },
        "manifestHash": {
          "$ref": "#/$defs/hash"
        },
        "bundleHash": {
          "$ref": "#/$defs/hash"
        },
        "downloadManifestPath": {
          "$ref": "#/$defs/path"
        }
      }
    },
    "compatibilityDecision": {
      "type": "object",
      "additionalProperties": false,
      "required": ["result", "decisionPath"],
      "properties": {
        "result": {
          "enum": ["allow", "warn", "block"]
        },
        "decisionPath": {
          "$ref": "#/$defs/path"
        }
      }
    },
    "criticalUpdate": {
      "type": "object",
      "additionalProperties": false,
      "required": ["available", "required", "reasonCodes"],
      "properties": {
        "available": {
          "type": "boolean"
        },
        "required": {
          "type": "boolean"
        },
        "reasonCodes": {
          "type": "array",
          "items": {
            "enum": ["SECURITY", "COMPATIBILITY", "SIGNATURE_ROTATION", "REVOKED_BUNDLE", "BUGFIX"]
          },
          "uniqueItems": true
        }
      }
    }
  }
}
