FROM node:20-slim

# Install ripgrep, git, and docker CLI
RUN apt-get update && apt-get install -y --no-install-recommends \
    ripgrep \
    git \
    ca-certificates \
    curl \
    gnupg \
    && install -m 0755 -d /etc/apt/keyrings \
    && curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg \
    && chmod a+r /etc/apt/keyrings/docker.gpg \
    && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable" \
       > /etc/apt/sources.list.d/docker.list \
    && apt-get update \
    && apt-get install -y --no-install-recommends docker-ce-cli \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install all deps including devDeps (tsc needs them).
# Override NODE_ENV so Coolify's build-time NODE_ENV=production doesn't skip devDeps.
COPY package*.json ./
RUN NODE_ENV=development npm ci

# Copy source and compile
COPY tsconfig.json ./
COPY src/ ./src/
RUN npm run build

# Prune dev deps after build
RUN npm prune --omit=dev

# Create workspace dir
RUN mkdir -p /workspaces

# Git identity for commits made by agents
RUN git config --global user.email "agent@vibnai.com" && \
    git config --global user.name "Vibn Agent Runner"

EXPOSE 3333

ENV NODE_ENV=production
ENV PORT=3333

CMD ["node", "dist/server.js"]
