# Use official Node.js v22.15.0 image
FROM node:22.15.0-alpine AS builder

# Set working directory
WORKDIR /app

# Install dependencies only when needed
COPY package.json package-lock.json ./
RUN npm ci --prefer-offline --no-audit

# Copy the rest of the application code
COPY . .

# Pass build-time environment variables
ARG BACKEND_URL
ENV BACKEND_URL=${BACKEND_URL}

# Build the Next.js app
RUN npm run build

# Production image, copy built assets from builder
FROM node:22.15.0-alpine AS runner
WORKDIR /app

ENV NODE_ENV=production

# Copy only necessary files for running the app
COPY --from=builder /app/.next .next
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/node_modules ./node_modules

# Runtime environment variables
ENV BACKEND_URL=${BACKEND_URL}

EXPOSE 3000

CMD ["npm", "start"]