# syntax=docker/dockerfile:1
#
# Next.js 15 (standalone) + Prisma server image for the IT Cost Dashboard.
#
# Debian slim, NOT Alpine: better-sqlite3 (a devDependency native addon) ships
# glibc prebuilds and would otherwise compile from source, and Prisma's query
# engine needs the musl-specific binaryTarget on Alpine. ~40 MB more for two
# fewer traps.

# ---------------------------------------------------------------------------
# Stage 1: deps — install node_modules once, including native addons.
# ---------------------------------------------------------------------------
FROM node:20-bookworm-slim AS deps
WORKDIR /app

# python3/make/g++ are the better-sqlite3 source-build fallback; openssl is what
# the Prisma postinstall links its query engine against. All discarded with this
# stage.
RUN apt-get update && apt-get install -y --no-install-recommends \
      openssl ca-certificates python3 make g++ \
    && rm -rf /var/lib/apt/lists/*

COPY package.json package-lock.json ./
# No --ignore-scripts: Prisma's postinstall is what unpacks the query engine.
RUN npm ci

# ---------------------------------------------------------------------------
# Stage 2: builder — prisma generate (prod/mysql schema) + next build.
# ---------------------------------------------------------------------------
FROM node:20-bookworm-slim AS builder
WORKDIR /app

RUN apt-get update && apt-get install -y --no-install-recommends \
      openssl ca-certificates \
    && rm -rf /var/lib/apt/lists/*

COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Auth.js v5 constructs NextAuth() at module init (src/auth.ts), which can throw
# without a secret while pre-rendering. This placeholder is build-time only —
# the real AUTH_SECRET arrives at runtime from the k8s Secret.
ARG AUTH_SECRET=build-placeholder-not-used-at-runtime
ENV AUTH_SECRET=${AUTH_SECRET}
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production

# No DATABASE_URL needed: every page is "use client" and fetches /api/* at
# runtime, and `await auth()` in the layout returns null harmlessly.
# build:prod == prisma generate --schema=prisma/prod/schema.prisma && next build
RUN npm run build:prod

# ---------------------------------------------------------------------------
# Stage 3: runner — standalone output only, non-root.
# ---------------------------------------------------------------------------
FROM node:20-bookworm-slim AS runner
WORKDIR /app

# openssl is required at runtime: the Prisma query engine links against it.
RUN apt-get update && apt-get install -y --no-install-recommends \
      openssl ca-certificates \
    && rm -rf /var/lib/apt/lists/*

RUN groupadd --system --gid 1001 nodejs \
    && useradd --system --uid 1001 --gid nodejs nextjs

# .next/standalone ships its own traced node_modules — do NOT copy the
# builder's. .next/static is NOT part of standalone and must come separately or
# every CSS/JS chunk 404s. config/ is read at runtime by lookupRole()
# (src/lib/authz.ts) via process.cwd() once SSO is re-enabled.
# NOTE: this repo has no public/ directory; adding a COPY for it would fail.
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nodejs /app/config ./config

USER nextjs

ENV HOSTNAME=0.0.0.0
ENV PORT=3000
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

EXPOSE 3000

# Standalone replaces `next start`.
CMD ["node", "server.js"]
