FROM node:22-slim

# System dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    git \
    curl \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Install provider CLIs
RUN npm install -g @anthropic-ai/claude-code @openai/codex 2>/dev/null || true

# Copy AIWG package
WORKDIR /opt/aiwg
COPY package*.json ./
RUN npm ci --production --ignore-scripts 2>/dev/null || npm install --production --ignore-scripts
COPY . .

# Workspace mount point
RUN mkdir -p /workspace
WORKDIR /workspace

# Environment
ENV NODE_ENV=production
ENV CI=true
ENV AIWG_DOCKER=1

# Web UI port
EXPOSE 7474

# Health check via heartbeat file
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
  CMD node -e "const fs=require('fs');const hb=JSON.parse(fs.readFileSync('/workspace/.aiwg/daemon/heartbeat','utf8'));const age=Date.now()-new Date(hb.timestamp).getTime();process.exit(age<120000?0:1)"

ENTRYPOINT ["node", "/opt/aiwg/tools/daemon/daemon-main.mjs"]
