#!/bin/bash
# 系统状态检查脚本 - 带git冲突处理

# 自动处理git冲突，拉取最新状态
git stash push -q > /dev/null 2>&1
git pull origin main > /dev/null 2>&1
git stash pop -q > /dev/null 2>&1

echo "========================================="
echo "📊 任务管理系统状态报告"
echo "时间: $(date '+%Y-%m-%d %H:%M:%S')"
echo "========================================="
echo ""

echo "📁 任务文件统计:"
echo "  pending:   $(ls management/tasks/pending/*.md 2>/dev/null | wc -l | xargs) 个"
echo "  assigned:  $(ls management/tasks/assigned/*.md 2>/dev/null | wc -l | xargs) 个"
echo ""

echo "👥 Agent 状态:"
for AGENT in pangtong zhangfei guanyu zhaoyun jiangwei simayi; do
  # 检查任务文件
  TASK_COUNT=$(ls management/agents/${AGENT}/*.task 2>/dev/null | wc -l | xargs)
  DONE_COUNT=$(ls management/agents/${AGENT}/*.done 2>/dev/null | wc -l | xargs)
  
  # 检查进程
  PROCESS_COUNT=$(ps aux | grep -c "agent_monitor_fixed.sh $AGENT")
  if [ "$PROCESS_COUNT" -gt 0 ]; then
    PROCESS_STATUS="✅ 运行中"
  else
    PROCESS_STATUS="❌ 未运行"
  fi
  
  # 列出未完成任务ID
  echo "  $AGENT: $DONE_COUNT/$((DONE_COUNT + TASK_COUNT)) 完成, $PROCESS_STATUS"
  if [ "$TASK_COUNT" -gt 0 ]; then
    echo "  未完成任务:"
    ls management/agents/${AGENT}/*.task 2>/dev/null | while read line; do
      echo "    $line" | grep -o "TASK-[0-9]*"
    done
  fi
done

echo ""
echo "🚀 使用方法:"
echo "  创建新任务: ./management/workflow/scripts/create_simple.sh \"任务描述\""
echo "  分配任务: ./management/workflow/scripts/assign_simple.sh TASK-ID agent-name"
echo "  检查状态: ./management/workflow/scripts/check_status.sh"
echo "  查看日志: tail -f agent-name_monitor.log"
echo ""
echo "🌐 Gitee仓库: https://gitee.com/cfdaily/sanguo_quant_live"
echo "========================================="
