ARG AUTO_ONCALL_RUNTIME_BASE_IMAGE=auto-oncall-services-runtime-base:local

FROM node:20-bookworm-slim@sha256:2cf067cfed83d5ea958367df9f966191a942351a2df77d6f0193e162b5febfc0 AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci

FROM deps AS build
COPY tsconfig.json ./
COPY src ./src
COPY test ./test
RUN npm run build

FROM ${AUTO_ONCALL_RUNTIME_BASE_IMAGE} AS runtime

ARG AUTO_ONCALL_BUILD_SHA=unknown
ENV AUTO_ONCALL_BUILD_SHA=${AUTO_ONCALL_BUILD_SHA}
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=8080
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
COPY package*.json ./
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
COPY product-knowledge ./product-knowledge
COPY skills /opt/auto-oncall-bundled-skills
COPY docs/opts /opt/auto-oncall-optimization-records
COPY scripts/install-codex-skills.sh ./scripts/install-codex-skills.sh
COPY scripts/codex-run-permissions-wrapper.sh ./scripts/codex-run-permissions-wrapper.sh
COPY scripts/cleanup-codex-history.sh ./scripts/cleanup-codex-history.sh
RUN chmod +x ./scripts/install-codex-skills.sh ./scripts/codex-run-permissions-wrapper.sh ./scripts/cleanup-codex-history.sh
USER node
EXPOSE 8080
ENTRYPOINT ["./scripts/install-codex-skills.sh"]
CMD ["node", "dist/src/server.js"]
