# Multi-stage build for Next.js frontend
FROM node:24-trixie-slim@sha256:9707cd4542f400df5078df04f9652a272429112f15202d22b5b8bdd148df494f AS builder

WORKDIR /app

RUN apt-get update && apt-get upgrade -y && apt-get clean && rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/*

RUN npm install -g npm@11.12.1

# Copy package files
COPY package.json package-lock.json ./

# Use npm install so Docker builds are not blocked by lockfile drift and
# platform-specific optional dependencies introduced by Next.js tooling.
RUN npm install

# Copy source files
COPY . .

# Build the Next.js app (produces .next/standalone/)
RUN npm run build

# Production stage - Node.js runtime (no nginx; standalone server serves everything)
FROM node:24-trixie-slim@sha256:9707cd4542f400df5078df04f9652a272429112f15202d22b5b8bdd148df494f AS runner

WORKDIR /app

RUN apt-get update && apt-get upgrade -y && apt-get clean && rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/*

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

# Copy the standalone build output
COPY --from=builder /app/.next/standalone ./

# Copy static assets and public directory (not bundled into standalone/)
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public

EXPOSE 3000

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