---
name: step-01-analyze
description: Analyze existing codebase - find Entity, DbContext, patterns
next_step: steps/step-02-plan.md
---

# Step 1: Analyze Codebase

## YOUR TASK:

Gather all context needed to generate the controller. Find the Entity, check DbContext, analyze existing patterns.

**ULTRA THINK before searching.**

---

## EXECUTION SEQUENCE:

### 1. Find Entity Domain

**Search for entity file:**

```
Glob: "src/SmartStack.Domain/**/{entity}.cs"
Glob: "src/SmartStack.Domain/**/{entity}s.cs"
```

**Extract from Entity:**
- Properties (for DTO generation)
- Factory method signature `Create(...)`
- Update method signature `Update(...)`
- Relationships (navigation properties)
- Base class (BaseEntity, AuditableEntity, TenantEntity)

**If Entity NOT found:**
```
STOP - Entity {entity} not found in Domain layer.
Create entity first using /application skill to add the entity to Domain
```

### 2. Check DbContext

**Verify DbSet exists:**

```
Grep: "DbSet<{entity}>" in IApplicationDbContext.cs
```

**If DbSet NOT found:**
```
WARNING: DbSet<{entity}> not in DbContext.
Will need to add: DbSet<{entity}> {module} { get; }
```

### 3. Analyze Existing Permissions

**Check current permissions:**

```
Read: src/SmartStack.Application/Common/Authorization/Permissions.cs
```

**Look for:**
- Existing permission class for this module
- Permission naming pattern used
- Nested class structure

### 4. Find Similar Controllers

**Find reference controllers in same area:**

```
Glob: "src/SmartStack.Api/Controllers/{area}/*.cs"
```

**Extract patterns from existing controllers:**
- Constructor injection pattern
- Base class used
- Common attributes
- Response type patterns
- Error handling patterns

### 5. Check for Existing Controller

**Verify controller doesn't exist:**

```
Glob: "src/SmartStack.Api/Controllers/{area}/{module}Controller.cs"
```

**If EXISTS:**
```
WARNING: Controller already exists!
Options:
1. Overwrite (will backup existing)
2. Abort generation
```

### 6. Compile Analysis Report

**Store in state:**

```
{entity_found}       = true/false
{entity_path}        = "src/SmartStack.Domain/..."
{entity_properties}  = [list of properties]
{entity_base_class}  = "TenantEntity" | "AuditableEntity" | "BaseEntity"
{dbset_exists}       = true/false
{permissions_exist}  = true/false
{similar_controller} = "path/to/similar.cs" (for reference)
{controller_exists}  = true/false
```

---

## OUTPUT FORMAT:

```
Analysis Complete:

| Check | Status | Details |
|-------|--------|---------|
| Entity | ✅/❌ | {entity_path} |
| DbSet | ✅/❌ | DbSet<{entity}> |
| Permissions | ✅/❌ | {permission_path}.* |
| Reference | ✅ | {similar_controller} |
| Existing | ⚠️/✅ | Controller exists/new |

Entity Properties:
- Id (Guid)
- Name (string)
- ... (extracted from entity)

-> Planning controller structure...
```

---

## BLOCKING CONDITIONS:

- **Entity not found** → STOP, suggest creating entity first
- **Controller exists + auto_mode=false** → ASK user to confirm overwrite

## NEXT STEP:

After analysis complete, proceed to `./step-02-plan.md`
