{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/distribution/bundle-distribution-index.v1.schema.json",
  "title": "RootBlocks Bundle Distribution Index v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "distributionId",
    "generatedAt",
    "publisher",
    "bootstrap",
    "endpoints",
    "bundles"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.bundle-distribution-index/v1"
    },
    "distributionId": {
      "$ref": "#/$defs/id"
    },
    "generatedAt": {
      "type": "string",
      "format": "date-time"
    },
    "publisher": {
      "$ref": "#/$defs/publisher"
    },
    "bootstrap": {
      "$ref": "#/$defs/bootstrap"
    },
    "endpoints": {
      "$ref": "#/$defs/endpoints"
    },
    "bundles": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/bundleEntry"
      }
    }
  },
  "$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
    },
    "bundleKind": {
      "enum": [
        "rootblocks-global-standard",
        "organization-component-extension",
        "project-component-extension",
        "personal-component-extension"
      ]
    },
    "publisher": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "trustDomain", "keySetRef"],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "trustDomain": {
          "$ref": "#/$defs/id"
        },
        "keySetRef": {
          "$ref": "#/$defs/uri"
        }
      }
    },
    "bootstrap": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode", "sourceRef", "localVerificationRequired"],
      "properties": {
        "mode": {
          "enum": ["service", "git-bootstrap", "service-with-git-fallback"]
        },
        "sourceRef": {
          "$ref": "#/$defs/uri"
        },
        "indexPath": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_./@-]+$"
        },
        "localVerificationRequired": {
          "type": "boolean",
          "const": true
        }
      }
    },
    "endpoints": {
      "type": "object",
      "additionalProperties": false,
      "required": ["listBundles", "listVersions", "latestCompatible", "download"],
      "properties": {
        "listBundles": {
          "const": "GET /standards/bundles"
        },
        "listVersions": {
          "const": "GET /standards/bundles/{bundleId}/versions"
        },
        "versionMetadata": {
          "const": "GET /standards/bundles/{bundleId}/versions/{version}"
        },
        "latestCompatible": {
          "const": "POST /standards/bundles/{bundleId}/latest-compatible"
        },
        "download": {
          "const": "GET /standards/bundles/{bundleId}/versions/{version}/download"
        },
        "componentImport": {
          "const": "POST /standards/components/import"
        },
        "componentSync": {
          "const": "POST /standards/components/sync"
        }
      }
    },
    "bundleEntry": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "bundleKind", "visibility", "versions"],
      "properties": {
        "bundleId": {
          "$ref": "#/$defs/id"
        },
        "bundleKind": {
          "$ref": "#/$defs/bundleKind"
        },
        "displayName": {
          "type": "string",
          "minLength": 1
        },
        "visibility": {
          "enum": ["public", "organization-private", "project-local", "personal-private"]
        },
        "owner": {
          "$ref": "#/$defs/owner"
        },
        "versions": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/bundleVersion"
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "bundleKind": {
                "enum": [
                  "organization-component-extension",
                  "project-component-extension",
                  "personal-component-extension"
                ]
              }
            },
            "required": ["bundleKind"]
          },
          "then": {
            "required": ["owner"]
          }
        }
      ]
    },
    "owner": {
      "type": "object",
      "additionalProperties": false,
      "required": ["scope", "ownerId"],
      "properties": {
        "scope": {
          "enum": ["organization", "project", "personal"]
        },
        "ownerId": {
          "type": "string",
          "pattern": "^[a-zA-Z0-9][a-zA-Z0-9_.:-]{1,127}$"
        }
      }
    },
    "bundleVersion": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "version",
        "releaseStatus",
        "publishedAt",
        "manifestHash",
        "bundleHash",
        "signature",
        "compatibility",
        "download",
        "criticalUpdate"
      ],
      "properties": {
        "version": {
          "$ref": "#/$defs/semver"
        },
        "releaseStatus": {
          "enum": ["draft", "pre-release", "stable", "deprecated", "revoked"]
        },
        "publishedAt": {
          "type": "string",
          "format": "date-time"
        },
        "manifestHash": {
          "$ref": "#/$defs/hash"
        },
        "bundleHash": {
          "$ref": "#/$defs/hash"
        },
        "signature": {
          "$ref": "#/$defs/signature"
        },
        "compatibility": {
          "$ref": "#/$defs/compatibilitySummary"
        },
        "download": {
          "$ref": "#/$defs/downloadSummary"
        },
        "criticalUpdate": {
          "$ref": "#/$defs/criticalUpdate"
        },
        "deprecation": {
          "$ref": "#/$defs/deprecation"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "releaseStatus": {
                "enum": ["stable", "deprecated"]
              }
            },
            "required": ["releaseStatus"]
          },
          "then": {
            "properties": {
              "signature": {
                "properties": {
                  "mode": {
                    "const": "signed"
                  }
                }
              },
              "download": {
                "properties": {
                  "available": {
                    "const": true
                  }
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "releaseStatus": {
                "const": "deprecated"
              }
            },
            "required": ["releaseStatus"]
          },
          "then": {
            "required": ["deprecation"]
          }
        }
      ]
    },
    "signature": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode", "algorithm", "keyId"],
      "properties": {
        "mode": {
          "enum": ["unsigned-dev-only", "signed"]
        },
        "algorithm": {
          "enum": ["none", "ed25519", "rsa-pss-sha256"]
        },
        "keyId": {
          "type": "string",
          "minLength": 1
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "mode": {
                "const": "signed"
              }
            },
            "required": ["mode"]
          },
          "then": {
            "properties": {
              "algorithm": {
                "enum": ["ed25519", "rsa-pss-sha256"]
              }
            }
          }
        }
      ]
    },
    "compatibilitySummary": {
      "type": "object",
      "additionalProperties": false,
      "required": ["matrixRef", "supportedConsumerNames", "schemaFamilies"],
      "properties": {
        "matrixRef": {
          "$ref": "#/$defs/uri"
        },
        "supportedConsumerNames": {
          "type": "array",
          "minItems": 1,
          "items": {
            "enum": ["rootblocks-console", "darkfactory", "builderx", "rbstd", "agent", "custom"]
          },
          "uniqueItems": true
        },
        "schemaFamilies": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string",
            "minLength": 1
          },
          "uniqueItems": true
        }
      }
    },
    "downloadSummary": {
      "type": "object",
      "additionalProperties": false,
      "required": ["available", "manifestPath", "downloadManifestPath"],
      "properties": {
        "available": {
          "type": "boolean"
        },
        "manifestPath": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_./@-]+$"
        },
        "downloadManifestPath": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_./@-]+$"
        },
        "sizeBytes": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "criticalUpdate": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required", "reasonCodes"],
      "properties": {
        "required": {
          "type": "boolean"
        },
        "reasonCodes": {
          "type": "array",
          "items": {
            "enum": ["SECURITY", "COMPATIBILITY", "SIGNATURE_ROTATION", "REVOKED_BUNDLE", "BUGFIX"]
          },
          "uniqueItems": true
        },
        "message": {
          "type": "string",
          "minLength": 1
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "required": {
                "const": true
              }
            },
            "required": ["required"]
          },
          "then": {
            "properties": {
              "reasonCodes": {
                "minItems": 1
              }
            },
            "required": ["message"]
          }
        }
      ]
    },
    "deprecation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["announcedAt", "supportedUntil", "migration"],
      "properties": {
        "announcedAt": {
          "type": "string",
          "format": "date-time"
        },
        "supportedUntil": {
          "type": "string",
          "format": "date-time"
        },
        "migration": {
          "type": "string",
          "minLength": 1
        }
      }
    }
  }
}
