#!/bin/bash

# Script to update agents to use automation-tracking-manager

# List of agents to update with their prefixes and tracking types
declare -A agents=(
    ["architect.md"]="AUTO-ARCH|Architecture Review"
    ["architecture-guard.md"]="AUTO-GUARD|Code Analysis Report"
    ["bug-hunter.md"]="AUTO-BUG-POOL|Bug Detection Report"
    ["docs-writer.md"]="AUTO-DOCS|Documentation Report"
    ["epic-planner.md"]="AUTO-EPIC|Epic Planning Update"
    ["human-liaison.md"]="AUTO-LIAISON|Human Activity Report"
    ["spec-updater.md"]="AUTO-SPEC|Specification Update"
    ["test-infra-improver.md"]="AUTO-INF-POOL|Infrastructure Analysis"
)

# Function to update an agent file
update_agent() {
    local file=$1
    local prefix=$2
    local tracking_type=$3
    
    echo "Updating $file with prefix $prefix and tracking type '$tracking_type'"
    
    # Create a temporary file with the updated content
    cat > /tmp/tracking_update.txt << 'EOF'
## Automation Tracking System

**Updated**: This agent uses the centralized automation-tracking-manager subagent for all tracking operations.

### Tracking Issue Format
- **Health Reports**: `[PREFIX] TRACKING_TYPE (Cycle N)`
- **Announcements**: `[PREFIX] Announce: <message summary>`
- **Labels**: "Automation Tracking" + any relevant priority labels

### Tracking Operations

All tracking operations are now handled by the automation-tracking-manager subagent:

```bash
# Create a new tracking issue (closes previous automatically)
task automation-tracking-manager "CREATE_TRACKING_ISSUE" \
  --agent-prefix "PREFIX" \
  --tracking-type "TRACKING_TYPE" \
  --body "$tracking_body" \
  --repo-owner "$owner" \
  --repo-name "$repo"

# Update current tracking issue with a comment
task automation-tracking-manager "UPDATE_TRACKING_ISSUE" \
  --agent-prefix "PREFIX" \
  --tracking-type "TRACKING_TYPE" \
  --comment "$update_comment" \
  --repo-owner "$owner" \
  --repo-name "$repo"

# Get the next cycle number
next_cycle=$(task automation-tracking-manager "GET_NEXT_CYCLE_NUMBER" \
  --agent-prefix "PREFIX" \
  --tracking-type "TRACKING_TYPE" \
  --repo-owner "$owner" \
  --repo-name "$repo")

# Read tracking state from latest issue
tracking_state=$(task automation-tracking-manager "READ_TRACKING_STATE" \
  --agent-prefix "PREFIX" \
  --tracking-type "TRACKING_TYPE" \
  --repo-owner "$owner" \
  --repo-name "$repo")
```
EOF
    
    # Replace PREFIX and TRACKING_TYPE with actual values
    sed -i "s/PREFIX/$prefix/g" /tmp/tracking_update.txt
    sed -i "s/TRACKING_TYPE/$tracking_type/g" /tmp/tracking_update.txt
    
    echo "Updated tracking section saved to /tmp/tracking_update.txt"
}

# Process each agent
for agent in "${!agents[@]}"; do
    IFS='|' read -r prefix tracking_type <<< "${agents[$agent]}"
    update_agent "$agent" "$prefix" "$tracking_type"
    echo "---"
done

echo "Script complete. Manual updates still needed for each agent file."