{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.baijimu.com/contracts/bundle-project-manifest/12.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",
  "required": [
    "schemaVersion",
    "definition"
  ],
  "properties": {
    "schemaVersion": {
      "const": "12.0.0"
    },
    "definition": {
      "$ref": "#/$defs/definition"
    }
  },
  "$defs": {
    "definition": {
      "type": "object",
      "properties": {
        "resources": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/resourceSelection"
          },
          "default": []
        },
        "propertyDefinition": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/propertyDefinition"
          },
          "default": []
        },
        "configuration": {
          "$ref": "#/$defs/configuration"
        }
      },
      "not": {
        "anyOf": [
          {
            "required": [
              "dependencies"
            ],
            "properties": {
              "dependencies": {}
            }
          },
          {
            "required": [
              "resourceKey"
            ],
            "properties": {
              "resourceKey": {}
            }
          },
          {
            "required": [
              "resourceLocator"
            ],
            "properties": {
              "resourceLocator": {}
            }
          },
          {
            "required": [
              "schemaVersion"
            ],
            "properties": {
              "schemaVersion": {}
            }
          },
          {
            "required": [
              "typeDefinitionDependencies"
            ],
            "properties": {
              "typeDefinitionDependencies": {}
            }
          },
          {
            "required": [
              "modules"
            ],
            "properties": {
              "modules": {}
            }
          },
          {
            "required": [
              "skills"
            ],
            "properties": {
              "skills": {}
            }
          },
          {
            "required": [
              "agents"
            ],
            "properties": {
              "agents": {}
            }
          },
          {
            "required": [
              "platformApplications"
            ],
            "properties": {
              "platformApplications": {}
            }
          },
          {
            "required": [
              "workflows"
            ],
            "properties": {
              "workflows": {}
            }
          },
          {
            "required": [
              "issueDefinitions"
            ],
            "properties": {
              "issueDefinitions": {}
            }
          },
          {
            "required": [
              "eventTriggers"
            ],
            "properties": {
              "eventTriggers": {}
            }
          },
          {
            "required": [
              "deviceEventTriggers"
            ],
            "properties": {
              "deviceEventTriggers": {}
            }
          },
          {
            "required": [
              "timers"
            ],
            "properties": {
              "timers": {}
            }
          },
          {
            "required": [
              "permissionDefinitionSets"
            ],
            "properties": {
              "permissionDefinitionSets": {}
            }
          },
          {
            "required": [
              "roleTemplateSets"
            ],
            "properties": {
              "roleTemplateSets": {}
            }
          },
          {
            "required": [
              "lifecyclePlugins"
            ],
            "properties": {
              "lifecyclePlugins": {}
            }
          }
        ]
      }
    },
    "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-]+)*)?$"
    },
    "resourceSelection": {
      "type": "object",
      "required": [
        "resourceType",
        "resourceKey",
        "semanticVersion"
      ],
      "properties": {
        "resourceType": {
          "type": "string",
          "pattern": "^[A-Z][A-Z0-9_]{0,31}$"
        },
        "resourceKey": {
          "type": "string",
          "minLength": 1,
          "pattern": "^[^/\\s]+$"
        },
        "semanticVersion": {
          "$ref": "#/$defs/exactSemanticVersion"
        }
      },
      "not": {
        "anyOf": [
          {
            "required": [
              "resourceLocator"
            ],
            "properties": {
              "resourceLocator": {}
            }
          },
          {
            "required": [
              "environmentKey"
            ],
            "properties": {
              "environmentKey": {}
            }
          },
          {
            "required": [
              "bundleId"
            ],
            "properties": {
              "bundleId": {}
            }
          },
          {
            "required": [
              "templateKey"
            ],
            "properties": {
              "templateKey": {}
            }
          }
        ]
      }
    },
    "propertyDefinition": {
      "type": "object",
      "required": [
        "name",
        "type"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "type": {
          "$ref": "#/$defs/propertyType"
        },
        "description": {
          "type": [
            "string",
            "null"
          ]
        },
        "required": {
          "type": [
            "boolean",
            "null"
          ]
        },
        "isSensitive": {
          "type": [
            "boolean",
            "null"
          ]
        },
        "autoBinding": {
          "type": "boolean",
          "default": false
        },
        "isExternalManaged": {
          "type": [
            "boolean",
            "null"
          ]
        },
        "readOnly": {
          "type": [
            "boolean",
            "null"
          ]
        },
        "defaultValue": {}
      },
      "allOf": [
        {
          "if": {
            "required": [
              "autoBinding"
            ],
            "properties": {
              "autoBinding": {
                "const": true
              }
            },
            "type": "object"
          },
          "then": {
            "properties": {
              "type": {
                "properties": {
                  "@type": {
                    "enum": [
                      "Interface",
                      "RefType",
                      "InterfaceMap"
                    ]
                  }
                },
                "type": "object"
              },
              "isExternalManaged": {
                "not": {
                  "const": true
                }
              }
            },
            "type": "object"
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "properties": {
                  "@type": {
                    "not": {
                      "const": "JsonSchema"
                    }
                  }
                },
                "type": "object"
              }
            },
            "type": "object"
          },
          "then": {
            "not": {
              "required": [
                "defaultValue"
              ],
              "type": "object",
              "properties": {
                "defaultValue": {}
              }
            }
          }
        }
      ]
    },
    "propertyType": {
      "type": "object",
      "required": [
        "@type"
      ],
      "properties": {
        "@type": {
          "enum": [
            "JsonSchema",
            "Interface",
            "RefType",
            "InterfaceMap",
            "InterfaceList",
            "MethodDefinition"
          ]
        }
      }
    },
    "configuration": {
      "type": "object",
      "properties": {
        "resourceBindings": {
          "type": "array",
          "default": [],
          "items": {
            "type": "object",
            "required": [
              "resourceType",
              "resourceKey",
              "fields"
            ],
            "properties": {
              "resourceType": {
                "type": "string",
                "minLength": 1
              },
              "resourceKey": {
                "type": "string",
                "minLength": 1
              },
              "fields": {
                "type": "object",
                "additionalProperties": {
                  "$ref": "#/$defs/configurationValueSource"
                }
              }
            }
          }
        }
      }
    },
    "configurationValueSource": {
      "oneOf": [
        {
          "type": "object",
          "required": [
            "@type",
            "value"
          ],
          "properties": {
            "@type": {
              "const": "Data"
            },
            "value": {}
          }
        },
        {
          "type": "object",
          "required": [
            "@type",
            "expression"
          ],
          "properties": {
            "@type": {
              "const": "Expression"
            },
            "expression": {
              "type": "string",
              "minLength": 1
            }
          }
        },
        {
          "type": "object",
          "required": [
            "@type",
            "property"
          ],
          "properties": {
            "@type": {
              "const": "Property"
            },
            "property": {
              "type": "string",
              "minLength": 1
            }
          }
        }
      ]
    }
  },
  "not": {
    "anyOf": [
      {
        "required": [
          "requestedPermissions"
        ],
        "properties": {
          "requestedPermissions": {}
        }
      },
      {
        "required": [
          "providedPermissions"
        ],
        "properties": {
          "providedPermissions": {}
        }
      }
    ]
  }
}
