# =============================================================================
# Multi-stage Dockerfile for Next.js Frontend
# =============================================================================
# Usage:
#   Development: docker compose up (mounts source, hot reload)
#   Production:  docker compose -f docker-compose.yml -f docker-compose.prod.yml up

# =============================================================================
# Stage 1: Dependencies (cached layer)
# =============================================================================
FROM node:18-alpine AS deps

WORKDIR /app

# Copy only package files for dependency installation
COPY package*.json ./

# Install ALL dependencies (including devDependencies for dev mode)
RUN npm ci

# =============================================================================
# Stage 2: Development (hot reload, source mounted)
# =============================================================================
FROM node:18-alpine AS development

WORKDIR /app

# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules

# Copy package files (for npm scripts)
COPY package*.json ./

# Copy config files
COPY next.config.js postcss.config.js tailwind.config.js tsconfig.json ./

# Note: src/ and public/ are mounted at runtime for hot reload
# Start dev server
EXPOSE 3000
CMD ["npm", "run", "dev"]

# =============================================================================
# Stage 3: Builder (compile for production)
# =============================================================================
FROM node:18-alpine AS builder

WORKDIR /app

# Copy dependencies
COPY --from=deps /app/node_modules ./node_modules

# Copy all source files
COPY . .

# Build for production
RUN npm run build

# =============================================================================
# Stage 4: Production (optimized, minimal)
# =============================================================================
FROM node:18-alpine AS production

WORKDIR /app

ENV NODE_ENV=production

# Copy only production dependencies
COPY package*.json ./
RUN npm ci --only=production && npm cache clean --force

# Copy built application from builder
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public
COPY --from=builder /app/next.config.js ./

EXPOSE 3000

USER node

CMD ["npm", "run", "start"]
