FROM oven/bun:1.3.11-alpine AS deps
WORKDIR /app
COPY package.json bun.lock ./
COPY packages/shared/package.json packages/shared/
COPY packages/admin/package.json packages/admin/
COPY packages/backend/package.json packages/backend/
RUN bun install --frozen-lockfile

FROM oven/bun:1.3.11-alpine AS build
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY --from=deps /app/packages/shared/node_modules ./packages/shared/node_modules
COPY --from=deps /app/packages/admin/node_modules ./packages/admin/node_modules
COPY packages/shared ./packages/shared
COPY packages/admin ./packages/admin
COPY package.json ./
COPY tsconfig.base.json ./
WORKDIR /app/packages/admin
RUN bun run build

FROM nginx:alpine
COPY --from=build /app/packages/admin/dist /usr/share/nginx/html
# nginx docker image processes templates in /etc/nginx/templates/ with envsubst at startup
COPY nginx.conf /etc/nginx/templates/default.conf.template
ENV BACKEND_URL=http://localhost:8000
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
