FROM mcr.microsoft.com/playwright:v1.59.1-noble AS base

# Install dependencies only when needed
FROM base AS deps
WORKDIR /app

# Install dependencies
COPY package.json package-lock.json ./
RUN npm ci --ignore-scripts && npx playwright install --with-deps

# For development, just copy the source over
FROM base AS app
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY ./.env ./hero.ts ./playwright.ts ./next.config.mjs ./package.json ./postcss.config.mjs ./tsconfig.json ./
COPY ./config ./config
COPY ./lib ./lib
COPY ./styles ./styles
COPY ./public ./public
COPY ./app ./app
COPY ./types ./types
COPY ./components ./components

ENV NEXT_TELEMETRY_DISABLED=1

# Rebuild the source code only when needed
FROM app AS builder

RUN npm run build && npm prune --omit=dev

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

RUN addgroup --system --gid 1002 nodejs && \
    adduser --system --uid 1002 nextjs

COPY --from=builder /app/public ./public

# Set permissions for prerender cache
RUN mkdir .next .next/cache && \
    chown nextjs:nodejs .next/cache

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT=3000
# set hostname to localhost
ENV HOSTNAME="0.0.0.0"

# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
CMD ["node", "server.js"]
