# ---- Build stage ----
FROM node:18-alpine AS builder
WORKDIR /app

# Copy dependency files
COPY package*.json ./

# Install dependencies
RUN npm install --legacy-peer-deps

# Copy source code
COPY . .

# Build Next.js app
RUN npm run build


# ---- Run stage ----
FROM node:18-alpine AS runner
ENV NODE_ENV=production
WORKDIR /app

# Copy build output and required files
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/next.config.* ./
COPY --from=builder /app/postcss.config.* ./
COPY --from=builder /app/tsconfig.json ./

# Install only production deps
RUN npm install --omit=dev --legacy-peer-deps

# Expose Next.js default port
EXPOSE 3000

# Start app
CMD ["npm", "start"]
