#!/bin/bash
# 管理会计OS — 一键启动开发环境
set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

echo "===== 管理会计OS 开发环境 ====="
echo ""

# 1. 启动后端
echo "[1/2] 启动后端 (systemd)..."
cd "$SCRIPT_DIR/backend"
if [ ! -f .env ]; then
  cp .env.example .env
  echo "    已创建 .env (默认配置)"
fi
systemctl start cma-backend 2>/dev/null || true
sleep 1
echo "    后端: http://127.0.0.1:8010"
echo "    文档: http://127.0.0.1:8010/docs"

# 2. 启动前端
echo "[2/2] 启动前端 (Vite HMR)..."
cd "$SCRIPT_DIR/frontend"
if [ ! -d "node_modules" ]; then
  echo "    安装前端依赖..."
  pnpm install --no-frozen-lockfile --silent
fi
echo "    前端: http://localhost:5173"
echo ""
echo "===== 启动完成 ====="
echo "    线上: https://cma.sxbh.ltd"
echo "    本地: http://localhost:5173"
echo "    后端: http://127.0.0.1:8010/docs"
echo ""
echo "按 Ctrl+C 停止前端开发服务器"

exec pnpm dev
