# /manage — Interactive Data Editor

## Usage
```
/manage show me all ideas with priority=critical
/manage change the priority of idea-emergency-001 to high
/manage add a new idea: "Add telemetry dashboard for command usage"
/manage move idea-003 to approved stage
/manage update our MRR to 5000 in business context
/manage show ideas stuck in inbox for more than 7 days
/manage delete hypothesis hyp-005
/manage list all goals that are behind
```

## Input
- `$ARGUMENTS` — **required**: natural-language request describing what to query, create, update, or delete

## Data File Map

| File | Structure | Contains |
|------|-----------|----------|
| `data/ideas.json` | `{ ideas: BusinessIdea[] }` | Innovation pipeline |
| `data/goals.json` | `{ goals: BusinessGoal[] }` | Business goals + KPIs |
| `data/hypotheses.json` | `{ hypotheses: BusinessHypothesis[] }` | Testable hypotheses |
| `data/business-context.json` | `BusinessConfig` | Product info, funnels, monetization |
| `data/config.json` | `AnalystConfig` | Repos, schedules, autonomy settings |
| `data/competitors.json` | `{ competitors[], last_updated }` | Competitor intelligence |
| `data/positioning.json` | `PositioningData` | Brand positioning & copy bank |
| `data/sessions.json` | `{ sessions: AnalysisSession[] }` | Analysis session history |

## Schema Reference (Valid Enum Values)

```
Priority:          critical | high | medium | low
Stage:             inbox | under_review | approved | in_progress | testing | shipped | deferred | rejected | rolled_back
Category:          product | ux_design | growth | performance | tech_debt | security | infrastructure | content | analytics | integration
Effort (size):     xs | s | m | l | xl
Impact (size):     xs | s | m | l | xl
GoalCategory:      acquisition | activation | retention | revenue | referral
GoalStatus:        on_track | at_risk | behind | achieved
HypothesisStatus:  stated | testing | validated | invalidated | deferred
SourceType:        codebase_analysis | metrics_review | market_research | seo_audit | manual
KPISource:         manual | rest_api | posthog | firestore | stripe | whatsapp
KPIDirection:      higher_is_better | lower_is_better
```

## Process

### Step 1: Parse Request
Analyze `$ARGUMENTS` to determine:
- **Intent**: query, create, update, delete, or bulk operation
- **Entity type**: idea, goal, hypothesis, business-context, config, competitor, positioning, session
- **Target**: specific ID (e.g., `idea-emergency-001`) or filter criteria (e.g., `priority=critical`)
- **Fields to change**: which fields and their new values

### Step 2: Load State
Read **only** the data file(s) needed for this operation. Do NOT read all files.

- Idea operations → `data/ideas.json`
- Goal operations → `data/goals.json`
- Hypothesis operations → `data/hypotheses.json`
- Business context → `data/business-context.json`
- Config changes → `data/config.json`
- Competitor operations → `data/competitors.json`
- Positioning operations → `data/positioning.json`
- Session queries → `data/sessions.json`

### Step 3: Execute

#### For **queries** (show, list, find, count):
- Filter the data based on the request criteria
- Format results as a readable table or summary
- Include relevant fields (id, title, stage, priority at minimum for ideas)
- For time-based queries (e.g., "stuck in inbox for 7 days"), compare `created_at` or `updated_at` against current date

#### For **create** (add, new, create):
- Generate an ID following the existing pattern:
  - Ideas: `idea-[slug]-NNN` (e.g., `idea-telemetry-001`)
  - Goals: `goal-NNN` (e.g., `goal-005`)
  - Hypotheses: `hyp-NNN` (e.g., `hyp-010`)
- Set `created_at` and `updated_at` to current ISO timestamp
- Set defaults for required fields not specified by the user:
  - Ideas: `stage: "inbox"`, `priority: "medium"`, `source: { type: "manual" }`
  - Hypotheses: `status: "stated"`, `priority: "medium"`
  - Goals: `status: "on_track"`, `kpis: []`
- Append to the appropriate array in the JSON file
- Show the full created entity

#### For **update** (change, set, move, update):
- Find the entity by ID (exact match)
- If ID is ambiguous or not found, search by title substring and ask for confirmation
- Record the **before** value of changed fields
- Apply the changes
- Set `updated_at` to current ISO timestamp
- Show a before/after diff

#### For **delete** (remove, delete):
- Find the entity by ID
- Show what will be deleted and ask for confirmation before proceeding
- Remove the entity from the array
- Show confirmation of deletion

#### For **bulk operations** (e.g., "move all inbox ideas older than 30 days to deferred"):
- Show all entities that match the criteria
- Ask for confirmation before applying changes
- Apply changes to all matching entities
- Show summary of changes

### Step 4: Validate
Before writing any changes, verify:
1. **Enum values are valid** — check against the Schema Reference above
2. **Required fields are present** — id, title, created_at, updated_at at minimum
3. **IDs are unique** — no duplicate IDs in the array
4. **Array structure preserved** — the file still has the correct top-level structure (e.g., `{ "ideas": [...] }`)
5. **Timestamps are ISO 8601** — format: `2026-02-16T12:00:00Z`

If validation fails, report the error and do NOT write the file.

### Step 5: Report
After executing, print a summary:

**For queries:**
```
📋 Found N [entities] matching "[criteria]"
[formatted table or list]
```

**For mutations (create/update/delete):**
```
✅ [Action] completed

Entity: [id]
File: data/[file].json

[Before → After diff for changed fields]
```

## Safety Rules
1. **Always read before writing** — never modify a file without reading it first
2. **Preserve structure** — maintain the JSON array wrapper and all unchanged fields
3. **Validate enums** — reject invalid enum values with a helpful error message listing valid options
4. **Update timestamps** — set `updated_at` on every mutation
5. **Confirm destructive actions** — ask before delete or bulk operations
6. **One file at a time** — avoid writing multiple data files in a single operation unless explicitly requested

## Output Files
- The specific `data/*.json` file(s) modified by the operation
