# Multi-stage build for Next.js frontend
FROM node:26-trixie-slim@sha256:e6bedef217677b276c591795e169840edd9096dbf13f29df8ef8d1856b8820bc 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@12.2.2

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

# Install exactly what the lockfile says and skip lifecycle scripts
RUN npm ci --ignore-scripts

# 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:26-trixie-slim@sha256:e6bedef217677b276c591795e169840edd9096dbf13f29df8ef8d1856b8820bc 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"]
