FROM oven/bun:1.4.0 AS builder
WORKDIR /app
COPY package.json bun.lock ./
RUN bun install --frozen-lockfile
COPY . .
ENV NODE_ENV=production
ENV BETTER_AUTH_SECRET=build-placeholder-at-least-32-characters
ENV ORIGIN=http://localhost:3000
RUN bun --bun x vitest --run
RUN bun run build

FROM oven/bun:1.4.0
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/build ./build
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/prod-server.ts ./prod-server.ts
COPY --from=builder /app/src/lib/server/ws-attach.ts ./src/lib/server/ws-attach.ts
COPY --from=builder /app/scripts ./scripts
ENV NODE_ENV=production
ENV PORT=3000
ENV HOST=0.0.0.0
ENV BODY_SIZE_LIMIT=6M
EXPOSE 3000
CMD ["bun", "prod-server.ts"]
