{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/source-projects/structured-source-project.v1.schema.json",
  "title": "RootBlocks Structured Source Project v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "projectId",
    "standard",
    "profile",
    "generationMode",
    "rootLayout",
    "applicationIr",
    "sourceManifest",
    "sourceMaps",
    "regression",
    "evidence",
    "build"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.structured-source-project/v1"
    },
    "projectId": {
      "$ref": "#/$defs/id"
    },
    "standard": {
      "$ref": "#/$defs/standardRef"
    },
    "profile": {
      "$ref": "#/$defs/profileRef"
    },
    "generationMode": {
      "enum": ["builderx-compiler", "agent-codegen-governed", "hybrid"]
    },
    "rootLayout": {
      "$ref": "#/$defs/rootLayout"
    },
    "applicationIr": {
      "$ref": "#/$defs/applicationIr"
    },
    "sourceManifest": {
      "$ref": "#/$defs/sourceManifest"
    },
    "sourceMaps": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/sourceMapRef"
      }
    },
    "regression": {
      "$ref": "#/$defs/regression"
    },
    "evidence": {
      "$ref": "#/$defs/evidencePolicy"
    },
    "build": {
      "$ref": "#/$defs/build"
    }
  },
  "allOf": [
    {
      "properties": {
        "rootLayout": {
          "properties": {
            "rootFolders": {
              "allOf": [
                {
                  "contains": {
                    "properties": {
                      "path": {
                        "const": "src"
                      }
                    },
                    "required": ["path"]
                  }
                },
                {
                  "contains": {
                    "properties": {
                      "path": {
                        "const": "tests"
                      }
                    },
                    "required": ["path"]
                  }
                },
                {
                  "contains": {
                    "properties": {
                      "path": {
                        "const": "builderx"
                      }
                    },
                    "required": ["path"]
                  }
                },
                {
                  "contains": {
                    "properties": {
                      "path": {
                        "const": "docs"
                      }
                    },
                    "required": ["path"]
                  }
                },
                {
                  "contains": {
                    "properties": {
                      "path": {
                        "const": "dist"
                      }
                    },
                    "required": ["path"]
                  }
                }
              ]
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "sourceManifest": {
            "properties": {
              "files": {
                "contains": {
                  "properties": {
                    "sourceMapRequired": {
                      "const": true
                    }
                  },
                  "required": ["sourceMapRequired"]
                }
              }
            },
            "required": ["files"]
          }
        },
        "required": ["sourceManifest"]
      },
      "then": {
        "properties": {
          "sourceMaps": {
            "minItems": 1
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "regression": {
            "properties": {
              "criticalWorkflowCoverage": {
                "const": "required"
              }
            },
            "required": ["criticalWorkflowCoverage"]
          }
        },
        "required": ["regression"]
      },
      "then": {
        "properties": {
          "regression": {
            "properties": {
              "suites": {
                "contains": {
                  "properties": {
                    "type": {
                      "const": "workflow-regression"
                    },
                    "required": {
                      "const": true
                    }
                  },
                  "required": ["type", "required"]
                }
              }
            }
          }
        }
      }
    }
  ],
  "$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}$"
    },
    "path": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_./@*-]+$"
    },
    "standardRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "version", "hash"],
      "properties": {
        "bundleId": {
          "$ref": "#/$defs/id"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "profileRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["profileId", "version", "path", "hash"],
      "properties": {
        "profileId": {
          "enum": ["ddd-clean", "component-pack", "custom"]
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "path": {
          "type": "string",
          "pattern": "^profiles/architecture/.+\\.json$"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "rootLayout": {
      "type": "object",
      "additionalProperties": false,
      "required": ["rootFolders"],
      "properties": {
        "rootFolders": {
          "type": "array",
          "minItems": 5,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["path", "purpose", "retention"],
            "properties": {
              "path": {
                "enum": ["src", "tests", "builderx", "docs", "dist"]
              },
              "purpose": {
                "enum": ["source", "tests", "builderx-metadata", "docs", "reproducible-output"]
              },
              "retention": {
                "enum": ["mandatory", "output-only"]
              }
            }
          }
        }
      }
    },
    "applicationIr": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "modules", "workflows", "hosts", "ports", "adapters"],
      "properties": {
        "path": {
          "type": "string",
          "pattern": "^builderx/inventory/application-ir\\.(json|yaml|yml)$"
        },
        "modules": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/module"
          }
        },
        "workflows": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/workflowBinding"
          }
        },
        "hosts": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/hostBinding"
          }
        },
        "ports": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/portBinding"
          }
        },
        "adapters": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/adapterBinding"
          }
        }
      }
    },
    "module": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "layer", "sourceRoot", "testRoot"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "layer": {
          "enum": ["domain", "application", "infrastructure", "workflows", "host"]
        },
        "sourceRoot": {
          "type": "string",
          "pattern": "^src/.+"
        },
        "testRoot": {
          "type": "string",
          "pattern": "^tests/.+"
        }
      }
    },
    "workflowBinding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["workflowId", "flowPath", "sourcePath", "regressionSuitePath", "sourceMapRefs"],
      "properties": {
        "workflowId": {
          "$ref": "#/$defs/id"
        },
        "flowPath": {
          "type": "string",
          "pattern": "^builderx/flows/.+\\.(json|yaml|yml)$"
        },
        "sourcePath": {
          "type": "string",
          "pattern": "^src/.+"
        },
        "regressionSuitePath": {
          "type": "string",
          "pattern": "^builderx/regression/.+\\.(json|yaml|yml)$"
        },
        "sourceMapRefs": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        }
      }
    },
    "hostBinding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "kind", "sourcePath"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "kind": {
          "enum": ["api", "worker", "console", "library", "custom"]
        },
        "sourcePath": {
          "type": "string",
          "pattern": "^src/.+"
        }
      }
    },
    "portBinding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "ownerModule", "sourcePath"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "ownerModule": {
          "$ref": "#/$defs/id"
        },
        "sourcePath": {
          "type": "string",
          "pattern": "^src/.+"
        }
      }
    },
    "adapterBinding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "portId", "sourcePath"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "portId": {
          "$ref": "#/$defs/id"
        },
        "sourcePath": {
          "type": "string",
          "pattern": "^src/.+"
        }
      }
    },
    "sourceManifest": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "files", "generatedZones", "customZones"],
      "properties": {
        "path": {
          "type": "string",
          "pattern": "^builderx/inventory/generated-source\\.(json|yaml|yml)$"
        },
        "files": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/sourceFile"
          }
        },
        "generatedZones": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/sourceZone"
          }
        },
        "customZones": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/sourceZone"
          }
        }
      }
    },
    "sourceFile": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "role", "hash", "generatedBy", "sourceMapRequired", "protectedZone"],
      "properties": {
        "path": {
          "type": "string",
          "pattern": "^(src|tests|builderx|docs)/.+"
        },
        "role": {
          "enum": ["generated-source", "generated-test", "builderx-metadata", "source-map", "docs"]
        },
        "hash": {
          "$ref": "#/$defs/hash"
        },
        "generatedBy": {
          "enum": ["builderx-compiler", "agent-codegen-governed", "hybrid", "manual-governed"]
        },
        "sourceMapRequired": {
          "type": "boolean"
        },
        "sourceMapRefs": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "protectedZone": {
          "type": "boolean"
        }
      }
    },
    "sourceZone": {
      "type": "object",
      "additionalProperties": false,
      "required": ["pathPattern", "owner", "driftPolicy"],
      "properties": {
        "pathPattern": {
          "$ref": "#/$defs/path"
        },
        "owner": {
          "enum": ["builderx", "rootblocks-console", "governed-agent", "manual"]
        },
        "driftPolicy": {
          "enum": ["overwrite-generated", "preserve", "merge-with-markers", "manual-review"]
        }
      }
    },
    "sourceMapRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "path", "covers"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "path": {
          "type": "string",
          "pattern": "^builderx/source-maps/.+\\.(json|yaml|yml)$"
        },
        "covers": {
          "type": "array",
          "minItems": 1,
          "items": {
            "enum": ["construction", "workflow-node", "use-case", "adapter", "endpoint", "generated-source", "test", "evidence"]
          },
          "uniqueItems": true
        }
      }
    },
    "regression": {
      "type": "object",
      "additionalProperties": false,
      "required": ["criticalWorkflowCoverage", "suites"],
      "properties": {
        "criticalWorkflowCoverage": {
          "enum": ["required", "advisory"]
        },
        "suites": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/regressionSuite"
          }
        }
      }
    },
    "regressionSuite": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "path", "required"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "type": {
          "enum": ["unit", "contract", "integration", "workflow-regression", "smoke", "e2e"]
        },
        "path": {
          "type": "string",
          "pattern": "^(tests|builderx/regression)/.+"
        },
        "required": {
          "type": "boolean"
        }
      }
    },
    "evidencePolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required", "reproducibilityRequired", "evidenceRefs"],
      "properties": {
        "required": {
          "const": true
        },
        "reproducibilityRequired": {
          "const": true
        },
        "evidenceRefs": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string",
            "pattern": "^evidence/.+\\.(json|yaml|yml)$"
          },
          "uniqueItems": true
        },
        "sourceAuditReportRef": {
          "type": "string",
          "pattern": "^evidence/.+\\.source-audit\\.(json|yaml|yml)$"
        }
      }
    },
    "build": {
      "type": "object",
      "additionalProperties": false,
      "required": ["commands", "outputs"],
      "properties": {
        "commands": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["id", "command", "workingDirectory"],
            "properties": {
              "id": {
                "$ref": "#/$defs/id"
              },
              "command": {
                "type": "string",
                "minLength": 1,
                "maxLength": 240
              },
              "workingDirectory": {
                "enum": [".", "src", "tests"]
              }
            }
          }
        },
        "outputs": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["id", "kind", "path", "hash", "rebuildableFromSource"],
            "properties": {
              "id": {
                "$ref": "#/$defs/id"
              },
              "kind": {
                "enum": ["binary", "package", "container-image", "sbom", "test-report", "coverage-report"]
              },
              "path": {
                "type": "string",
                "pattern": "^dist/.+"
              },
              "hash": {
                "$ref": "#/$defs/hash"
              },
              "rebuildableFromSource": {
                "const": true
              }
            }
          }
        }
      }
    }
  }
}
