FROM oven/bun:alpine AS bun
# RUN apk add --no-cache upx
# RUN upx --all-methods --no-lzma /usr/local/bin/bun
FROM node:23-alpine AS base
COPY --from=bun /usr/local/bin/bun /usr/local/bin/

ENV NODE_ENV=production

RUN mkdir /app
COPY package.json /app
COPY bun.lock /app
WORKDIR /app

### BUILD ###
FROM base AS build
RUN bun install
COPY . /app
RUN cp .env.example .env
RUN bun svelte-kit sync
RUN bun run build
RUN rm .env

### PRODUCTION ###
FROM base AS production
ENV NODE_ENV=production
ENV PORT=3000
ENV HOST=0.0.0.0
EXPOSE 3000
RUN bun install -p
COPY --from=build /app/build /app/build
# Migrations: copy drizzle config + SQL files (run via `bunx drizzle-kit migrate`)
COPY drizzle.config.ts /app/
COPY drizzle/ /app/drizzle/
COPY src/lib/server/db/schema.ts /app/src/lib/server/db/schema.ts
# One-off scripts (backfills, reparses) run via `bun run <script>` against the
# deployed image. They import from src/, so copy both directories in.
COPY --from=build /app/infra /app/infra
COPY --from=build /app/src /app/src
# Liveness probe (negotiated-t46.4): busybox wget (bundled in alpine) exits
# non-zero on a 503 or connection failure, so Docker/Northflank can recycle a
# wedged container faster. /health checks the app + DB, not the GPU.
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
	CMD wget -qO- http://localhost:3000/health || exit 1
# Default command = web tier. The SAME image also runs the out-of-process match
# worker (negotiated-t46.7) — deploy a second Northflank service from this image
# with MATCH_WORKER_MODE=external on the web service and this start command on
# the worker (no HTTP port, no HEALTHCHECK relevance):
#   bun --preload ./infra/scripts/sveltekit-env-preload.ts src/worker.ts
# See docs/architecture/out-of-process-match-worker.md.
CMD [ "node", "build/index.js" ]