{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/architecture/architecture-profile.v1.schema.json",
  "title": "RootBlocks Architecture Profile v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "profileId",
    "version",
    "owner",
    "status",
    "projectKind",
    "displayName",
    "description",
    "compatibility",
    "renderer",
    "layout",
    "dependencyRules",
    "sourceZones",
    "sourceMapPolicy",
    "regressionPolicy",
    "evidencePolicy"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.architecture-profile/v1"
    },
    "profileId": {
      "$ref": "#/$defs/id"
    },
    "version": {
      "$ref": "#/$defs/semver"
    },
    "owner": {
      "$ref": "#/$defs/owner"
    },
    "status": {
      "enum": ["experimental", "stable", "deprecated"]
    },
    "projectKind": {
      "enum": ["application", "component-pack", "library", "worker", "service", "custom"]
    },
    "displayName": {
      "type": "string",
      "minLength": 1,
      "maxLength": 120
    },
    "description": {
      "type": "string",
      "minLength": 1,
      "maxLength": 1000
    },
    "compatibility": {
      "$ref": "#/$defs/compatibility"
    },
    "renderer": {
      "$ref": "#/$defs/renderer"
    },
    "layout": {
      "$ref": "#/$defs/layout"
    },
    "dependencyRules": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/dependencyRule"
      }
    },
    "sourceZones": {
      "$ref": "#/$defs/sourceZones"
    },
    "sourceMapPolicy": {
      "$ref": "#/$defs/sourceMapPolicy"
    },
    "regressionPolicy": {
      "$ref": "#/$defs/regressionPolicy"
    },
    "evidencePolicy": {
      "$ref": "#/$defs/evidencePolicy"
    },
    "extensionPolicy": {
      "$ref": "#/$defs/extensionPolicy"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "profileId": {
            "const": "ddd-clean"
          }
        },
        "required": ["profileId"]
      },
      "then": {
        "properties": {
          "projectKind": {
            "const": "application"
          },
          "renderer": {
            "properties": {
              "rendererId": {
                "const": "builderx-structured-project"
              }
            }
          },
          "layout": {
            "properties": {
              "projects": {
                "allOf": [
                  {
                    "contains": {
                      "properties": {
                        "layer": {
                          "const": "Domain"
                        }
                      },
                      "required": ["layer"]
                    }
                  },
                  {
                    "contains": {
                      "properties": {
                        "layer": {
                          "const": "Application"
                        }
                      },
                      "required": ["layer"]
                    }
                  },
                  {
                    "contains": {
                      "properties": {
                        "layer": {
                          "const": "Infrastructure"
                        }
                      },
                      "required": ["layer"]
                    }
                  },
                  {
                    "contains": {
                      "properties": {
                        "layer": {
                          "const": "Workflows"
                        }
                      },
                      "required": ["layer"]
                    }
                  },
                  {
                    "contains": {
                      "properties": {
                        "layer": {
                          "const": "Host"
                        }
                      },
                      "required": ["layer"]
                    }
                  }
                ]
              }
            }
          },
          "dependencyRules": {
            "not": {
              "contains": {
                "properties": {
                  "fromLayer": {
                    "const": "Domain"
                  },
                  "allowedToLayers": {
                    "contains": {
                      "enum": ["Application", "Infrastructure", "Workflows", "Host", "BuilderX"]
                    }
                  }
                },
                "required": ["fromLayer", "allowedToLayers"]
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "profileId": {
            "const": "component-pack"
          }
        },
        "required": ["profileId"]
      },
      "then": {
        "properties": {
          "projectKind": {
            "const": "component-pack"
          },
          "renderer": {
            "properties": {
              "rendererId": {
                "const": "builderx-component-pack"
              }
            }
          },
          "layout": {
            "properties": {
              "projects": {
                "allOf": [
                  {
                    "contains": {
                      "properties": {
                        "layer": {
                          "const": "Abstractions"
                        }
                      },
                      "required": ["layer"]
                    }
                  },
                  {
                    "contains": {
                      "properties": {
                        "layer": {
                          "const": "Components"
                        }
                      },
                      "required": ["layer"]
                    }
                  },
                  {
                    "contains": {
                      "properties": {
                        "layer": {
                          "const": "Registration"
                        }
                      },
                      "required": ["layer"]
                    }
                  },
                  {
                    "not": {
                      "contains": {
                        "properties": {
                          "layer": {
                            "const": "Domain"
                          }
                        },
                        "required": ["layer"]
                      }
                    }
                  }
                ]
              }
            }
          },
          "dependencyRules": {
            "not": {
              "contains": {
                "properties": {
                  "fromLayer": {
                    "const": "Abstractions"
                  },
                  "allowedToLayers": {
                    "contains": {
                      "enum": ["Components", "Registration", "Workflows"]
                    }
                  }
                },
                "required": ["fromLayer", "allowedToLayers"]
              }
            }
          }
        }
      }
    }
  ],
  "$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.-]+)?$"
    },
    "runtimeVersion": {
      "type": "string",
      "pattern": "^[A-Za-z0-9][A-Za-z0-9.+-]{0,80}$"
    },
    "pathPattern": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_./*{}@-]+$"
    },
    "layer": {
      "enum": [
        "Domain",
        "Application",
        "Infrastructure",
        "Workflows",
        "Host",
        "Tests",
        "BuilderX",
        "Docs",
        "Abstractions",
        "Components",
        "Registration",
        "Contracts",
        "PackageMetadata"
      ]
    },
    "owner": {
      "type": "object",
      "additionalProperties": false,
      "required": ["scope", "name"],
      "properties": {
        "scope": {
          "enum": ["rootblocks", "organization", "project", "personal"]
        },
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 120
        }
      }
    },
    "compatibility": {
      "type": "object",
      "additionalProperties": false,
      "required": ["profileMajor", "consumerSupport", "languageRuntimeMatrix"],
      "properties": {
        "profileMajor": {
          "type": "integer",
          "minimum": 0
        },
        "consumerSupport": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["consumerName", "minimumVersion", "capabilities"],
            "properties": {
              "consumerName": {
                "enum": ["rootblocks-console", "darkfactory", "builderx", "rbstd", "agent", "custom"]
              },
              "minimumVersion": {
                "$ref": "#/$defs/semver"
              },
              "capabilities": {
                "type": "array",
                "minItems": 1,
                "items": {
                  "enum": ["select-profile", "validate-profile", "render-profile", "generate-source", "compile", "run", "debug", "component-pack"]
                },
                "uniqueItems": true
              }
            }
          }
        },
        "languageRuntimeMatrix": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/languageRuntime"
          }
        }
      }
    },
    "languageRuntime": {
      "type": "object",
      "additionalProperties": false,
      "required": ["language", "runtimes"],
      "properties": {
        "language": {
          "enum": ["dotnet", "node", "python", "java", "go", "custom"]
        },
        "frameworks": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/runtimeVersion"
          },
          "uniqueItems": true
        },
        "runtimes": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/runtimeVersion"
          },
          "uniqueItems": true
        }
      }
    },
    "renderer": {
      "type": "object",
      "additionalProperties": false,
      "required": ["rendererId", "generationModes", "requiredCapabilities"],
      "properties": {
        "rendererId": {
          "enum": ["builderx-structured-project", "builderx-component-pack", "custom"]
        },
        "generationModes": {
          "type": "array",
          "minItems": 1,
          "items": {
            "enum": ["builderx-compiler", "agent-codegen-governed", "hybrid", "template-only"]
          },
          "uniqueItems": true
        },
        "requiredCapabilities": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string",
            "pattern": "^[a-z0-9][a-z0-9.-]{1,127}$"
          },
          "uniqueItems": true
        }
      }
    },
    "layout": {
      "type": "object",
      "additionalProperties": false,
      "required": ["rootFolders", "projects"],
      "properties": {
        "rootFolders": {
          "type": "array",
          "minItems": 1,
          "items": {
            "enum": ["src", "tests", "builderx", "docs"]
          },
          "uniqueItems": true
        },
        "projects": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/projectLayout"
          }
        }
      }
    },
    "projectLayout": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "layer", "kind", "pathPattern", "required"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "layer": {
          "$ref": "#/$defs/layer"
        },
        "kind": {
          "enum": ["source", "test", "builderx", "docs", "package-metadata"]
        },
        "pathPattern": {
          "$ref": "#/$defs/pathPattern"
        },
        "required": {
          "type": "boolean"
        }
      }
    },
    "dependencyRule": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "fromLayer", "allowedToLayers", "blockedToLayers", "enforcement"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "fromLayer": {
          "$ref": "#/$defs/layer"
        },
        "allowedToLayers": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/layer"
          },
          "uniqueItems": true
        },
        "blockedToLayers": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/layer"
          },
          "uniqueItems": true
        },
        "enforcement": {
          "enum": ["block", "warn", "advisory"]
        }
      }
    },
    "sourceZones": {
      "type": "object",
      "additionalProperties": false,
      "required": ["generated", "custom"],
      "properties": {
        "generated": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/sourceZone"
          }
        },
        "custom": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/sourceZone"
          }
        }
      }
    },
    "sourceZone": {
      "type": "object",
      "additionalProperties": false,
      "required": ["pathPattern", "owner", "preservation"],
      "properties": {
        "pathPattern": {
          "$ref": "#/$defs/pathPattern"
        },
        "owner": {
          "enum": ["rootblocks-console", "builderx", "governed-agent", "manual"]
        },
        "preservation": {
          "enum": ["overwrite-generated", "preserve", "merge-with-markers", "manual-review"]
        }
      }
    },
    "sourceMapPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode", "requiredForGenerated", "criticalLayers"],
      "properties": {
        "mode": {
          "enum": ["always-required", "critical-only", "optional"]
        },
        "requiredForGenerated": {
          "type": "boolean"
        },
        "criticalLayers": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/layer"
          },
          "uniqueItems": true
        }
      }
    },
    "regressionPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required", "suiteKinds", "failureBehavior"],
      "properties": {
        "required": {
          "type": "boolean"
        },
        "suiteKinds": {
          "type": "array",
          "minItems": 1,
          "items": {
            "enum": ["unit", "contract", "integration", "workflow-regression", "smoke", "e2e"]
          },
          "uniqueItems": true
        },
        "failureBehavior": {
          "enum": ["block", "warn"]
        }
      }
    },
    "evidencePolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required", "recordProfileRef", "requiredArtifacts"],
      "properties": {
        "required": {
          "type": "boolean"
        },
        "recordProfileRef": {
          "type": "boolean"
        },
        "requiredArtifacts": {
          "type": "array",
          "minItems": 1,
          "items": {
            "enum": ["profile", "source-map", "regression-suite", "generated-source", "tests", "bundle"]
          },
          "uniqueItems": true
        }
      }
    },
    "extensionPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["allowOrganizationExtension", "globalProfileMutable"],
      "properties": {
        "allowOrganizationExtension": {
          "type": "boolean"
        },
        "globalProfileMutable": {
          "const": false
        },
        "extensionRules": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        }
      }
    }
  }
}
