# Single image serving the whole application: the NestJS server (API, MCP,
# scheduler) also serves the built client, so one container is enough.

# ── Client build ──────────────────────────────────────────────────────────────
FROM node:24-slim AS client-build

WORKDIR /app

# Root lockfile + workspace manifest so npm ci can resolve the full graph
COPY package*.json ./
COPY client/package.json ./client/
RUN npm ci -w client

COPY client ./client
RUN npm run -w client build

# ── Server build ──────────────────────────────────────────────────────────────
FROM node:24-slim AS server-build

WORKDIR /app

COPY package*.json ./
COPY server/package.json ./server/
RUN npm ci -w server

COPY server/nest-cli.json ./server/
COPY server/tsconfig*.json ./server/
COPY server/src ./server/src
RUN npm run -w server build

# ── Runtime dependencies ──────────────────────────────────────────────────────
FROM node:24-slim AS runtime-deps

WORKDIR /app

COPY package*.json ./
COPY server/package.json ./server/

# Production dependencies and Playwright's headless-only Chromium build,
# matching the installed Playwright version. All browser launches are
# headless, so the full desktop Chromium (GTK, Mesa/LLVM, icon themes) isn't
# needed, and neither is ffmpeg (only used for video recording).
#
# Then drop what nothing loads at runtime: source maps, TypeScript sources and
# declarations, docs (but not license files), TypeORM's browser build and
# better-sqlite3's C sources (the addon is already compiled).
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
RUN npm ci --omit=dev -w server \
    && npx playwright install --only-shell chromium \
    && rm -rf /ms-playwright/ffmpeg-* \
    && find node_modules -type f \( \
        -name '*.map' -o -name '*.ts' -o -name '*.mts' -o -name '*.cts' \
        -o \( \( -iname '*.md' -o -iname '*.markdown' \) \
              ! -iname 'licen[cs]e*' ! -iname 'notice*' ! -iname 'copying*' \) \
       \) -delete \
    && rm -rf node_modules/typeorm/browser \
        node_modules/better-sqlite3/deps \
        node_modules/better-sqlite3/src

# ── License notices ───────────────────────────────────────────────────────────
# One file with the project's LICENSE and the license of everything the image
# ships: the production dependencies of both workspaces (the client's are
# bundled into its build, so their license files aren't in the image
# otherwise) and the Node.js binary, whose LICENSE the node image doesn't
# include.
FROM node:24-slim AS licenses

WORKDIR /app

COPY package*.json LICENSE ./
COPY client/package.json ./client/
COPY server/package.json ./server/
COPY scripts/third-party-licenses.mjs ./scripts/
RUN npm ci --omit=dev --ignore-scripts \
    && node scripts/third-party-licenses.mjs > licenses.txt \
    && node -e ' \
        const url = `https://raw.githubusercontent.com/nodejs/node/v${process.versions.node}/LICENSE`; \
        fetch(url).then(async (r) => { \
          if (!r.ok) throw new Error(`${url}: ${r.status}`); \
          const rule = "-".repeat(79); \
          process.stdout.write(`\n${rule}\n\nnode@${process.versions.node}\nLicense: MIT\n\n${(await r.text()).trim()}\n`); \
        }).catch((e) => { console.error(e.message); process.exit(1); });' >> licenses.txt

# ── Runtime ───────────────────────────────────────────────────────────────────
# Plain Debian with just the node binary: npm, yarn, corepack and the C
# headers that come with the node image aren't needed to run the app.
FROM debian:bookworm-slim AS runtime

# Set by CI from the root package.json version
ARG VERSION=dev
LABEL org.opencontainers.image.title="liqa" \
      org.opencontainers.image.version="${VERSION}" \
      org.opencontainers.image.licenses="MIT" \
      org.opencontainers.image.source="https://git.hq.ars9.space/ars9/liqa"

# Runtime only: in the build stages it would make npm ci skip devDependencies
ENV NODE_ENV=production

# Shared libraries chrome-headless-shell links against (from `ldd`), plus
# fontconfig and basic fonts for text rendering. Playwright's `--with-deps`
# would also pull in xvfb (with Mesa/LLVM) and extra font packages, which
# headless-only use doesn't need.
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    fontconfig \
    fonts-dejavu-core \
    fonts-liberation \
    libasound2 \
    libatk-bridge2.0-0 \
    libatk1.0-0 \
    libatspi2.0-0 \
    libdbus-1-3 \
    libexpat1 \
    libgbm1 \
    libglib2.0-0 \
    libnspr4 \
    libnss3 \
    libx11-6 \
    libxcb1 \
    libxcomposite1 \
    libxdamage1 \
    libxext6 \
    libxfixes3 \
    libxkbcommon0 \
    libxrandr2 \
    && rm -rf /var/lib/apt/lists/*

COPY --from=runtime-deps /usr/local/bin/node /usr/local/bin/node

WORKDIR /app

COPY package*.json ./
COPY server/package.json ./server/

ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
COPY --from=runtime-deps /ms-playwright /ms-playwright
COPY --from=runtime-deps /app/node_modules ./node_modules

COPY --from=server-build /app/server/dist ./dist
COPY --from=client-build /app/client/dist ./client

# The project's license, and every shipped component's license notices (also
# served by the app at /licenses.txt)
COPY LICENSE ./
COPY --from=licenses /app/licenses.txt ./client/licenses.txt

# Serve the built client at / (see server/src/common/client-static.ts)
ENV CLIENT_DIR=client

# Runtime directory (SQLite DB and stored files, mounted as a volume)
RUN mkdir -p data

EXPOSE 3000

# Apply pending database migrations, then start the server (which refuses to
# start while migrations are pending)
CMD ["sh", "-c", "node dist/database/migrate && exec node dist/main"]
