FROM docker.io/oven/bun:1.3.14 AS base

WORKDIR /repo
ENV PATH=/repo/apps/web/node_modules/.bin:/repo/packages/dashboard-model/node_modules/.bin:/repo/packages/ui/node_modules/.bin:/repo/node_modules/.bin:$PATH

FROM base AS pruner

COPY . .
RUN bunx turbo prune @dimensionlab/web --docker

FROM base AS deps

COPY --from=pruner /repo/out/json/ ./
RUN bun install --frozen-lockfile --ignore-scripts

FROM deps AS build

COPY --from=pruner /repo/out/full/ ./
COPY --from=pruner /repo/tsconfig.base.json /repo/tsconfig.json ./
RUN bun run build

FROM base AS runtime

WORKDIR /repo/apps/web
ENV NODE_ENV=production
ENV HOST=0.0.0.0
ENV PORT=3000
ENV DATABASE_URL=file:/data/dimensionlab.sqlite
ENV DASHBOARD_MIGRATIONS_DIR=/repo/apps/web/drizzle

COPY --from=build /repo/apps/web/build ./build
COPY --from=build /repo/apps/web/dist ./dist
COPY --from=build /repo/apps/web/drizzle ./drizzle

RUN mkdir -p /data
VOLUME ["/data"]
EXPOSE 3000

CMD ["bun", "build/index.js"]
