#!/bin/bash
# ============================================
# 本地构建脚本 - 构建 Flutter + Vue Admin
# ============================================
set -e

# ==================== 工具函数 ====================
log() {
    echo "[$(date '+%H:%M:%S')] $1"
}

PROJECT_ROOT="$(dirname "$0")/.."

# ==================== 1. 构建 Flutter Web ====================
build_flutter() {
    log "🚀 [Flutter] 开始构建..."
    cd $PROJECT_ROOT/flutter_monisuo

    log "1.1 获取依赖..."
    flutter pub get

    log "1.2 构建 Web..."
    flutter build web --release

    if [ ! -d "build/web" ]; then
        log "❌ Flutter 构建失败"
        exit 1
    fi

    log "✅ Flutter 构建完成: flutter_monisuo/build/web/"
}

# ==================== 2. 构建 Vue Admin ====================
build_admin() {
    log "🚀 [Vue Admin] 开始构建..."
    cd $PROJECT_ROOT/monisuo-admin

    log "2.1 安装依赖..."
    pnpm install

    log "2.2 构建..."
    pnpm build

    if [ ! -d "dist" ]; then
        log "❌ Vue Admin 构建失败"
        exit 1
    fi

    log "✅ Vue Admin 构建完成: monisuo-admin/dist/"
}

# ==================== 主流程 ====================
case "$1" in
    flutter)
        build_flutter
        ;;
    admin)
        build_admin
        ;;
    *)
        build_flutter
        build_admin
        ;;
esac

log "🏁 构建完成！"
