# Optimized Dockerfile for Next.js e2e testing
# Simplified approach to avoid multi-stage build issues

FROM node:20-alpine

# Accept build arguments
ARG NODE_ENV=production
ARG NEXT_PUBLIC_API_BASE_URL

# Install build dependencies required for SWC and sharp
RUN apk add --no-cache \
    python3 \
    python3-dev \
    py3-pip \
    py3-setuptools \
    make \
    g++ \
    git \
    curl \
    && ln -sf python3 /usr/bin/python

# Install sharp dependencies (if using image optimization)
RUN apk add --no-cache vips-dev

WORKDIR /app

# Copy package files
COPY package.json package-lock.json* ./
COPY apps/website/package.json apps/website/package-lock.json* ./apps/website/

# Install dependencies
ENV NPM_CONFIG_FUND=false \
    NPM_CONFIG_AUDIT=false \
    NPM_CONFIG_UPDATE_NOTIFIER=false \
    NPM_CONFIG_PREFER_OFFLINE=true

# Install dependencies (use install instead of ci to handle missing sharp)
RUN npm install --include-workspace-root --no-audit --fund=false

# Copy source code
COPY core ./core
COPY adapters ./adapters
COPY apps/website ./apps/website
COPY scripts ./scripts
COPY tsconfig.json tsconfig.base.json .eslintrc.json ./

# Set environment variables for build
ENV NODE_ENV=${NODE_ENV}
ENV NEXT_PUBLIC_API_BASE_URL=${NEXT_PUBLIC_API_BASE_URL}
ENV NEXT_TELEMETRY_DISABLED=1

# Build the website
WORKDIR /app/apps/website
RUN npm run build

# Expose port
EXPOSE 3000

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD curl -f http://localhost:3000/ || exit 1

# Start command (production mode)
CMD ["npm", "start"]