# 简化的Dockerfile，专门解决构建问题
FROM node:18-alpine AS builder

WORKDIR /app

# 接收构建参数
ARG VITE_API_BASE_URL=https://aiedu.ireborn.com.cn/api
ARG VITE_WS_BASE_URL=wss://aiedu.ireborn.com.cn/ws
ARG VITE_USE_MOCK_DATA=false

# 设置环境变量
ENV VITE_API_BASE_URL=$VITE_API_BASE_URL
ENV VITE_WS_BASE_URL=$VITE_WS_BASE_URL
ENV VITE_USE_MOCK_DATA=$VITE_USE_MOCK_DATA

# 复制package文件
COPY package*.json ./

# 使用阿里云镜像源
RUN npm config set registry https://registry.npmmirror.com

# 安装依赖（跳过脚本）
RUN npm install --ignore-scripts

# 复制源代码
COPY . .

# 构建应用
RUN ./node_modules/.bin/vite build

# 生产阶段
FROM nginx:alpine

# 安装curl用于健康检查
RUN apk add --no-cache curl

# 复制nginx配置
COPY docker/nginx.conf /etc/nginx/nginx.conf
COPY docker/default.conf /etc/nginx/conf.d/default.conf

# 复制构建产物
COPY --from=builder /app/dist /usr/share/nginx/html

# 暴露端口
EXPOSE 80

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

# 启动nginx
CMD ["nginx", "-g", "daemon off;"]
