#!/bin/bash
# 三国量化任务平台 - 工作流同步脚本
# 按照workflow-rules.md进行目录整理和同步

echo "=== 🚀 三国量化任务平台 - 工作流同步 ===" 
echo "⏰ 当前时间: $(date '+%Y-%m-%d %H:%M:%S')"
echo "📖 当前目录: $(pwd)"
echo "📋 当前用户: $(whoami)"
echo ""

# 检查是否在正确目录
if [ ! -d "sanguo_quant_live" ]; then
    echo "❌ 错误：未找到sanguo_quant_live目录"
    echo "请进入正确的工作区目录"
    exit 1
fi

# 进入sanguo_quant_live目录
cd sanguo_quant_live || exit 1

echo "📂 当前Git状态检查："
git status
echo ""

echo "=== 🔄 第一步：拉取最新代码 ===" 
echo "正在从远程仓库拉取最新变更..."
git pull origin main

if [ $? -ne 0 ]; then
    echo "❌ 拉取失败，请检查网络连接"
    exit 1
fi

echo "✅ 拉取成功"
echo ""

echo "=== 📂 第二步：查看工作流规则 ==="
if [ -f "management/workflow-rules.md" ]; then
    echo "✅ 找到工作流规则文档"
    echo "📄 路径：management/workflow-rules.md"
    echo ""
    echo "🔍 关键内容摘要："
    grep "## 第一层目录结构" -A 25 management/workflow-rules.md | head -30
else
    echo "⚠️  警告：未找到工作流规则文档"
    echo "请确认workflow-rules.md文件存在"
fi
echo ""

echo "=== 🔍 第三步：识别将军角色 ==="
CURRENT_DIR=$(basename $(pwd))

# 识别将军角色
case "$CURRENT_DIR" in
    "sanguo_quant_live")
        echo "📍 在根目录 - 显示所有将军工作区"
        echo ""
        echo "各将军工作区状态："
        echo ""
        
        # 赵云
        if [ -d "zhaoyun-data" ]; then
            echo "✅ zhaoyun-data/ (赵云-数据工程)"
            echo "   状态：$(ls -la zhaoyun-data/ | head -5)"
            echo "   目录数：$(find zhaoyun-data/ -maxdepth 1 -type d | wc -l | xargs)"
            echo ""
        else
            echo "⚠️  zhaoyun-data/ (赵云-数据工程) - 未找到"
            echo ""
        fi
        
        # 关羽
        if [ -d "guanyu-risk" ]; then
            echo "✅ guanyu-risk/ (关羽-风控管理)"
            echo "   状态：$(ls -la guanyu-risk/ | head -5)"
            echo "   目录数：$(find guanyu-risk/ -maxdepth 1 -type d | wc -l | xargs)"
            echo ""
        else
            echo "⚠️  guanyu-risk/ (关羽-风控管理) - 未找到"
            echo ""
        fi
        
        # 姜维
        if [ -d "jiangwei-platform" ]; then
            echo "✅ jiangwei-platform/ (姜维-平台基础设施)"
            echo "   状态：$(ls -la jiangwei-platform/ | head -5)"
            echo "   目录数：$(find jiangwei-platform/ -maxdepth 1 -type d | wc -l | xargs)"
            echo ""
        else
            echo "⚠️  jiangwei-platform/ (姜维-平台基础设施) - 未找到"
            echo ""
        fi
        
        # 张飞
        if [ -d "zhangfei-technical" ]; then
            echo "✅ zhangfei-technical/ (张飞-技术策略)"
            echo "   状态：$(ls -la zhangfei-technical/ | head -5)"
            echo "   目录数：$(find zhangfei-technical/ -maxdepth 1 -type d | wc -l | xargs)"
            echo ""
        else
            echo "⚠️  zhangfei-technical/ (张飞-技术策略) - 未找到"
            echo ""
        fi
        
        # 庞统
        if [ -d "pangtong-value" ]; then
            echo "✅ pangtong-value/ (庞统-价值投资)"
            echo "   状态：$(ls -la pangtong-value/ | head -5)"
            echo "   目录数：$(find pangtong-value/ -maxdepth 1 -type d | wc -l | xargs)"
            echo ""
        else
            echo "⚠️  pangtong-value/ (庞统-价值投资) - 未找到"
            echo ""
        fi
        
        # 司马懿
        if [ -d "simayi-quality" ]; then
            echo "✅ simayi-quality/ (司马懿-质量保证)"
            echo "   状态：$(ls -la simayi-quality/ | head -5)"
            echo "   目录数：$(find simayi-quality/ -maxdepth 1 -type d | wc -l | xargs)"
            echo ""
        else
            echo "⚠️  simayi-quality/ (司马懿-质量保证) - 未找到"
            echo ""
        fi
        
        # 管理目录
        echo "📋 管理目录："
        echo "   ✅ archive/ (归档目录)"
        echo "   ✅ management/ (项目管理)"
        echo "   ✅ strategies/ (最终成果物)"
        echo ""
        ;;
        
    "zhaoyun-data")
        echo "👤 赵云将军 - 数据工程工作区"
        echo "📋 你的职责：数据获取、清洗验证、质量检查"
        echo ""
        echo "🔍 标准目录结构："
        echo "   research/     # 调研报告目录"
        echo "   scripts/      # 数据处理脚本"
        echo "   data/         # 数据文件"
        echo "   reports/      # 报告文档"
        echo "   references/   # 参考资料"
        ;;
        
    "guanyu-risk")
        echo "👤 关羽将军 - 风控管理工作区"
        echo "📋 你的职责：风控模块开发、风险控制、安全防护"
        echo ""
        echo "🔍 标准目录结构："
        echo "   research/     # 风险研究目录"
        echo "   scripts/      # 风控脚本"
        echo "   reports/      # 风险报告"
        echo "   references/   # 参考资料"
        ;;
        
    "jiangwei-platform")
        echo "👤 姜维将军 - 平台基础设施工作区"
        echo "📋 你的职责：基础设施选型、环境搭建、平台运维"
        echo ""
        echo "🔍 标准目录结构："
        echo "   research/     # 平台研究目录"
        echo "   scripts/      # 部署脚本"
        echo "   reports/      # 部署报告"
        echo "   references/   # 参考资料"
        ;;
        
    "zhangfei-technical")
        echo "👤 张飞将军 - 技术策略工作区"
        echo "📋 你的职责：vnpy框架改造、多风格兼容、回测引擎"
        echo ""
        echo "🔍 标准目录结构："
        echo "   research/     # 技术策略研究"
        echo "   scripts/      # 策略脚本"
        echo "   reports/      # 回测报告"
        echo "   references/   # 参考资料"
        ;;
        
    "pangtong-value")
        echo "👤 庞统将军 - 价值投资工作区"
        echo "📋 你的职责：价值投资策略、策略设计、代码整合"
        echo ""
        echo "🔍 标准目录结构："
        echo "   research/     # 价值投资研究"
        echo "   scripts/      # 策略脚本"
        echo "   reports/      # 策略报告"
        echo "   references/   # 参考资料"
        ;;
        
    "simayi-quality")
        echo "👤 司马懿将军 - 质量保证工作区"
        echo "📋 你的职责：代码审计、质量复核、最终验收"
        echo ""
        echo "🔍 标准目录结构："
        echo "   research/     # 质量标准研究"
        echo "   scripts/      # 审计脚本"
        echo "   reports/      # 审计报告"
        echo "   references/   # 参考资料"
        ;;
        
    *)
        echo "⚠️ 未知目录：$CURRENT_DIR"
        echo "请在正确的将军工作区或根目录中执行此脚本"
        exit 1
        ;;
