#!/bin/bash

# Soul派对小程序 - 快速启动脚本
# 用于启动后端API服务器

echo "=================================="
echo "   Soul派对·创业实验 启动脚本    "
echo "=================================="
echo ""

# 检查Node.js
if ! command -v node &> /dev/null; then
    echo "❌ 错误: 未检测到Node.js，请先安装Node.js"
    exit 1
fi

echo "✅ Node.js版本: $(node -v)"

# 检查pnpm
if ! command -v pnpm &> /dev/null; then
    echo "⚠️  警告: 未检测到pnpm，尝试使用npm..."
    PACKAGE_MANAGER="npm"
else
    echo "✅ pnpm版本: $(pnpm -v)"
    PACKAGE_MANAGER="pnpm"
fi

echo ""
echo "1️⃣  检查依赖..."

# 检查是否已安装依赖
if [ ! -d "node_modules" ]; then
    echo "📦 正在安装依赖..."
    $PACKAGE_MANAGER install
    if [ $? -ne 0 ]; then
        echo "❌ 依赖安装失败"
        exit 1
    fi
else
    echo "✅ 依赖已安装"
fi

echo ""
echo "2️⃣  启动后端API服务器..."
echo ""
echo "🚀 服务器将运行在: http://localhost:3000"
echo "📡 API接口地址: http://localhost:3000/api"
echo ""
echo "📱 小程序配置步骤:"
echo "   1. 打开微信开发者工具"
echo "   2. 导入项目，选择 miniprogram/ 目录"
echo "   3. 修改 miniprogram/app.js 中的 apiBase 为: http://localhost:3000/api"
echo "   4. 点击编译运行"
echo ""
echo "🔧 后台管理地址: http://localhost:3000/admin"
echo "   默认账号: admin / admin123"
echo ""
echo "=================================="
echo "按 Ctrl+C 停止服务器"
echo "=================================="
echo ""

# 启动开发服务器
$PACKAGE_MANAGER run dev
