#!/bin/bash

# 山寨币高盈亏比狙击策略 - 一键应用脚本
# 使用方法: bash apply_altcoin_strategy.sh

echo "=================================="
echo "山寨币高盈亏比狙击策略 - 一键应用"
echo "=================================="
echo ""

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 检查是否在正确的目录
if [ ! -f "trading_system/config.py" ]; then
    echo -e "${RED}❌ 错误：请在项目根目录运行此脚本${NC}"
    exit 1
fi

echo "第1步：重新构建前端..."
echo "----------------------------------------"
cd frontend
if [ ! -d "node_modules" ]; then
    echo -e "${YELLOW}⚠️  node_modules不存在，跳过前端构建${NC}"
    echo "    如需更新前端界面，请手动执行: cd frontend && npm run build"
else
    npm run build
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}✅ 前端构建成功${NC}"
    else
        echo -e "${RED}❌ 前端构建失败${NC}"
        exit 1
    fi
fi
cd ..
echo ""

echo "第2步：重启所有交易进程..."
echo "----------------------------------------"
supervisorctl restart auto_sys:*
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✅ 交易进程重启成功${NC}"
else
    echo -e "${YELLOW}⚠️  交易进程重启失败，请手动执行: supervisorctl restart auto_sys:*${NC}"
fi
echo ""

echo "第3步：重启推荐服务..."
echo "----------------------------------------"
supervisorctl restart auto_recommend:*
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✅ 推荐服务重启成功${NC}"
else
    echo -e "${YELLOW}⚠️  推荐服务重启失败，请手动执行: supervisorctl restart auto_recommend:*${NC}"
fi
echo ""

echo "第4步：查看进程状态..."
echo "----------------------------------------"
supervisorctl status | grep -E "auto_sys|auto_recommend"
echo ""

echo "第5步：验证配置..."
echo "----------------------------------------"
echo "查看最近日志，确认关键配置："
tail -n 50 logs/trading_*.log 2>/dev/null | grep -E "ATR_STOP_LOSS_MULTIPLIER|RISK_REWARD_RATIO|MIN_HOLD_TIME_SEC|USE_TRAILING_STOP" | tail -5

if [ $? -eq 0 ]; then
    echo ""
    echo -e "${GREEN}✅ 配置验证完成${NC}"
else
    echo -e "${YELLOW}⚠️  日志文件不存在或未找到配置，请稍后查看${NC}"
fi

echo ""
echo "=================================="
echo -e "${GREEN}✅ 山寨币策略应用完成！${NC}"
echo "=================================="
echo ""
echo "📊 预期效果："
echo "   • 胜率目标: 35%"
echo "   • 盈亏比: 4.0:1"
echo "   • 期望值: +0.75%/笔"
echo ""
echo "⚠️  重要提醒："
echo "   1. 前3笔交易必须人工监控"
echo "   2. 确认止损距离≈15%，盈亏比≈4:1"
echo "   3. 单日亏损>5%立即暂停"
echo "   4. 只做24H成交量≥3000万美元的币种"
echo ""
echo "📝 详细说明请查看："
echo "   • 山寨币策略快速应用完整指南.md"
echo "   • ALTCOIN_STRATEGY_UPDATE.md"
echo ""
echo "🔍 持续监控："
echo "   tail -f logs/trading_*.log"
echo ""
