# ---------- Base for installs ----------
FROM node:22-alpine AS base
WORKDIR /app

# ---------- Production deps ----------
FROM base AS deps
COPY package*.json ./
# nur Prod-Deps für die runner-Stage (schnellerer Start, kleineres Image)
RUN npm ci --only=production

# ---------- Full deps + Build ----------
FROM base AS builder
COPY package*.json ./
# volle Deps inkl. devDependencies
RUN npm ci
# Rest des Codes
COPY . .
RUN npm run build

# ---------- Development stage ----------
FROM base AS dev
ENV NODE_ENV=development
# nur package-Dateien kopieren, damit npm install gecacht wird
COPY package*.json ./
RUN npm install
# Quellcode kommt zur Laufzeit per Bind-Mount aus Compose
# (sorgt für Hot Reload)
EXPOSE 3000
ENV HOSTNAME="0.0.0.0"
CMD ["npm", "run", "dev"]

# ---------- Production runner ----------
FROM node:22-alpine AS runner
WORKDIR /app

ENV NODE_ENV=production
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"

# non-root user
RUN addgroup --system --gid 1001 nodejs \
  && adduser --system --uid 1001 nextjs

# für Healthcheck
RUN apk add --no-cache curl

# statische Assets & Standalone-Output aus builder
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs
EXPOSE 3000

# Health check (pfad anpassen, falls dein Endpoint anders heißt)
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD curl -fsS http://127.0.0.1:3000/api/health || exit 1

# Next.js standalone startet über server.js
CMD ["node", "server.js"]

