# Build Stage
FROM node:20-alpine AS build

WORKDIR /app

# Copy package files and install dependencies
COPY package*.json ./
RUN npm ci

# Copy the rest of the app
COPY . .

# Build Next.js app (standalone mode)
# All environment variables are runtime-configurable via .env or docker-compose
RUN npm run build

# Production Stage - Next.js standalone server
FROM node:20-alpine

WORKDIR /app

# Copy standalone server files from build stage
COPY --from=build /app/.next/standalone ./
COPY --from=build /app/.next/static ./.next/static
COPY --from=build /app/public ./public

# Expose port 3000
EXPOSE 3000

# Set environment to production
ENV NODE_ENV=production
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"

# Start Next.js server
# Runtime environment variables (NEXT_PUBLIC_*) can be passed via docker-compose or -e flags
CMD ["node", "server.js"]
