# syntax=docker/dockerfile:1

# ─────────────────────────────────────────────────────────────────────────────
#  api — production image
#
#  Two stages. `deps` installs production dependencies only, then the final
#  stage receives just the runtime artifacts. Everything pnpm keeps for
#  itself (content-addressable store, metadata cache, npm cache) stays behind
#  in the build stage, so it never inflates the shipped image.
# ─────────────────────────────────────────────────────────────────────────────

# Typst — compiles certificate.typ into the downloadable PDF certificate.
# Not available as an apk package, so lift the binary out of pandoc/typst.
FROM pandoc/typst:latest-alpine AS typst

# ── Stage 1: dependencies (production only) ──────────────────────────────────
FROM node:22-alpine AS deps

RUN corepack enable && corepack prepare pnpm@11.3.0 --activate

WORKDIR /app

# Workspace manifests first (cache-friendly), then a filtered install —
# pnpm needs every workspace package's manifest present to resolve the
# graph even though only apps/api's deps get installed here. pnpm-lock.yaml
# is gitignored (not committed), so this resolves fresh rather than
# installing from a frozen lockfile.
COPY package.json pnpm-workspace.yaml ./
COPY apps/api/package.json ./apps/api/package.json
COPY apps/web/package.json ./apps/web/package.json

# --prod skips jest / nodemon / concurrently. The pnpm store is deleted in
# the same layer: node_modules entries are hardlinks, so they survive the
# delete while ~500 MB of duplicate store files do not.
#
# ffprobe-static ships ffprobe for darwin/linux/win32 + ia32/x64 (~335 MB).
# The runtime image installs the distro ffmpeg instead (see below), which both
# services prefer via `which ffprobe`, so only the package stub is kept.
RUN pnpm install --filter api... --prod --no-frozen-lockfile \
 && rm -rf /root/.local/share/pnpm /root/.cache/pnpm /root/.cache/node /root/.npm \
 && rm -rf /app/node_modules/.pnpm/ffprobe-static@*/node_modules/ffprobe-static/bin

# ── Stage 2: runtime ─────────────────────────────────────────────────────────
FROM node:22-alpine

# sequelize-cli stays available for manual `db:migrate` runs on the droplet;
# the npm cache is cleared in the same layer so it never ships.
#
# ffmpeg brings ffprobe with it and is what the mov/mkv fast-start remux
# (services/ffmpeg.service.js) actually needs — ffprobe-static never shipped an
# ffmpeg binary. Costs ~100 MB unpacked (libavcodec/x265/aom), still far less
# than the 335 MB of removed ffprobe-static binaries.
RUN apk add --no-cache ffmpeg \
 && npm install -g --no-audit --no-fund sequelize-cli@^6.6.5 \
 && npm cache clean --force \
 && rm -rf /root/.npm

COPY --from=typst /usr/local/bin/typst /usr/local/bin/typst

WORKDIR /app

# Symlinks inside pnpm's node_modules are relative, so copying the same
# directory layout keeps them resolving correctly.
COPY --from=deps /app/node_modules ./node_modules
COPY --from=deps /app/apps/api/node_modules ./apps/api/node_modules

COPY package.json pnpm-workspace.yaml ./
COPY apps/api ./apps/api

WORKDIR /app/apps/api

RUN chmod +x docker-entrypoint.sh

EXPOSE 3024

ENTRYPOINT ["./docker-entrypoint.sh"]
