FROM node:20-slim

# Install better-sqlite3 build dependencies
RUN apt-get update && apt-get install -y python3 make g++ && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copy package files and install
COPY package.json package-lock.json ./
RUN npm ci --production

# Copy source
COPY . .

# Data directory (mount a volume here)
ENV WALL_E_DATA_DIR=/data
RUN mkdir -p /data/backups

# Health check
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD node -e "fetch('http://localhost:3457/api/wall-e/health').then(r=>r.ok?process.exit(0):process.exit(1)).catch(()=>process.exit(1))"

EXPOSE 3457

CMD ["node", "agent.js"]
