# syntax=docker/dockerfile:1
# ===== ATS — self-hosted image -----
# Build from the REPO ROOT or the root of the extracted release kit (the image needs the whole workspace):
#   docker build -f apps/node/Dockerfile -t ats .
# BuildKit picks up apps/node/Dockerfile.dockerignore for this Dockerfile.
# Node runs the TypeScript sources directly (type stripping), so there is no server build step; only the
# UI is compiled, in its own stage, and copied in.

# ---------- build the UI ----------
FROM node:26-slim AS ui
WORKDIR /app
COPY package.json package-lock.json tsconfig.base.json ./
COPY packages/core/package.json packages/core/
COPY packages/http/package.json packages/http/
COPY packages/ui/package.json packages/ui/
COPY apps/node/package.json apps/node/
COPY apps/cloudflare/package.json apps/cloudflare/
RUN npm ci --no-audit --no-fund
COPY packages/core packages/core
COPY packages/ui packages/ui
RUN npm run build -w @ats/ui

# ---------- runtime ----------
FROM node:26-slim
ENV NODE_ENV=production \
    HOST=0.0.0.0 \
    PORT=3000 \
    BASE_PATH=/ats \
    DATA_DIR=/data
WORKDIR /app
COPY package.json package-lock.json ./
COPY packages/core/package.json packages/core/
COPY packages/http/package.json packages/http/
COPY packages/ui/package.json packages/ui/
COPY apps/node/package.json apps/node/
COPY apps/cloudflare/package.json apps/cloudflare/
RUN npm ci --omit=dev --no-audit --no-fund && npm cache clean --force
COPY packages/core/src packages/core/src
COPY packages/http/src packages/http/src
COPY apps/node/src apps/node/src
COPY --from=ui /app/packages/ui/dist packages/ui/dist
RUN mkdir -p /data && chown node:node /data

USER node
VOLUME ["/data"]
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --start-interval=2s --retries=3 \
  CMD node -e "fetch('http://127.0.0.1:'+process.env.PORT+process.env.BASE_PATH+'/api/health').then(r=>process.exit(r.ok?0:1),()=>process.exit(1))"
CMD ["node", "apps/node/src/main.ts"]
