#!/bin/bash
# Flask API 服务启动脚本
# =====================

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

echo -e "${GREEN}Universal Data Fetcher API 服务启动脚本${NC}"
echo "=========================================="

# 检查 Python
echo -e "\n1. 检查 Python 环境..."
if ! command -v python &> /dev/null; then
    echo -e "${RED}✗ Python 未安装${NC}"
    exit 1
fi
echo -e "${GREEN}✓ Python 已安装: $(python --version)${NC}"

# 检查依赖
echo -e "\n2. 检查依赖..."
python -c "import flask" 2>/dev/null || {
    echo -e "${YELLOW}⚠ Flask 未安装，正在安装...${NC}"
    pip install flask flask-cors
}
echo -e "${GREEN}✓ 依赖检查完成${NC}"

# 检查环境变量
echo -e "\n3. 检查环境变量..."
if [ -z "$TUSHARE_TOKEN" ]; then
    if [ -f ".env" ]; then
        echo -e "${YELLOW}⚠ 从 .env 文件加载环境变量${NC}"
        export $(cat .env | grep -v '^#' | xargs)
    else
        echo -e "${RED}✗ TUSHARE_TOKEN 未设置${NC}"
        echo "   请在 .env 文件中设置 TUSHARE_TOKEN"
        exit 1
    fi
fi
echo -e "${GREEN}✓ 环境变量检查完成${NC}"

# 检查 SSH 配置
echo -e "\n4. 检查 SSH 配置..."
if [ -f "hk_ecs.pem" ]; then
    echo -e "${GREEN}✓ SSH 私钥文件存在 (hk_ecs.pem)${NC}"
    
    # 检查权限
    PERM=$(stat -f "%Lp" hk_ecs.pem 2>/dev/null || stat -c "%a" hk_ecs.pem 2>/dev/null)
    if [ "$PERM" != "600" ]; then
        echo -e "${YELLOW}⚠ 修复 SSH 私钥权限...${NC}"
        chmod 600 hk_ecs.pem
    fi
    echo -e "${GREEN}✓ SSH 私钥权限正确 (600)${NC}"
else
    echo -e "${YELLOW}⚠ SSH 私钥文件不存在，港美股数据获取将受限${NC}"
fi

# 解析参数
HOST="0.0.0.0"
PORT="5000"
DEBUG=""

while [[ $# -gt 0 ]]; do
    case $1 in
        --host)
            HOST="$2"
            shift 2
            ;;
        --port)
            PORT="$2"
            shift 2
            ;;
        --debug)
            DEBUG="--debug"
            shift
            ;;
        --with-ssh)
            export SSH_ENABLED=true
            export SSH_HOST=8.218.167.69
            export SSH_PORT=22
            export SSH_USERNAME=root
            export SSH_KEY_PATH=hk_ecs.pem
            export SSH_LOCAL_PORT=1080
            echo -e "${GREEN}✓ SSH 隧道已启用${NC}"
            shift
            ;;
        --help)
            echo ""
            echo "用法: ./start_flask_server.sh [选项]"
            echo ""
            echo "选项:"
            echo "  --host HOST     绑定主机 (默认: 0.0.0.0)"
            echo "  --port PORT     绑定端口 (默认: 5000)"
            echo "  --debug         启用调试模式"
            echo "  --with-ssh      启用 SSH 隧道"
            echo "  --help          显示帮助"
            echo ""
            echo "示例:"
            echo "  ./start_flask_server.sh"
            echo "  ./start_flask_server.sh --port 8080"
            echo "  ./start_flask_server.sh --with-ssh"
            echo "  ./start_flask_server.sh --host 127.0.0.1 --port 5000 --debug --with-ssh"
            exit 0
            ;;
        *)
            echo -e "${RED}未知选项: $1${NC}"
            echo "使用 --help 查看帮助"
            exit 1
            ;;
    esac
done

# 启动服务
echo -e "\n5. 启动 Flask 服务..."
echo -e "   主机: ${YELLOW}$HOST${NC}"
echo -e "   端口: ${YELLOW}$PORT${NC}"
echo -e "   调试: ${YELLOW}$([ -n "$DEBUG" ] && echo "是" || echo "否")${NC}"
echo -e "   SSH:  ${YELLOW}$([ "$SSH_ENABLED" = "true" ] && echo "启用" || echo "禁用")${NC}"
echo ""

echo -e "${GREEN}✓ 服务启动中...${NC}"
echo "=========================================="
echo ""

python core/datasource/flask_server.py --host "$HOST" --port "$PORT" $DEBUG
