#!/bin/bash
set -e

echo "LiteMall 轻商城 - 一键安装脚本"
echo "=================================="

# 检查Node.js
if ! command -v node &> /dev/null; then
  echo "❌ 未检测到Node.js，请先安装 Node.js >= 16"
  echo "   推荐: https://nodejs.org 或 nvm"
  exit 1
fi

NODE_VER=$(node -v | cut -d'v' -f2 | cut -d'.' -f1)
echo "✓ Node.js $(node -v)"

# 检查npm
if ! command -v npm &> /dev/null; then
  echo "❌ 未检测到npm"
  exit 1
fi

echo ""
echo "📦 安装后端依赖..."
cd "$(dirname "$0")/server"
npm install --production 2>&1 | tail -1
echo "✓ 依赖安装完成"

echo ""
echo "🗄️  初始化数据库..."
node src/init-db.js
echo "✓ 数据库初始化完成"

echo ""
echo "📊 填充演示数据..."
node src/seed-demo.js 2>/dev/null && echo "✓ 演示数据填充完成" || echo "⚠️  跳过演示数据（不影响运行）"

echo ""
echo "🔧 创建上传目录..."
mkdir -p uploads

echo ""
echo "=================================="
echo "✅ 安装完成!"
echo ""
echo "启动命令:"
echo "  cd server && node src/app.js"
echo ""
echo "访问地址:"
echo "  管理后台: http://localhost:3001/admin/"
echo "  移动端:   http://localhost:3001/mobile/"
echo ""
echo "管理员账号:"
echo "  手机号: admin"
echo "  密码:   admin888"
echo "=================================="
