#!/bin/bash
# ================================================
# Urban Lifeline - 前端镜像构建脚本
# 
# 使用方式:
#   ./build.sh           # 构建 All-in-One 镜像
# ================================================

set -e

# 项目根目录 (相对于此脚本)
PROJECT_ROOT="$(cd "$(dirname "$0")/../../.." && pwd)"
cd "$PROJECT_ROOT"

# 镜像版本
VERSION=${IMAGE_VERSION:-latest}

# 前端站点列表
SITES=(shared platform workcase bidding workcase_wechat)

# 构建 All-in-One 镜像
build() {
    echo "=========================================="
    echo "构建 All-in-One 前端镜像: urban-lifeline-web:${VERSION}"
    echo "=========================================="
    
    # 检查所有 dist 目录
    local missing=0
    for site in "${SITES[@]}"; do
        if [ ! -d "urbanLifelineWeb/packages/${site}/dist" ]; then
            echo "缺少: urbanLifelineWeb/packages/${site}/dist"
            missing=1
        fi
    done
    
    if [ $missing -eq 1 ]; then
        echo ""
        echo "请先执行前端构建:"
        echo "  cd urbanLifelineWeb && pnpm build"
        exit 1
    fi
    
    docker build -t urban-lifeline-web:${VERSION} \
        -f docker/urbanLifeline/web/Dockerfile.web .
    
    if [ "$VERSION" != "latest" ]; then
        docker tag urban-lifeline-web:${VERSION} urban-lifeline-web:latest
    fi
    
    echo "=========================================="
    echo "镜像构建完成!"
    echo ""
    echo "管理服务:"
    echo "  docker exec urban-lifeline-web /app/web-manager.sh status"
    echo "  docker exec urban-lifeline-web /app/web-manager.sh restart platform"
    echo "=========================================="
}

build
