# ── Stage 1: Next.js Build ────────────────────────────────────────────────────
FROM node:22-bookworm-slim AS node-builder

WORKDIR /build
COPY package.json package-lock.json ./
RUN npm ci

COPY . .
RUN npm run build

# ── Stage 2: Runtime (Next.js standalone) ─────────────────────────────────────
FROM node:22-bookworm-slim

WORKDIR /app

RUN useradd --system --shell /bin/false --uid 998 --create-home nextjs

COPY --from=node-builder /build/.next/standalone ./
COPY --from=node-builder /build/.next/static ./.next/static
COPY --from=node-builder /build/public ./public

RUN chown -R nextjs:nextjs /app

USER nextjs
EXPOSE 3000

ENV PORT=3000
ENV HOSTNAME=0.0.0.0
# NEXT_PUBLIC_API_URL wird zur Laufzeit gesetzt (docker-compose environment)

CMD ["node", "server.js"]
