# Build stage
FROM node:14 AS builder

# Set npm registry for faster downloads in China
ENV NPM_CONFIG_REGISTRY=http://mirrors.cloud.tencent.com/npm

# Set working directory
WORKDIR /app

# Install dependencies
COPY package.json .
RUN npm install

# Copy source code
COPY frontend/public ./frontend/public
COPY frontend/admin ./frontend/admin
COPY app ./app

# Build frontend
RUN npx vite --config /app/frontend/admin/vite.config.js build

# Production stage
FROM nginx:latest AS production

# Copy built assets from builder stage
COPY --from=builder /app/frontend/admin/dist /usr/share/nginx/html

# Copy nginx configuration
COPY frontend/admin/nginx.conf /etc/nginx/conf.d/default.conf

# Expose HTTP port
EXPOSE 80

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