#!/bin/bash
# Agent协作系统设置脚本
# 为一人公司创建多agent git协作环境

set -e

echo "🚀 设置AI Agent协作系统..."

# 创建必要的目录
mkdir -p agents/keys
mkdir -p agents/logs

# 设置权限
chmod 700 agents/keys

# 检查依赖
check_dependency() {
    if ! command -v $1 &> /dev/null; then
        echo "❌ 需要安装: $1"
        exit 1
    fi
}

check_dependency "git"
check_dependency "ssh-keygen"

echo "✅ 依赖检查通过"

# 初始化agent身份管理器
echo "🤖 初始化agent身份..."
python3 agents/agent_identity_manager.py

# 创建git hooks模板
cat > agents/pre-commit-hook << 'EOF'
#!/bin/bash
# Agent提交前的钩子

echo "🔍 检查agent身份..."
AGENT_NAME=$(git config user.name)
if [[ -z "$AGENT_NAME" ]]; then
    echo "❌ 未设置agent身份，请先使用agent协作系统"
    exit 1
fi

echo "✅ 当前agent: $AGENT_NAME"
EOF

chmod +x agents/pre-commit-hook

# 创建快速切换脚本
cat > agents/switch_agent.sh << 'EOF'
#!/bin/bash
# 快速切换agent身份

if [[ $# -eq 0 ]]; then
    echo "用法: ./switch_agent.sh <agent名称>"
    echo "可用agents:"
    python3 -c "
import sys
sys.path.append('agents')
from agent_identity_manager import AgentIdentityManager
manager = AgentIdentityManager()
for agent in manager.list_agents():
    print(f'  - {agent[\"name\"]} ({agent[\"role\"]})')
    "
    exit 1
fi

AGENT_NAME=$1
echo "🔄 切换到agent: $AGENT_NAME"
python3 -c "
import sys
sys.path.append('agents')
from agent_identity_manager import AgentIdentityManager
manager = AgentIdentityManager()
try:
    manager.switch_to_agent('$AGENT_NAME')
    print('✅ 切换成功')
except Exception as e:
    print(f'❌ 切换失败: {e}')
    exit(1)
"
EOF

chmod +x agents/switch_agent.sh

# 创建agent提交脚本
cat > agents/commit_as_agent.sh << 'EOF'
#!/bin/bash
# 以指定agent身份提交

if [[ $# -lt 2 ]]; then
    echo "用法: ./commit_as_agent.sh <agent名称> \"提交信息\" [文件...]"
    exit 1
fi

AGENT_NAME=$1
MESSAGE=$2
shift 2
FILES=$@

echo "📝 Agent $AGENT_NAME 正在提交..."
python3 -c "
import sys
sys.path.append('agents')
from agent_identity_manager import AgentIdentityManager
manager = AgentIdentityManager()
try:
    manager.commit_as_agent('$AGENT_NAME', '$MESSAGE', '$FILES'.split() if '$FILES' else None)
    print('✅ 提交成功')
except Exception as e:
    print(f'❌ 提交失败: {e}')
    exit(1)
"
EOF

chmod +x agents/commit_as_agent.sh

# 创建统计脚本
cat > agents/stats.sh << 'EOF'
#!/bin/bash
# 查看agent统计信息

echo "📊 Agent协作统计"
echo "=================="

python3 -c "
import sys
sys.path.append('agents')
from agent_identity_manager import AgentIdentityManager
manager = AgentIdentityManager()

for agent in manager.list_agents():
    name = agent['name']
    stats = manager.get_agent_stats(name)
    print(f'👤 {name} ({agent["role"]})')
    print(f'   📧 {agent["email"]}')
    print(f'   📈 提交数: {stats["total_commits"]}')
    if stats["commits"]:
        print(f'   📝 最近提交: {stats["commits"][0]}')
    print()
"
EOF

chmod +x agents/stats.sh

echo "🎉 设置完成！"
echo ""
echo "📋 使用说明:"
echo "1. 查看agent列表: ./agents/stats.sh"
echo "2. 切换agent: ./agents/switch_agent.sh <agent名称>"
echo "3. agent提交: ./agents/commit_as_agent.sh <agent名称> \"消息\""
echo "4. 查看统计: ./agents/stats.sh"
echo ""
echo "🔑 SSH公钥位置:"
for key in agents/keys/*_rsa.pub; do
    if [[ -f "$key" ]]; then
        agent_name=$(basename "$key" _rsa.pub)
        echo "  $agent_name: $key"
    fi
done

echo ""
echo "💡 下一步:"
echo "1. 将SSH公钥添加到GitHub/Gitea/Bitbucket"
echo "2. 测试agent切换和提交功能"
echo "3. 开始真正的多agent协作开发！"