FROM node:22-alpine AS base
RUN corepack enable && corepack prepare pnpm@10.8.1 --activate

FROM base AS deps
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile

FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

ENV NEXT_TELEMETRY_DISABLED=1
# Hardcoded build-time placeholders — NextAuth v5 reads AUTH_URL at module init time.
# Real values are injected at runtime via docker-compose environment.
ENV AUTH_URL=http://localhost:3000
ENV NEXTAUTH_URL=http://localhost:3000
ENV AUTH_SECRET=build-time-placeholder-secret-minimum-32-characters
ENV NEXTAUTH_SECRET=build-time-placeholder-secret-minimum-32-characters
ENV AUTH_TRUST_HOST=1
ENV BACKEND_URL=http://localhost:8080
ENV BASE_URL=http://localhost:3000
RUN pnpm build

FROM base AS runner
WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

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
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"

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