import json
import subprocess
import sys

# Agent ID 映射
agents = {
    "前端开发专家": {
        "id": "8ddccf1d-9ed4-469e-a335-a14d0b72d025",
        "active_tasks": 7,
        "keywords": ["前端", "frontend", "UI", "界面", "组件", "vue", "react"]
    },
    "后端开发专家": {
        "id": "79fbfb25-e622-4986-9bb9-21efe499274d",
        "active_tasks": 6,
        "keywords": ["后端", "backend", "API", "数据库", "服务器"]
    },
    "自动化测试与QA": {
        "id": "64b26c5e-1823-477c-9c0f-c5c01d599365",
        "active_tasks": 0,
        "keywords": ["测试", "test", "QA", "E2E", "单元测试"]
    },
    "代码评审专家": {
        "id": "34d7c53d-bd70-45a8-bbbb-77dbb1da16b5",
        "active_tasks": 31,
        "keywords": ["评审", "review", "代码审查"]
    },
    "架构师兼项目经理": {
        "id": "d1e4fe91-fb56-4c47-95d0-818d5f22b5bd",
        "active_tasks": 16,
        "keywords": ["架构", "设计", "规划", "文档", "SPEC", "API文档"]
    }
}

# 读取 todo 任务
with open('/tmp/todo_issues.json', 'r') as f:
    todo_data = json.load(f)

todo_issues = todo_data.get('issues', [])

print("=" * 80)
print("智能任务调度报告")
print("=" * 80)
print(f"\n当前时间: 2026-05-29 18:00")
print(f"\n待办任务数: {len(todo_issues)}")

if len(todo_issues) == 0:
    print("\n✅ 没有待办任务需要分配")
    sys.exit(0)

print("\n" + "=" * 80)
print("Agent 负载情况")
print("=" * 80)

for agent_name, info in agents.items():
    status = "🟢 空闲" if info['active_tasks'] == 0 else f"🟡 忙碌 ({info['active_tasks']}个任务)"
    if info['active_tasks'] >= 3:
        status = f"🔴 超载 ({info['active_tasks']}个任务)"
    print(f"{agent_name}: {status}")

print("\n" + "=" * 80)
print("待办任务分析")
print("=" * 80)

for issue in todo_issues:
    print(f"\n任务: {issue['identifier']} - {issue['title']}")
    print(f"优先级: {issue['priority']}")
    print(f"当前分配: {issue.get('assignee_id', '无')}")
    
    # 智能匹配
    title_lower = issue['title'].lower()
    desc_lower = issue.get('description', '').lower()
    combined = title_lower + " " + desc_lower
    
    matched_agent = None
    for agent_name, info in agents.items():
        for keyword in info['keywords']:
            if keyword.lower() in combined:
                matched_agent = agent_name
                break
        if matched_agent:
            break
    
    if matched_agent:
        print(f"建议分配: {matched_agent} (匹配关键词)")
    else:
        # 默认分配给最空闲的 agent
        sorted_agents = sorted(agents.items(), key=lambda x: x[1]['active_tasks'])
        matched_agent = sorted_agents[0][0]
        print(f"建议分配: {matched_agent} (负载最低)")

print("\n" + "=" * 80)
print("调度决策")
print("=" * 80)

# 当前只有一个 todo 任务，且已分配
issue = todo_issues[0]
print(f"\n任务 {issue['identifier']}: {issue['title']}")
print(f"当前状态: {issue['status']}")
print(f"优先级: {issue['priority']}")
print(f"已分配给: agent ID {issue['assignee_id']}")

# 检查是否是 MiniMax.io agent
if issue['assignee_id'] == 'c3178464-27be-4b20-b515-d5dbe50a0b60':
    print("\n⚠️ 注意: 该任务分配给了 MiniMax.io agent")
    print("该 agent 历史任务全部失败，建议重新分配")
else:
    print("\n✅ 任务已有合适的 assignee，无需重新分配")

print("\n" + "=" * 80)
print("总结")
print("=" * 80)
print("\n当前工作区任务分配健康:")
print("- 1 个 todo 任务已分配")
print("- 0 个 backlog 任务")
print("- 大部分 agent 负载合理")
print("\n建议: 监控 MiniMax.io agent 的任务执行情况")

