#!/bin/bash
set -e

FRONTEND_DIR="frontend"
BACKEND_DIR="backend"
STATIC_DIR="${BACKEND_DIR}/src/main/resources/static"

echo "========================================"
echo "  前后端合并构建部署工具"
echo "========================================"
echo

build() {
    echo "---- [1/3] 前端打包（直接输出到后端 static 目录）----"
    cd "${FRONTEND_DIR}"
    npm run build
    cd ..

    echo "---- [2/3] 后端打包 ----"
    cd "${BACKEND_DIR}"
    mvn package -DskipTests -q
    cd ..

    echo "---- [3/3] 构建完成 ----"
}

case "${1}" in
    build)
        echo "[模式] 仅构建（不启动服务）"
        build
        echo
        echo "[完成] 构建成功！使用以下命令运行："
        echo "  java -jar ${BACKEND_DIR}/target/*.jar"
        ;;
    *)
        echo "[模式] 构建 + 启动"
        build
        echo
        echo "[启动] 正在启动后端服务..."
        cd "${BACKEND_DIR}"
        mvn spring-boot:run -q
        ;;
esac
