#!/bin/bash
# Monitor QA queues with graceful handling of missing directories
# Location: ~/.config/mosaic/tools/qa/qa-queue-monitor.sh

PROJECT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || pwd)

echo "=== QA Automation Queue Status ==="
echo "Project: $(basename "$PROJECT_ROOT")"
echo "Time: $(date '+%Y-%m-%d %H:%M:%S')"
echo

# Function to count files safely
count_files() {
    local dir="$1"
    if [ -d "$dir" ]; then
        ls "$dir" 2>/dev/null | wc -l
    else
        echo "0"
    fi
}

# Check Epic-specific queues
EPIC_BASE="$PROJECT_ROOT/docs/task-management/epics/active"
if [ -d "$EPIC_BASE" ]; then
    for EPIC_DIR in "$EPIC_BASE"/*/; do
        if [ -d "$EPIC_DIR" ]; then
            EPIC_NAME=$(basename "$EPIC_DIR")
            QA_DIR="$EPIC_DIR/reports/qa-automation"
            
            if [ -d "$QA_DIR" ]; then
                echo "Epic: $EPIC_NAME"
                echo "  Pending: $(count_files "$QA_DIR/pending")"
                echo "  In Progress: $(count_files "$QA_DIR/in-progress")"
                echo "  Done: $(count_files "$QA_DIR/done")"
                echo "  Escalated: $(count_files "$QA_DIR/escalated")"
                
                # Show escalated files if any
                if [ -d "$QA_DIR/escalated" ] && [ "$(ls "$QA_DIR/escalated" 2>/dev/null | wc -l)" -gt 0 ]; then
                    echo "  ⚠️ Escalated Issues:"
                    for file in "$QA_DIR/escalated"/*_remediation_needed.md; do
                        if [ -f "$file" ]; then
                            echo "    - $(basename "$file")"
                        fi
                    done
                fi
                echo
            fi
        fi
    done
else
    echo "[WARN] No Epic structure found at: $EPIC_BASE"
    echo
fi

# Check general queue
GENERAL_DIR="$PROJECT_ROOT/docs/reports/qa-automation"
if [ -d "$GENERAL_DIR" ]; then
    echo "General (Non-Epic):"
    echo "  Pending: $(count_files "$GENERAL_DIR/pending")"
    echo "  In Progress: $(count_files "$GENERAL_DIR/in-progress")"
    echo "  Done: $(count_files "$GENERAL_DIR/done")"
    echo "  Escalated: $(count_files "$GENERAL_DIR/escalated")"
    
    # Show escalated files
    if [ -d "$GENERAL_DIR/escalated" ] && [ "$(ls "$GENERAL_DIR/escalated" 2>/dev/null | wc -l)" -gt 0 ]; then
        echo "  ⚠️ Escalated Issues:"
        for file in "$GENERAL_DIR/escalated"/*_remediation_needed.md; do
            if [ -f "$file" ]; then
                echo "    - $(basename "$file")"
            fi
        done
    fi
else
    echo "[INFO] No general QA directory found (will be created on first use)"
fi

echo
echo "=== Recent Activity ==="
# Show last 5 log entries
if [ -f "$PROJECT_ROOT/logs/qa-automation.log" ]; then
    tail -5 "$PROJECT_ROOT/logs/qa-automation.log"
else
    echo "No activity log found"
fi

echo
echo "=== Queue Processing Tips ==="
echo "• View pending reports: ls -la $PROJECT_ROOT/docs/reports/qa-automation/pending/"
echo "• Check stale reports: find $PROJECT_ROOT -path '*/in-progress/*' -mmin +60"
echo "• Manual escalation: mv {report} {path}/escalated/"
echo "• View full log: tail -f $PROJECT_ROOT/logs/qa-automation.log"
