#!/bin/bash

# GitHub Issue 33 Real-time Progress Monitor
# Runs every 3 minutes to update issue with current swarm progress

ISSUE_URL="https://github.com/cgbarlow/sasi/issues/33"
LOG_FILE="github-updates.log"

# Function to get current error count
get_error_count() {
    npm run build 2>&1 | grep -c "error TS" || echo "0"
}

# Function to get git status
get_git_status() {
    echo "Branch: $(git branch --show-current)"
    echo "Recent commits:"
    git log --oneline -3
    echo "Modified files: $(git status --porcelain | wc -l)"
}

# Function to create GitHub update content
create_update() {
    local error_count=$1
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    
    cat > github-issue-33-realtime.md << EOF
# 🐝 SWARM UPDATE - $(date '+%H:%M:%S')

## 📊 Current Status
- **Errors:** $error_count TypeScript errors
- **Swarm:** 6 agents active in parallel
- **Branch:** $(git branch --show-current)
- **Last Update:** $timestamp

## 🔄 Agent Activity
- **Type Agent:** Fixing TS2339 property errors
- **Import Agent:** Resolving TS2304 module issues  
- **Flow Agent:** Handling TS2551 initialization
- **Build Monitor:** Real-time error tracking
- **Memory Coordinator:** Cross-agent sync
- **Priority Manager:** Task optimization

## 📈 Progress Trajectory
\`\`\`
Previous: 249 errors (baseline)
Current:  $error_count errors
Reduction: $((249 - error_count)) errors eliminated
\`\`\`

## 🎯 Next Check: $(date -d '+3 minutes' '+%H:%M:%S')

*Auto-generated by Swarm Coordination System*
EOF
}

# Main monitoring loop
monitor_progress() {
    echo "[$(date)] Starting GitHub Issue 33 monitoring..." >> $LOG_FILE
    
    while true; do
        echo "[$(date)] Checking build status..." >> $LOG_FILE
        
        # Get current error count
        ERROR_COUNT=$(get_error_count)
        
        # Create update
        create_update $ERROR_COUNT
        
        # Log progress
        echo "[$(date)] Update created: $ERROR_COUNT errors detected" >> $LOG_FILE
        
        # Store in memory
        npx claude-flow@alpha hooks notify --message "GitHub monitor: $ERROR_COUNT errors tracked" --level "info" 2>/dev/null
        
        # Break if we reach 0 errors
        if [ "$ERROR_COUNT" -eq 0 ]; then
            echo "[$(date)] SUCCESS: 0 errors achieved! Monitoring complete." >> $LOG_FILE
            break
        fi
        
        # Wait 3 minutes before next check
        sleep 180
    done
}

# Run if called directly
if [ "${BASH_SOURCE[0]}" == "${0}" ]; then
    monitor_progress
fi
EOF

chmod +x github-monitor.sh