FROM oven/bun:1.3.11 AS base

WORKDIR /app

# Install dependencies
COPY package.json bun.lock ./
COPY patches ./patches
COPY api/package.json ./api/package.json
COPY dalpuri/package.json ./dalpuri/package.json
COPY ui/package.json ./ui/package.json
RUN bun install --frozen-lockfile

# Copy UI source files
COPY ui/ ./ui/

WORKDIR /app/ui

ARG PUBLIC_API_URL=https://opt-api.osdci.net
ENV PUBLIC_API_URL=$PUBLIC_API_URL

RUN bun run prepare
RUN bun run build:server

# Bundle the server into a single file with all dependencies
RUN bun build build/index.js --target node --outdir build-bundled --minify

# Production image
FROM node:22-alpine AS production

WORKDIR /app

COPY --from=base /app/ui/build-bundled/index.js ./index.js
COPY --from=base /app/ui/build/client ./client

ENV NODE_ENV=production
ENV PORT=3000
ENV ORIGIN=https://optima.osdci.net

EXPOSE 3000

CMD ["node", "index.js"]
