# Single image for both modes. The repository itself is bind-mounted at runtime
# (see docker-compose.yml), so day-to-day code changes never require a rebuild —
# the entrypoint installs/builds on start and `dev` mode additionally watches.
FROM node:22-bookworm-slim

# openssl is required by Prisma's query engine; ca-certificates for outbound TLS.
RUN apt-get update \
    && apt-get install -y --no-install-recommends openssl ca-certificates curl \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Warm the layer cache with the dependency manifests only.
COPY package.json package-lock.json ./
RUN npm ci --include=dev

COPY . .
RUN npx prisma generate

COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

ENV NODE_ENV=production
EXPOSE 3000

HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \
  CMD curl -fsS http://127.0.0.1:3000/health || exit 1

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
