{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/bundle/standard-bundle.manifest.v1.schema.json",
  "title": "RootBlocks Standard Bundle Manifest v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "bundleId",
    "bundleKind",
    "version",
    "producer",
    "createdAt",
    "compatibility",
    "assets",
    "hashes",
    "signature"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.standard.bundle/v1"
    },
    "bundleId": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "bundleKind": {
      "enum": [
        "rootblocks-global-standard",
        "organization-component-extension",
        "project-component-extension",
        "personal-component-extension"
      ]
    },
    "version": {
      "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.-]+)?$"
    },
    "producer": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "product"],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "product": {
          "type": "string",
          "minLength": 1
        },
        "commit": {
          "type": "string",
          "minLength": 7
        }
      }
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "compatibility": {
      "$ref": "../compatibility/compatibility-matrix.v1.schema.json"
    },
    "assets": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/asset"
      }
    },
    "extends": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/bundleRef"
      }
    },
    "extensionOwner": {
      "$ref": "#/$defs/extensionOwner"
    },
    "externalImportPolicy": {
      "$ref": "#/$defs/externalImportPolicy"
    },
    "hashes": {
      "type": "object",
      "additionalProperties": false,
      "required": ["algorithm", "manifestHash"],
      "properties": {
        "algorithm": {
          "enum": ["sha256", "sha384", "sha512"]
        },
        "manifestHash": {
          "type": "string",
          "pattern": "^[a-f0-9]{64,128}$"
        }
      }
    },
    "signature": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode", "algorithm", "keyId", "value"],
      "properties": {
        "mode": {
          "enum": ["unsigned-dev-only", "signed"]
        },
        "algorithm": {
          "enum": ["none", "ed25519", "rsa-pss-sha256"]
        },
        "keyId": {
          "type": "string",
          "minLength": 1
        },
        "value": {
          "type": "string"
        }
      }
    },
    "policies": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "refreshPolicy": {
          "enum": ["allow-stale", "warn-stale", "block-critical-stale"]
        },
        "enforcementMode": {
          "enum": ["advisory", "enforced"]
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "bundleKind": {
            "enum": [
              "organization-component-extension",
              "project-component-extension",
              "personal-component-extension"
            ]
          }
        },
        "required": ["bundleKind"]
      },
      "then": {
        "required": ["extends", "extensionOwner", "externalImportPolicy"],
        "properties": {
          "assets": {
            "items": {
              "properties": {
                "kind": {
                  "const": "component"
                }
              }
            }
          },
          "externalImportPolicy": {
            "properties": {
              "mode": {
                "const": "components-only"
              },
              "allowedAssetKinds": {
                "items": {
                  "const": "component"
                }
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "bundleKind": {
            "const": "organization-component-extension"
          }
        },
        "required": ["bundleKind"]
      },
      "then": {
        "properties": {
          "extensionOwner": {
            "properties": {
              "scope": {
                "const": "organization"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "bundleKind": {
            "const": "project-component-extension"
          }
        },
        "required": ["bundleKind"]
      },
      "then": {
        "properties": {
          "extensionOwner": {
            "properties": {
              "scope": {
                "const": "project"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "bundleKind": {
            "const": "personal-component-extension"
          }
        },
        "required": ["bundleKind"]
      },
      "then": {
        "properties": {
          "extensionOwner": {
            "properties": {
              "scope": {
                "const": "personal"
              }
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "extensionOwner": {
      "type": "object",
      "additionalProperties": false,
      "required": ["scope", "ownerId", "visibility", "approvalState"],
      "properties": {
        "scope": {
          "enum": ["organization", "project", "personal"]
        },
        "ownerId": {
          "type": "string",
          "pattern": "^[a-zA-Z0-9][a-zA-Z0-9_.:-]{1,127}$"
        },
        "visibility": {
          "enum": ["private", "shared", "public-candidate", "public"]
        },
        "approvalState": {
          "enum": ["draft", "approved", "published", "deprecated", "revoked"]
        }
      }
    },
    "bundleRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "version", "hash"],
      "properties": {
        "bundleId": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
        },
        "version": {
          "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}$"
        }
      }
    },
    "externalImportPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode", "allowedAssetKinds", "blockedAssetKinds"],
      "properties": {
        "mode": {
          "enum": ["components-only", "rootblocks-internal-only"]
        },
        "allowedAssetKinds": {
          "type": "array",
          "minItems": 1,
          "items": {
            "enum": ["component"]
          },
          "uniqueItems": true
        },
        "blockedAssetKinds": {
          "type": "array",
          "items": {
            "enum": [
              "schema",
              "rule",
              "template",
              "golden",
              "policy",
              "documentation",
              "semantic-engine",
              "architecture-profile",
              "recipe",
              "compatibility",
              "bundle",
              "registry"
            ]
          },
          "uniqueItems": true
        }
      }
    },
    "asset": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "kind", "path", "version", "hash"],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
        },
        "kind": {
          "enum": [
            "schema",
            "rule",
            "component",
            "template",
            "golden",
            "policy",
            "documentation",
            "architecture-profile",
            "recipe"
          ]
        },
        "path": {
          "type": "string",
          "pattern": "^(schemas|rules|components|templates|golden|docs|profiles|recipes)/.+$"
        },
        "version": {
          "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}$"
        },
        "required": {
          "type": "boolean",
          "default": true
        }
      }
    }
  }
}
