# 多阶段构建 - 前端构建阶段
FROM m.daocloud.io/docker.io/library/node:lts-alpine AS frontend-builder

# 设置工作目录
WORKDIR /app

# 复制前端源码
COPY qaup-ui/ ./

# 安装依赖并构建
RUN npm install && npm run build:prod

# Nginx 运行阶段
FROM m.daocloud.io/docker.io/library/nginx:stable-alpine

# 安装必要工具
RUN apk add --no-cache curl

# 创建 nginx 用户目录
RUN mkdir -p /var/cache/nginx/client_temp && \
    chown -R nginx:nginx /var/cache/nginx

# 复制构建好的前端文件
COPY --from=frontend-builder /app/dist /usr/share/nginx/html

# 复制 Nginx 配置文件
COPY deploy/docker/nginx/nginx.conf /etc/nginx/nginx.conf
COPY deploy/docker/nginx/default.conf /etc/nginx/conf.d/default.conf
COPY deploy/docker/nginx/nginx-env.conf.template /etc/nginx/conf.d/nginx-env.conf.template
COPY deploy/docker/nginx/start-nginx.sh /start-nginx.sh

# 设置执行权限
RUN chmod +x /start-nginx.sh

# 暴露端口
EXPOSE 80

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
    CMD curl -f http://localhost/health || exit 1

# 启动 Nginx
CMD ["/start-nginx.sh"]