########################## Maven 构建 ##########################
FROM maven:3.9.10-eclipse-temurin-8-noble AS aegis-builder

# 设置工作目录
WORKDIR /app

# 复制 Maven 配置文件
COPY ./deployment/maven/settings.xml /root/.m2/settings.xml

# 复制项目文件
COPY ./pom.xml pom.xml
COPY ./aegis-framework aegis-framework/
COPY ./aegis-common aegis-common/
COPY ./aegis-system aegis-system/
COPY ./aegis-quartz aegis-quartz/
COPY ./aegis-admin aegis-admin/
COPY ./aegis-emergency aegis-emergency/
COPY ./aegis-notification aegis-notification/
COPY ./aegis-file aegis-file/
COPY ./aegis-resource aegis-resource/
COPY ./aegis-knowledge aegis-knowledge/
COPY ./aegis-case aegis-case/
COPY ./aegis-camera aegis-camera/

# 构建项目
RUN mvn clean package -DskipTests

######################### 应急指挥服务镜像 #########################
FROM eclipse-temurin:8-jre-noble AS aegis-admin

# 应用启动参数
ENV JVM_XMS=512
ENV JVM_XMX=1024
ENV SERVER_PORT=8080
ENV SPRING_PROFILE=druid

# 设置工作目录
WORKDIR /app

# 复制构建好的 JAR 文件
COPY --from=aegis-builder /app/aegis-admin/target/aegis-admin.jar /app/aegis-admin.jar

# 启动应用
CMD java \
        -Xms${JVM_XMS}m \
        -Xmx${JVM_XMX}m \
        -Duser.timezone=GMT+8 \
        -Dserver.port=${SERVER_PORT} \
        -Dspring.profiles.active=${SPRING_PROFILE} \
        -jar /app/aegis-admin.jar

######################### 应急指挥前端构建 #########################
FROM node:14.19.1-bullseye AS aegis-ui-builder

# 设置镜像源
RUN npm config set registry https://registry.npmmirror.com/

# 安装构建依赖 (Python + g++ + make)
RUN apt-get update && apt-get install -y \
    python3 \
    make \
    g++ \
 && rm -rf /var/lib/apt/lists/*

# 设置工作目录
WORKDIR /app

# 复制依赖文件
COPY ./aegis-ui/package.json ./package.json

# 安装依赖
RUN npm install

# 复制项目文件
COPY ./aegis-ui .

# 构建前端
RUN npm run build:prod

######################### 应急指挥前端镜像 #########################
FROM nginx:1.29.0-alpine3.22 AS aegis-ui

# 复制前端文件
COPY --from=aegis-ui-builder /app/dist /usr/share/nginx/html

# 复制自定义 Nginx 配置
COPY ./deployment/nginx/ui.conf /etc/nginx/conf.d/default.conf

# 启动nginx并保持前台运行
CMD ["nginx", "-g", "daemon off;"]