FROM node:24-alpine AS base

# Install pnpm
RUN corepack enable pnpm

# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat

WORKDIR /app

# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc* ./

ARG GITHUB_NPM_TOKEN
ENV GITHUB_NPM_TOKEN=$GITHUB_NPM_TOKEN

# Install dependencies using pnpm
RUN \
    if [ -f pnpm-lock.yaml ]; then pnpm install --frozen-lockfile --ignore-scripts; \
    else echo "Lockfile not found." && exit 1; \
    fi

# For production Image Optimization with Next.js, the optional 'sharp' package is strongly recommended. 
# Next.js will use it automatically for Image Optimization.
RUN pnpm add sharp


# Run linters
FROM base AS lint

WORKDIR /app

COPY --from=deps /app/node_modules ./node_modules
COPY . .

ARG GITHUB_NPM_TOKEN
ENV GITHUB_NPM_TOKEN=$GITHUB_NPM_TOKEN

RUN pnpm check:ci


# Run unit tests
FROM base AS test

WORKDIR /app

COPY --from=deps /app/node_modules ./node_modules
COPY . .

ARG GITHUB_NPM_TOKEN
ENV GITHUB_NPM_TOKEN=$GITHUB_NPM_TOKEN

RUN pnpm test


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

WORKDIR /app

COPY --from=deps /app/node_modules ./node_modules
COPY . .

ARG GITHUB_NPM_TOKEN
ENV GITHUB_NPM_TOKEN=$GITHUB_NPM_TOKEN

ARG POEDITOR_PROJECT_ID
ENV POEDITOR_PROJECT_ID=$POEDITOR_PROJECT_ID

ARG POEDITOR_API_TOKEN
ENV POEDITOR_API_TOKEN=$POEDITOR_API_TOKEN

# Check if both environment variables are set
RUN if [ -n "$POEDITOR_PROJECT_ID" ] && [ -n "$POEDITOR_API_TOKEN" ]; \
    then pnpm i18n:sync; \
    fi


ENV NODE_ENV=production
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Comment the following line in case you want to enable telemetry during the build.
ENV NEXT_TELEMETRY_DISABLED=1

RUN pnpm build


# Production image
FROM base AS deploy

WORKDIR /app

ENV NODE_ENV=production
# Comment the following line in case you want to enable telemetry during runtime.
ENV NEXT_TELEMETRY_DISABLED=1

# Install curl for ECS health check
RUN apk add --no-cache curl

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

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

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next

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

# Copy the entrypoint script into the container
COPY entrypoint.sh /app/entrypoint.sh
# Copy the Datadog initialization module into the container
COPY dd-init.mts /app/dd-init.mts

# Make the entrypoint script executable
RUN chmod +x /app/entrypoint.sh

USER nextjs

EXPOSE 8080

ENV PORT=8080
# AWS Fargate overrides this instruction because its host process sets HOSTNAME
# Alternatively, HOSTNAME can be set in the CMD instruction that launches the server
ENV HOSTNAME="0.0.0.0"

# Set the entrypoint script to run before CMD
ENTRYPOINT ["/app/entrypoint.sh"]

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