#!/bin/bash
# 开发环境启动脚本

echo "==================================="
echo "考培练系统后端 - 开发环境启动"
echo "==================================="

# 检查是否在项目根目录
if [ ! -f "app/main.py" ]; then
    echo "错误：请在项目根目录运行此脚本"
    exit 1
fi

# 检查Python版本
python_version=$(python3 --version 2>&1 | awk '{print $2}')
echo "Python版本: $python_version"

# 检查MySQL服务
echo -n "检查MySQL服务... "
if command -v mysql &> /dev/null; then
    if mysql -u root -e "SELECT 1" &> /dev/null; then
        echo "✓"
    else
        echo "✗"
        echo "警告：无法连接到MySQL，请确保MySQL服务正在运行"
        echo "提示：使用 'sudo service mysql start' 启动MySQL"
    fi
else
    echo "✗"
    echo "警告：未找到MySQL客户端"
fi

# 检查Redis服务
echo -n "检查Redis服务... "
if command -v redis-cli &> /dev/null; then
    if redis-cli ping &> /dev/null; then
        echo "✓"
    else
        echo "✗"
        echo "警告：无法连接到Redis，请确保Redis服务正在运行"
        echo "提示：使用 'sudo service redis-server start' 启动Redis"
    fi
else
    echo "✗"
    echo "警告：未找到Redis客户端"
fi

# 安装依赖
echo ""
echo "安装/更新依赖..."
pip install --break-system-packages -r requirements/base.txt

# 初始化数据库
echo ""
echo "初始化数据库..."
python3 scripts/init_db.py

# 创建测试数据
echo ""
echo "创建测试数据..."
python3 scripts/create_test_data.py

# 启动服务器
echo ""
echo "==================================="
echo "启动开发服务器..."
echo "API文档地址: http://localhost:8000/docs"
echo "==================================="
echo ""

uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
