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/backend/package.json packages/backend/
COPY packages/admin/package.json packages/admin/
RUN bun install --frozen-lockfile

FROM oven/bun:1.3.11-alpine
ARG APP_VERSION=dev
ENV APP_VERSION=${APP_VERSION}
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/backend/node_modules ./packages/backend/node_modules
COPY packages/shared ./packages/shared
COPY packages/backend ./packages/backend
COPY package.json ./
COPY tsconfig.base.json ./
RUN addgroup -S app && adduser -S app -G app
COPY packages/backend/src/db/migrations ./migrations
ENV MIGRATIONS_DIR=/app/migrations
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
  CMD wget -qO- http://localhost:8000/v1/health || exit 1
USER app
CMD ["bun", "run", "packages/backend/src/main.ts"]
