{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.baijimu.com/contracts/bundle-project-manifest/2.0.0/schema.json",
  "title": "Baijimu Bundle Project Manifest",
  "description": "Git-owned Bundle source document stored at the project-root path baijimu.bundle.json.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "definition"
  ],
  "properties": {
    "schemaVersion": {
      "const": "2.0.0"
    },
    "definition": {
      "$ref": "#/$defs/definition"
    },
    "requestedPermissions": true,
    "providedPermissions": true
  },
  "$defs": {
    "definition": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "typeDefinitionDependencies": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/typeDefinitionDependency"
          },
          "default": []
        },
        "dependencies": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/bundleDependency"
          },
          "default": []
        },
        "modules": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/modulesSelection"
          },
          "default": []
        },
        "skills": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/skillsSelection"
          },
          "default": []
        },
        "agents": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/agentsSelection"
          },
          "default": []
        },
        "platformApplications": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/platformApplicationsSelection"
          },
          "default": []
        },
        "workflows": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/workflowsSelection"
          },
          "default": []
        },
        "issueDefinitions": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/issueDefinitionsSelection"
          },
          "default": []
        },
        "eventTriggers": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/eventTriggersSelection"
          },
          "default": []
        },
        "timers": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/timersSelection"
          },
          "default": []
        },
        "permissionDefinitionSets": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/permissionDefinitionSetsSelection"
          },
          "default": []
        },
        "roleTemplateSets": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/roleTemplateSetsSelection"
          },
          "default": []
        },
        "lifecyclePlugins": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/pluginBinding"
          },
          "default": []
        }
      }
    },
    "typeDefinitionDependency": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "locator",
        "semanticVersion"
      ],
      "properties": {
        "locator": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "environmentKey",
            "typeDefinitionId"
          ],
          "properties": {
            "environmentKey": {
              "type": "string",
              "pattern": "^[A-Za-z0-9][A-Za-z0-9_-]{1,63}$"
            },
            "typeDefinitionId": {
              "type": "string",
              "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
            }
          }
        },
        "semanticVersion": {
          "$ref": "#/$defs/nonBlank"
        }
      }
    },
    "bundleDependency": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "bundleId",
        "semanticVersion"
      ],
      "properties": {
        "bundleId": {
          "$ref": "#/$defs/nonBlank"
        },
        "semanticVersion": {
          "$ref": "#/$defs/nonBlank"
        }
      }
    },
    "nonBlank": {
      "type": "string",
      "minLength": 1,
      "pattern": "\\S"
    },
    "exactSemanticVersion": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(?:-(?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*)(?:\\.(?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*))*)?(?:\\+[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?$"
    },
    "domainIdentity": {
      "type": "string",
      "minLength": 1,
      "maxLength": 200,
      "pattern": "^[^\\s/@]+$"
    },
    "modulesSelection": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "moduleId",
        "semanticVersion"
      ],
      "properties": {
        "moduleId": {
          "$ref": "#/$defs/domainIdentity"
        },
        "semanticVersion": {
          "$ref": "#/$defs/exactSemanticVersion"
        }
      }
    },
    "skillsSelection": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "skillKey",
        "semanticVersion"
      ],
      "properties": {
        "skillKey": {
          "$ref": "#/$defs/domainIdentity"
        },
        "semanticVersion": {
          "$ref": "#/$defs/exactSemanticVersion"
        }
      }
    },
    "agentsSelection": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "agentKey",
        "semanticVersion"
      ],
      "properties": {
        "agentKey": {
          "$ref": "#/$defs/domainIdentity"
        },
        "semanticVersion": {
          "$ref": "#/$defs/exactSemanticVersion"
        }
      }
    },
    "platformApplicationsSelection": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "platformAppId",
        "semanticVersion"
      ],
      "properties": {
        "platformAppId": {
          "$ref": "#/$defs/domainIdentity"
        },
        "semanticVersion": {
          "$ref": "#/$defs/exactSemanticVersion"
        }
      }
    },
    "workflowsSelection": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "workflowKey",
        "semanticVersion"
      ],
      "properties": {
        "workflowKey": {
          "$ref": "#/$defs/domainIdentity"
        },
        "semanticVersion": {
          "$ref": "#/$defs/exactSemanticVersion"
        }
      }
    },
    "issueDefinitionsSelection": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "issueDefinitionId",
        "semanticVersion"
      ],
      "properties": {
        "issueDefinitionId": {
          "$ref": "#/$defs/domainIdentity"
        },
        "semanticVersion": {
          "$ref": "#/$defs/exactSemanticVersion"
        }
      }
    },
    "eventTriggersSelection": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "triggerKey",
        "semanticVersion"
      ],
      "properties": {
        "triggerKey": {
          "$ref": "#/$defs/domainIdentity"
        },
        "semanticVersion": {
          "$ref": "#/$defs/exactSemanticVersion"
        }
      }
    },
    "timersSelection": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "timerKey",
        "semanticVersion"
      ],
      "properties": {
        "timerKey": {
          "$ref": "#/$defs/domainIdentity"
        },
        "semanticVersion": {
          "$ref": "#/$defs/exactSemanticVersion"
        }
      }
    },
    "permissionDefinitionSetsSelection": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "permissionDefinitionSetId",
        "semanticVersion"
      ],
      "properties": {
        "permissionDefinitionSetId": {
          "$ref": "#/$defs/domainIdentity"
        },
        "semanticVersion": {
          "$ref": "#/$defs/exactSemanticVersion"
        }
      }
    },
    "roleTemplateSetsSelection": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "roleTemplateSetId",
        "semanticVersion"
      ],
      "properties": {
        "roleTemplateSetId": {
          "$ref": "#/$defs/domainIdentity"
        },
        "semanticVersion": {
          "$ref": "#/$defs/exactSemanticVersion"
        }
      }
    },
    "domainObject": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "moduleId"
          ],
          "properties": {
            "type": {
              "const": "MODULE"
            },
            "moduleId": {
              "$ref": "#/$defs/domainIdentity"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "skillKey"
          ],
          "properties": {
            "type": {
              "const": "SKILL"
            },
            "skillKey": {
              "$ref": "#/$defs/domainIdentity"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "agentKey"
          ],
          "properties": {
            "type": {
              "const": "AGENT"
            },
            "agentKey": {
              "$ref": "#/$defs/domainIdentity"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "platformAppId"
          ],
          "properties": {
            "type": {
              "const": "PLATFORM_APPLICATION"
            },
            "platformAppId": {
              "$ref": "#/$defs/domainIdentity"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "workflowKey"
          ],
          "properties": {
            "type": {
              "const": "WORKFLOW"
            },
            "workflowKey": {
              "$ref": "#/$defs/domainIdentity"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "issueDefinitionId"
          ],
          "properties": {
            "type": {
              "const": "ISSUE_DEFINITION"
            },
            "issueDefinitionId": {
              "$ref": "#/$defs/domainIdentity"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "triggerKey"
          ],
          "properties": {
            "type": {
              "const": "EVENT_TRIGGER"
            },
            "triggerKey": {
              "$ref": "#/$defs/domainIdentity"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "timerKey"
          ],
          "properties": {
            "type": {
              "const": "TIMER"
            },
            "timerKey": {
              "$ref": "#/$defs/domainIdentity"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "permissionDefinitionSetId"
          ],
          "properties": {
            "type": {
              "const": "PERMISSION_DEFINITION_SET"
            },
            "permissionDefinitionSetId": {
              "$ref": "#/$defs/domainIdentity"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "type",
            "roleTemplateSetId"
          ],
          "properties": {
            "type": {
              "const": "ROLE_TEMPLATE_SET"
            },
            "roleTemplateSetId": {
              "$ref": "#/$defs/domainIdentity"
            }
          }
        }
      ]
    },
    "propertyNames": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 191,
        "pattern": "^[^\\s/]+$"
      }
    },
    "pluginGrant": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "object",
        "readProperties",
        "writeProperties",
        "credentialProperties"
      ],
      "properties": {
        "object": {
          "$ref": "#/$defs/domainObject"
        },
        "readProperties": {
          "$ref": "#/$defs/propertyNames"
        },
        "writeProperties": {
          "$ref": "#/$defs/propertyNames"
        },
        "credentialProperties": {
          "$ref": "#/$defs/propertyNames"
        },
        "instanceRefProperty": {
          "type": "string",
          "minLength": 1,
          "maxLength": 191,
          "pattern": "^[^\\s/]+$"
        }
      }
    },
    "pluginBinding": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "pluginId",
        "protocolVersion",
        "order",
        "failurePolicy",
        "configuration",
        "resources"
      ],
      "properties": {
        "pluginId": {
          "type": "string",
          "minLength": 1,
          "maxLength": 191,
          "pattern": "^[^\\s/]+$"
        },
        "protocolVersion": {
          "$ref": "#/$defs/exactSemanticVersion"
        },
        "order": {
          "type": "integer",
          "minimum": -2147483648,
          "maximum": 2147483647
        },
        "failurePolicy": {
          "enum": [
            "FAIL_FAST",
            "WARN_CONTINUE"
          ]
        },
        "configuration": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "schemaVersion",
            "values"
          ],
          "properties": {
            "schemaVersion": {
              "$ref": "#/$defs/exactSemanticVersion"
            },
            "values": {
              "type": "object",
              "additionalProperties": true
            }
          }
        },
        "resources": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/pluginGrant"
          },
          "default": []
        }
      }
    }
  }
}
