#!/bin/bash

# AI助手Web客户端启动脚本

echo "========================================="
echo "  AI助手Web客户端启动脚本"
echo "========================================="
echo ""

# 检查Python环境
if ! command -v python3 &> /dev/null; then
    echo "❌ 错误: 未找到Python3，请先安装Python 3.7+"
    exit 1
fi

echo "✅ Python版本: $(python3 --version)"
echo ""

# 检查是否在正确的目录
if [ ! -f "app.py" ]; then
    echo "❌ 错误: 请在web_client目录下运行此脚本"
    exit 1
fi

# 检查依赖是否安装
echo "📦 检查依赖..."
if ! python3 -c "import flask" 2>/dev/null; then
    echo "⚠️  Flask未安装，正在安装依赖..."
    pip3 install -r requirements.txt
    if [ $? -ne 0 ]; then
        echo "❌ 依赖安装失败"
        exit 1
    fi
    echo "✅ 依赖安装成功"
else
    echo "✅ 依赖已安装"
fi

echo ""
echo "========================================="
echo "  启动Web服务器..."
echo "========================================="
echo ""

# 读取配置
PORT=${PORT:-15000}
API_BASE_URL=${API_BASE_URL:-http://localhost:8080}

echo "🌐 访问地址: http://localhost:$PORT"
echo "🔗 后端API: $API_BASE_URL"
echo "📝 按 Ctrl+C 停止服务"
echo ""

# 启动Flask应用
export PORT=$PORT
export API_BASE_URL=$API_BASE_URL
python3 app.py
