---
name: step-01-collect
description: Collect all JSON source data for HTML generation
model: opus
next_step: steps/step-02-build-data.md
---

# Step 1: Collect JSON Sources

## YOUR TASK

Read all JSON analysis files for the feature and store them as `{collected_data}` for the next step.

---

## EXECUTION SEQUENCE

### 1. Locate Feature

```
1. Read feature_id from parameters
2. Locate master index.json:
   - Multi-app (project): docs/business-analyse/v{version}/index.json
   - Single-app: docs/{app}/business-analyse/v{version}/index.json
3. IF index.json NOT found → BLOCKING ERROR: "index.json not found — run /business-analyse first"
```

### 2. Detect Mode

```
IF index.json contains "applications[]" with multiple entries:
  mode = "project" (multi-app)
ELSE:
  mode = "single-app"

Store: workflow.mode
```

### 3. Verify Consolidation Status

```
IF workflow.mode === "project":
  projectFeature = ba-reader.findProjectFeature()
  FOR each app in projectFeature.applications:
    appFeature = ba-reader.findFeature(app.featureJsonPath)
    IF appFeature.status !== "consolidated":
      BLOCKING ERROR: "Application {app.code} not consolidated"
ELSE:
  feature = ba-reader.findFeature({feature_id})
  IF feature.status !== "consolidated":
    BLOCKING ERROR: "Feature status: {feature.status}, expected consolidated"
```

### 4. Read All JSON Files (FLAT-FILE ARCHITECTURE)

> **CRITICAL:** Module data lives in separate flat files (entities.json, usecases.json, etc.),
> NOT inside index.json. The module index.json only contains metadata and file pointers.
> You MUST read each flat file individually.

```
collected_data = {
  master: {
    index: READ(index.json),           // metadata, modules[], consolidation, handoff
    cadrage: READ(cadrage.json)         // context, stakeholders, scope
  },
  modules: {}
}

FOR each module in master.index.modules[]:
  // Resolve module directory path
  moduleDir = "{app}/{module.code}/business-analyse/v{version}/"
  moduleIndex = READ(moduleDir + "index.json")

  // Read ALL flat files from module directory
  const moduleData = {
    index: moduleIndex,
    entities: READ(moduleDir + "entities.json") || { entities: [] },
    rules: READ(moduleDir + "rules.json") || { rules: [] },
    usecases: READ(moduleDir + "usecases.json") || { useCases: [] },
    permissions: READ(moduleDir + "permissions.json") || { roles: [], permissions: [], matrix: [] },
    screens: READ(moduleDir + "screens.json") || { screens: [] }
  };

  // POST-COLLECT VALIDATION: detect missing mandatory data for specified modules
  const moduleStatus = moduleIndex?.metadata?.status || moduleIndex?.status || "unknown";
  const warnings = [];
  if (["specified", "consolidated", "designed", "reviewed", "handed-off"].includes(moduleStatus)) {
    if ((moduleData.entities.entities || []).length === 0)
      warnings.push("entities.json is EMPTY for specified module " + module.code);
    // Check both key conventions: useCases (camelCase) and usecases (lowercase)
    if ((moduleData.usecases.useCases || moduleData.usecases.usecases || []).length === 0)
      warnings.push("usecases.json is EMPTY for specified module " + module.code);
    // Check both key conventions: rules and businessRules
    if ((moduleData.rules.rules || moduleData.rules.businessRules || []).length === 0)
      warnings.push("rules.json is EMPTY for specified module " + module.code);
    // Check screens: both screens[] (Format A) and sections[] (Format B)
    if ((moduleData.screens.screens || moduleData.screens.sections || []).length === 0)
      warnings.push("screens.json is EMPTY for specified module " + module.code);
  }
  if (warnings.length > 0) {
    Display("⚠ WARNING — Missing data for module " + module.code + ":");
    warnings.forEach(w => Display("  - " + w));
    Display("The HTML document will have incomplete sections for this module.");
  }

  collected_data.modules[module.code] = moduleData;

// Read navigation.json if present (written by /business-analyse-design)
collected_data.navigation = READ(docs_dir + '/navigation.json') || null;
```

### 5. Display Collection Summary

```
JSON collected:
  Master: index.json + cadrage.json
  Modules: {module_count} modules (flat files: entities, rules, usecases, permissions, screens)
  Warnings: {total_warnings} (see above if any)
→ Proceeding to data build...
```

---

## VALIDATION

- BLOCK if index.json absent
- BLOCK if 0 modules found
- WARN (non-blocking) if specified module has empty mandatory files
- BLOCK if feature not consolidated

## NEXT STEP

Load: `steps/step-02-build-data.md`
