# Build context is the repository root (Bun workspace monorepo).
FROM oven/bun:1.4.0 AS build
WORKDIR /app

# Manifests first so `bun install` only reruns when the lockfile changes.
COPY package.json bun.lock bunfig.toml ./
COPY apps/api/package.json apps/api/package.json
COPY apps/web/package.json apps/web/package.json
COPY packages/shared/package.json packages/shared/package.json
RUN bun install --frozen-lockfile

COPY . .
# Empty by default: in production the SPA and the API share one origin and the
# browser calls `/api/*` (nginx proxies it). Set the build arg to point the
# frontend at a different API origin.
ARG VITE_API_URL=""
ENV VITE_API_URL=$VITE_API_URL
RUN cd apps/web && bun run build

FROM nginx:alpine AS runtime
COPY apps/web/nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/apps/web/dist /usr/share/nginx/html
EXPOSE 80
