FROM node:22-alpine AS base
WORKDIR /app
RUN corepack enable
RUN apk add --no-cache openssl

FROM base AS deps
COPY package.json pnpm-workspace.yaml turbo.json tsconfig.base.json ./
COPY apps/bot/package.json apps/bot/package.json
COPY packages/shared/package.json packages/shared/package.json
RUN pnpm install --frozen-lockfile=false

FROM deps AS build
COPY apps ./apps
COPY packages ./packages
COPY eslint.config.js .eslintrc.cjs .prettierrc ./
RUN pnpm install --frozen-lockfile=false
RUN pnpm --filter @nexumi/shared build
RUN pnpm --filter @nexumi/bot prisma:generate
RUN pnpm --filter @nexumi/bot build

FROM base AS runner
RUN apk add --no-cache postgresql16-client
COPY --from=build /app/package.json ./package.json
COPY --from=build /app/pnpm-workspace.yaml ./pnpm-workspace.yaml
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/apps/bot ./apps/bot
COPY --from=build /app/packages/shared ./packages/shared
CMD ["sh", "-c", "pnpm --filter @nexumi/bot prisma:migrate && node apps/bot/dist/index.js"]
