#!/bin/bash

# 宇之然内容管理平台 - 统一启动脚本
# 启动 Web 管理界面

set -e

echo "========================================"
echo "宇之然内容管理平台"
echo "========================================"

PLATFORM_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/platform" && pwd)"

if [ ! -d "$PLATFORM_DIR" ]; then
    echo "❌ 错误: platform 目录不存在: $PLATFORM_DIR"
    exit 1
fi

echo "项目目录: $(dirname "$PLATFORM_DIR")"
echo "平台目录: $PLATFORM_DIR"
echo "========================================"
echo ""

cd "$PLATFORM_DIR"

# 检查虚拟环境
VENV_PYTHON="$PLATFORM_DIR/backend/venv/bin/python"
if [ -f "$VENV_PYTHON" ]; then
    PYTHON_CMD="$VENV_PYTHON"
else
    PYTHON_CMD="python3"
fi

# 检查依赖
echo "检查依赖..."
$PYTHON_CMD -m pip install -q -r backend/requirements.txt

# 启动服务
PORT=${1:-8001}
echo ""
echo "🚀 启动服务..."
echo "端口: $PORT"
echo "界面: http://localhost:$PORT/"
echo "API: http://localhost:$PORT/docs"
echo "========================================"
echo ""

cd backend
exec $PYTHON_CMD -m uvicorn app.main:app --host 0.0.0.0 --port $PORT 
