# Start from the pre-built Nextjs Base image
FROM registry.infra.mintel.me/mintel/nextjs:latest AS builder

WORKDIR /app

# Build-time environment variables for Next.js
ARG NEXT_PUBLIC_BASE_URL
ARG UMAMI_API_ENDPOINT
ARG NEXT_PUBLIC_TARGET
ARG DIRECTUS_URL
ARG NPM_TOKEN

ENV NEXT_PUBLIC_BASE_URL=$NEXT_PUBLIC_BASE_URL
ENV UMAMI_API_ENDPOINT=$UMAMI_API_ENDPOINT
ENV NEXT_PUBLIC_TARGET=$NEXT_PUBLIC_TARGET
ENV DIRECTUS_URL=$DIRECTUS_URL
ENV NPM_TOKEN=$NPM_TOKEN
ENV SENTRY_SUPPRESS_TURBOPACK_WARNING=1

# Enable pnpm
RUN corepack enable

# Copy workspace configuration
COPY pnpm-lock.yaml pnpm-workspace.yaml package.json .npmrc ./
COPY apps/web/package.json ./apps/web/package.json

# Install dependencies (use cache mount if possible, but keep it simple as per standard)
RUN pnpm install --no-frozen-lockfile

# Copy source
COPY . .

# Build the app
RUN pnpm --filter @mintel/web build

# Production image
FROM registry.infra.mintel.me/mintel/runtime:latest AS runner
WORKDIR /app

# Copy standalone output and static files (Monorepo paths)
COPY --from=builder --chown=nextjs:nodejs /app/apps/web/public ./apps/web/public
COPY --from=builder --chown=nextjs:nodejs /app/apps/web/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static

USER nextjs

WORKDIR /app/apps/web
CMD ["node", "server.js"]
