{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/distribution/bundle-download-manifest.v1.schema.json",
  "title": "RootBlocks Bundle Download Manifest v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "downloadId",
    "issuedAt",
    "expiresAt",
    "bundle",
    "artifact",
    "signature",
    "localVerification"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.bundle-download-manifest/v1"
    },
    "downloadId": {
      "type": "string",
      "pattern": "^download-[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "issuedAt": {
      "type": "string",
      "format": "date-time"
    },
    "expiresAt": {
      "type": "string",
      "format": "date-time"
    },
    "bundle": {
      "$ref": "#/$defs/bundleRef"
    },
    "artifact": {
      "$ref": "#/$defs/artifact"
    },
    "signature": {
      "$ref": "#/$defs/signature"
    },
    "localVerification": {
      "$ref": "#/$defs/localVerification"
    }
  },
  "$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}$"
    },
    "uri": {
      "type": "string",
      "minLength": 1,
      "maxLength": 500
    },
    "bundleRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "bundleKind", "version", "manifestHash"],
      "properties": {
        "bundleId": {
          "$ref": "#/$defs/id"
        },
        "bundleKind": {
          "enum": [
            "rootblocks-global-standard",
            "organization-component-extension",
            "project-component-extension",
            "personal-component-extension"
          ]
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "manifestHash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "artifact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["uri", "mediaType", "sizeBytes", "hashAlgorithm", "bundleHash"],
      "properties": {
        "uri": {
          "$ref": "#/$defs/uri"
        },
        "mediaType": {
          "enum": ["application/vnd.rootblocks.standard-bundle+zip", "application/gzip", "application/zip"]
        },
        "sizeBytes": {
          "type": "integer",
          "minimum": 1
        },
        "hashAlgorithm": {
          "enum": ["sha256", "sha384", "sha512"]
        },
        "bundleHash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "signature": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode", "algorithm", "keyId", "signatureRef"],
      "properties": {
        "mode": {
          "const": "signed"
        },
        "algorithm": {
          "enum": ["ed25519", "rsa-pss-sha256"]
        },
        "keyId": {
          "type": "string",
          "minLength": 1
        },
        "signatureRef": {
          "$ref": "#/$defs/uri"
        }
      }
    },
    "localVerification": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required", "steps"],
      "properties": {
        "required": {
          "type": "boolean",
          "const": true
        },
        "steps": {
          "type": "array",
          "minItems": 3,
          "items": {
            "enum": ["download-temp", "verify-manifest-hash", "verify-bundle-hash", "verify-signature", "validate-manifest", "atomic-cache-replace"]
          },
          "uniqueItems": true,
          "allOf": [
            { "contains": { "const": "verify-manifest-hash" } },
            { "contains": { "const": "verify-bundle-hash" } },
            { "contains": { "const": "verify-signature" } }
          ]
        }
      }
    }
  }
}
