# syntax=docker/dockerfile:1.7

FROM oven/bun:1.3.11 AS build

WORKDIR /app

ARG NEXT_PUBLIC_API_URL=""
ARG NEXT_PUBLIC_NBBO_MAX_AGE_MS=1000

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
ENV NEXT_PUBLIC_NBBO_MAX_AGE_MS=${NEXT_PUBLIC_NBBO_MAX_AGE_MS}

COPY --from=workspace package.json ./package.json
COPY --from=workspace bun.lock ./bun.lock
COPY --from=workspace tsconfig.base.json ./tsconfig.base.json

COPY --from=apps desktop/package.json ./apps/desktop/package.json
COPY --from=apps web/package.json ./apps/web/package.json

COPY --from=packages bus/package.json ./packages/bus/package.json
COPY --from=packages config/package.json ./packages/config/package.json
COPY --from=packages observability/package.json ./packages/observability/package.json
COPY --from=packages storage/package.json ./packages/storage/package.json
COPY --from=packages types/package.json ./packages/types/package.json

COPY --from=services api/package.json ./services/api/package.json
COPY --from=services candles/package.json ./services/candles/package.json
COPY --from=services compute/package.json ./services/compute/package.json
COPY --from=services eod-enricher/package.json ./services/eod-enricher/package.json
COPY --from=services ingest-equities/package.json ./services/ingest-equities/package.json
COPY --from=services ingest-options/package.json ./services/ingest-options/package.json
COPY --from=services refdata/package.json ./services/refdata/package.json
COPY --from=services replay/package.json ./services/replay/package.json

RUN --mount=type=cache,target=/root/.bun/install/cache \
  bun install --frozen-lockfile

COPY --from=services . ./services
COPY --from=packages . ./packages
COPY --from=apps . ./apps

RUN bun run --cwd apps/web build

FROM oven/bun:1.3.11 AS runtime

WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

COPY --from=build /app/package.json ./package.json
COPY --from=build /app/bun.lock ./bun.lock
COPY --from=build /app/tsconfig.base.json ./tsconfig.base.json
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/apps/web ./apps/web
COPY --from=build /app/packages ./packages

EXPOSE 3000

CMD ["bun", "run", "--cwd", "apps/web", "start"]
