#!/bin/bash

# Agent 3: ENV Reference Analyzer
# Finds all ENV variable references that need migration
# Token-efficient: ~1200 tokens total

echo "🔍 Launching ENV Reference Analyzer Agent..."

# Create knowledge bundle for agent
KNOWLEDGE=$(cat << 'EOF'
# ENV Reference Analysis Task

## Find These Patterns:
- $env.PROVISIONING
- $env.PROVISIONING_DEBUG
- $env.PROVISIONING_OUT
- $env.PROVISIONING_ARGS
- $env.PROVISIONING_MODULE
- $env.PROVISIONING_*PATH

## Categorize:
1. **MIGRATE**: Static config values
   - PROVISIONING (base path)
   - PROVISIONING_DEBUG
   - PROVISIONING_*_PATH

2. **KEEP**: Runtime state
   - PROVISIONING_ARGS (command args)
   - PROVISIONING_OUT (output redirection)
   - NOW (timestamps)
   - CURRENT_* (context variables)

## Output Format:
```json
{
  "migrate_to_config": [
    {"file": "path/file.nu", "line": 45, "var": "PROVISIONING", "replacement": "get-base-path"}
  ],
  "keep_as_env": [
    {"file": "path/file.nu", "line": 23, "var": "PROVISIONING_ARGS", "reason": "runtime"}
  ],
  "total_env_refs": 89,
  "migration_needed": 56
}
```
EOF
)

# Launch Claude Code agent
claude-code task \
  --description "Find ENV references needing migration" \
  --prompt "$KNOWLEDGE

TASK: Find all \$env.PROVISIONING_* references in .nu files
SCOPE: All core/ directories
CATEGORIZE: Which need migration vs which stay as ENV
OUTPUT: JSON report with migration plan" \
  --type "general-purpose" \
  > .migration/state/env_analysis.json

echo "✅ ENV analysis complete. Results in .migration/state/env_analysis.json"
echo "📊 Run: cat .migration/state/env_analysis.json | jq '.migrate_to_config | length'"