# Unified CI image for Harb services (ponder, webapp, landing, txnBot).
# Parameterized via build args — eliminates per-service Dockerfile duplication.
#
# Usage:
#   docker build -f docker/Dockerfile.service-ci \
#     --build-arg SERVICE_DIR=services/ponder \
#     --build-arg SERVICE_PORT=42069 \
#     --build-arg ENTRYPOINT_SCRIPT=containers/ponder-entrypoint.sh \
#     -t ponder-ci .

# ── Build args (declared early for builder stage) ──────────────────
ARG SERVICE_DIR
ARG NPM_INSTALL_CMD=ci

# ── Builder stage ──────────────────────────────────────────────────
FROM node:20-alpine AS builder

RUN apk add --no-cache git bash

WORKDIR /app

# Copy root package files
COPY package.json package-lock.json ./

# Copy kraiken-lib package files
COPY kraiken-lib/package.json kraiken-lib/package-lock.json ./kraiken-lib/

# Copy ABI files needed by kraiken-lib
COPY onchain/out/Kraiken.sol/Kraiken.json ./onchain/out/Kraiken.sol/
COPY onchain/out/Stake.sol/Stake.json ./onchain/out/Stake.sol/

# Copy Stake.sol for sync-tax-rates + the script itself
COPY onchain/src/Stake.sol ./onchain/src/
COPY scripts/sync-tax-rates.mjs ./scripts/

# Install kraiken-lib dependencies, run sync-tax-rates, and build
WORKDIR /app/kraiken-lib
RUN npm ci --ignore-scripts
COPY kraiken-lib/ ./
RUN node ../scripts/sync-tax-rates.mjs && ./node_modules/.bin/tsc

# Install service dependencies
ARG SERVICE_DIR
ARG NPM_INSTALL_CMD
WORKDIR /app/${SERVICE_DIR}
COPY ${SERVICE_DIR}/package.json ./
# Use glob pattern to optionally copy package-lock.json (txnBot has none)
COPY ${SERVICE_DIR}/package-lock.jso[n] ./
RUN if [ "$NPM_INSTALL_CMD" = "install" ]; then npm install; else npm ci; fi

# Copy service source
COPY ${SERVICE_DIR}/ ./

# Ensure root node_modules exists (may be populated by workspace hoisting, or empty)
RUN mkdir -p /app/node_modules

# Copy onchain deployment artifacts (glob handles missing files)
WORKDIR /app
COPY onchain/deployments*.jso[n] ./onchain/

# ── Runtime stage ──────────────────────────────────────────────────
FROM node:20-alpine

RUN apk add --no-cache dumb-init wget bash

WORKDIR /app

# Copy kraiken-lib (src for Vite alias, dist for runtime, package.json for resolution)
COPY --from=builder /app/kraiken-lib/src ./kraiken-lib/src
COPY --from=builder /app/kraiken-lib/dist ./kraiken-lib/dist
COPY --from=builder /app/kraiken-lib/package.json ./kraiken-lib/

# Copy service with all node_modules
ARG SERVICE_DIR
COPY --from=builder /app/${SERVICE_DIR} ./${SERVICE_DIR}

# Copy root node_modules (workspace-hoisted deps for landing/webapp, empty for others)
COPY --from=builder /app/node_modules ./node_modules

# Copy onchain artifacts
COPY --from=builder /app/onchain ./onchain

# Create placeholder deployments-local.json if not present
RUN test -f /app/onchain/deployments-local.json || \
    (mkdir -p /app/onchain && echo '{"contracts":{}}' > /app/onchain/deployments-local.json)

# Conditionally create symlinks for Vite path resolution (webapp only)
ARG NEEDS_SYMLINKS=false
RUN if [ "$NEEDS_SYMLINKS" = "true" ]; then \
      ln -sf /app/web-app /web-app && \
      ln -sf /app/kraiken-lib /kraiken-lib && \
      ln -sf /app/onchain /onchain; \
    fi

# Copy entrypoint script
# For services with entrypoints (ponder, webapp, txnbot): pass the actual entrypoint
# For landing (no entrypoint): defaults to entrypoint-common.sh which is just helpers
ARG ENTRYPOINT_SCRIPT=containers/entrypoint-common.sh
COPY ${ENTRYPOINT_SCRIPT} /entrypoint.sh
RUN chmod +x /entrypoint.sh

# Set working directory to service
WORKDIR /app/${SERVICE_DIR}

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
ENV HOST=0.0.0.0

ARG SERVICE_PORT=8080
ENV PORT=${SERVICE_PORT}
EXPOSE ${SERVICE_PORT}

# HEALTHCHECK flags don't expand ARGs (Docker limitation), so values are hardcoded.
# PORT is an ENV (works in CMD at runtime). PATH is baked via ARG→ENV.
ARG HEALTHCHECK_PATH=/
ENV HEALTHCHECK_PATH=${HEALTHCHECK_PATH}
HEALTHCHECK --interval=5s --timeout=3s --retries=12 --start-period=20s \
  CMD wget --spider -q http://127.0.0.1:${PORT}${HEALTHCHECK_PATH} || exit 1

ENTRYPOINT ["dumb-init", "--", "/entrypoint.sh"]
# Force rebuild after cache prune — 2026-02-19T21:31:36Z
