---
name: setup-project-context
description: Analyze codebase and configure project context interactively
arguments:
  - name: mode
    description: Detection mode (--auto minimal questions, --full comprehensive)
    required: false
---

# Setup Project Context

Configure `.claude/rules/00-project-context.md` by analyzing the codebase and asking targeted questions.

## Execution

### Phase 1: Automatic Detection

Analyze the following files and directories:

**Configuration Files:**
- `package.json` → Node.js project name, dependencies, scripts
- `composer.json` → PHP project name, dependencies, framework
- `pubspec.yaml` → Flutter/Dart project name, dependencies
- `requirements.txt` / `pyproject.toml` → Python dependencies
- `Cargo.toml` → Rust project
- `go.mod` → Go module

**Environment & Config:**
- `.env`, `.env.example` → Database, services
- `config/` → Framework configuration
- `docker-compose.yml` → Services (DB, Redis, etc.)

**Structure:**
- `src/`, `lib/`, `app/` → Source code location
- `tests/`, `spec/` → Testing framework
- `docs/`, `specifications/` → Documentation
- `.github/`, `.gitlab-ci.yml` → CI/CD

**Domain (if applicable):**
- `src/Entity/`, `src/Domain/` → Business entities (PHP/Symfony)
- `lib/models/`, `lib/domain/` → Models (Flutter/Dart)
- `models/`, `schemas/` → Data models
- `migrations/` → Database schema

Display analysis results:

```
╔══════════════════════════════════════════════════════════════╗
║                 PROJECT ANALYSIS RESULTS                      ║
╚══════════════════════════════════════════════════════════════╝

✅ Detected Information:
┌─────────────────┬────────────────────────────────┐
│ Element         │ Value                          │
├─────────────────┼────────────────────────────────┤
│ Project Name    │ {detected_name}                │
│ Language        │ {detected_language}            │
│ Framework       │ {detected_framework}           │
│ Database        │ {detected_database}            │
│ Testing         │ {detected_testing}             │
│ CI/CD           │ {detected_cicd}                │
└─────────────────┴────────────────────────────────┘

📁 Project Structure:
{detected_structure}

📄 Documentation Found:
{detected_docs}

❌ Not Detected (will ask):
- {missing_items}
```

### Phase 2: Interactive Questions

Ask only for information NOT detected in Phase 1.
Skip questions if `--auto` mode is used and a reasonable default exists.

**Essential Questions:**

1. **Application Type** (if not detected):
   ```
   What type of application is this?
   [ ] REST API      [ ] Web Application    [ ] Mobile App
   [ ] CLI Tool      [ ] Library/Package    [ ] Monorepo
   ```

2. **Business Domain**:
   ```
   What is the business domain?
   [ ] E-commerce    [ ] SaaS Platform      [ ] FinTech
   [ ] HealthTech    [ ] EdTech             [ ] Social/Community
   [ ] Media/Content [ ] IoT                [ ] Other: _____
   ```

3. **Target Users** (2-3 personas):
   ```
   Describe your main users:

   Primary User:
   > Role: _____
   > Main goal: _____

   Secondary User (optional):
   > Role: _____
   > Main goal: _____
   ```

4. **Compliance Requirements**:
   ```
   Which compliance requirements apply?
   [ ] GDPR (EU data protection)
   [ ] HIPAA (US healthcare)
   [ ] PCI-DSS (Payment card)
   [ ] SOC2 (Security)
   [ ] None / Not applicable
   ```

**Extended Questions** (only with `--full` mode):

5. **Business Goals**:
   ```
   Short-term goals (3-6 months):
   > _____

   Medium-term goals (6-12 months):
   > _____
   ```

6. **Known Issues/Technical Debt**:
   ```
   Any known issues or technical debt to document?
   > _____
   ```

7. **Glossary Terms**:
   ```
   Key business terms to define (comma-separated):
   > _____
   ```

### Phase 3: Generate Context File

Create `.claude/rules/00-project-context.md`:

```markdown
# Project Context - {PROJECT_NAME}

> Auto-generated by `/common:setup-project-context` on {DATE}
> Review and customize as needed.

## Plan Mode

> **Plan mode is recommended.** Claude activates plan mode to structure the approach, identify dependencies, and present a generation strategy before creating artifacts.

## Overview

**{PROJECT_NAME}** is a {TYPE} application for the {DOMAIN} domain.

{DESCRIPTION_FROM_README_OR_USER}

## Technical Stack

| Component    | Technology           |
|--------------|----------------------|
| Language     | {LANGUAGE}           |
| Framework    | {FRAMEWORK}          |
| Database     | {DATABASE}           |
| Cache        | {CACHE_IF_DETECTED}  |
| Testing      | {TESTING_FRAMEWORKS} |
| CI/CD        | {CICD_PLATFORM}      |

## Project Structure

```
{DETECTED_STRUCTURE}
```

## Business Domain

### Core Concepts

{ENTITIES_IF_DETECTED}

### Bounded Contexts

<!-- Add if using DDD -->
- Context 1: ...
- Context 2: ...

## Users & Personas

### {PRIMARY_USER_ROLE}
- **Goal:** {PRIMARY_USER_GOAL}
- **Pain points:** To be documented
- **Key workflows:** To be documented

### {SECONDARY_USER_ROLE}
- **Goal:** {SECONDARY_USER_GOAL}
- **Pain points:** To be documented
- **Key workflows:** To be documented

## Constraints

### Compliance
{COMPLIANCE_REQUIREMENTS}

### Performance Targets
- Page load time: < 3s
- API response time: < 200ms
- Availability: 99.9%

### Security Requirements
- OWASP Top 10 compliance
- Input validation on all endpoints
- Authentication required for protected resources

## Goals

### Short-term
{SHORT_TERM_GOALS_OR_PLACEHOLDER}

### Medium-term
{MEDIUM_TERM_GOALS_OR_PLACEHOLDER}

## Known Issues / Technical Debt

{ISSUES_OR_PLACEHOLDER}

## Glossary

| Term | Definition |
|------|------------|
{GLOSSARY_TERMS_OR_EXAMPLES}
```

### Phase 4: Validation & Next Steps

Display summary and recommendations:

```
╔══════════════════════════════════════════════════════════════╗
║              PROJECT CONTEXT GENERATED                        ║
╚══════════════════════════════════════════════════════════════╝

✅ File created: .claude/rules/00-project-context.md

Summary:
┌─────────────────┬────────────────────────────────┐
│ Project         │ {PROJECT_NAME}                 │
│ Type            │ {TYPE}                         │
│ Stack           │ {FRAMEWORK} + {DATABASE}       │
│ Domain          │ {DOMAIN}                       │
│ Compliance      │ {COMPLIANCE}                   │
│ Personas        │ {COUNT} defined                │
└─────────────────┴────────────────────────────────┘

📋 Recommended Next Steps:

1. Review generated file and complete placeholder sections
2. Add detailed bounded contexts if using DDD
3. Document key business workflows
4. Consider running specialized agents:
   - @database-architect → Document database schema
   - @api-designer → Document API endpoints
   - @security-reviewer → Review security constraints

Would you like me to open the file for review?
```

## Modes

| Mode | Behavior |
|------|----------|
| (default) | Detect + essential questions (type, domain, users, compliance) |
| `--auto` | Maximum auto-detection, skip questions with reasonable defaults |
| `--full` | All questions including goals, issues, and glossary |

## Examples

```bash
# Standard mode - balanced detection and questions
/common:setup-project-context

# Auto mode - minimal interaction
/common:setup-project-context --auto

# Full mode - comprehensive questionnaire
/common:setup-project-context --full
```