esac

echo ""
echo "=== 📊 第四步：检查标准目录结构 ==="

# 根据所在目录创建标准结构
case "$CURRENT_DIR" in
    "zhaoyun-data"|"guanyu-risk"|"jiangwei-platform"|"zhangfei-technical"|"pangtong-value"|"simayi-quality")
        echo "🔍 创建标准子目录..."
        mkdir -p research scripts reports references
        
        # 如果是赵云，创建data目录
        if [ "$CURRENT_DIR" = "zhaoyun-data" ]; then
            mkdir -p data
            echo "   ✅ 创建 data/ 目录"
        fi
        
        echo "   ✅ 创建 research/ scripts/ reports/ references/ 目录"
        ;;
esac

echo ""
echo "=== 🔍 第五步：检查变更状态 ===" 
git status --porcelain | head -10

echo ""
echo "=== 📤 第六步：提交并推送 ===" 

# 检查是否有变更
if [ -n "$(git status --porcelain)" ]; then
    echo "发现变更，准备提交..."
    git add .
    
    # 获取将军名称作为提交信息
    GENERAL_NAME=$(echo "$CURRENT_DIR" | sed 's/-.*//' | sed 's/zhaoyun/赵云/' | sed 's/guanyu/关羽/' | sed 's/jiangwei/姜维/' | sed 's/zhangfei/张飞/' | sed 's/pangtong/庞统/' | sed 's/simayi/司马懿/')
    
    COMMIT_MSG="按工作流规则同步：${GENERAL_NAME}工作区更新"
    git commit -m "$COMMIT_MSG"
    
    if [ $? -ne 0 ]; then
        echo "❌ 提交失败"
        exit 1
    fi
    
    echo "✅ 提交成功"
    
    echo ""
    echo "正在推送到远程仓库..."
    git push origin main
    
    if [ $? -ne 0 ]; then
        echo "❌ 推送失败，请检查权限"
        exit 1
    fi
    
    echo "✅ 推送成功"
else
    echo "📭 无变更，跳过提交和推送"
fi

echo ""
echo "=== ✅ 工作流同步完成 ==="
echo "⏰ 完成时间: $(date '+%Y-%m-%d %H:%M:%S')"
echo "📋 仓库状态: 已与远程同步"
echo "📋 工作流规则: 请查看 management/workflow-rules.md"
echo ""
echo "🚀 下一步：按照工作流规则整理最新调研结果"