FROM node:20-bookworm-slim

# Install build deps for native modules (node-pty, better-sqlite3, bcrypt)
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential python3 python3-setuptools \
    jq ripgrep sqlite3 zip unzip tree vim-tiny curl git \
  && rm -rf /var/lib/apt/lists/*

# Install Claude Code CLI and Taskmaster globally
RUN npm install -g @anthropic-ai/claude-code task-master-ai

WORKDIR /app

# Copy package files and scripts needed for postinstall hooks
COPY package*.json ./
COPY scripts/ ./scripts/
RUN npm ci

# VITE_ vars must be present at build time (baked into frontend bundle)
ARG FORGEJO_BASE_URL=https://forge.wilddragon.net
ENV VITE_FORGEJO_BASE_URL=${FORGEJO_BASE_URL}

# Copy source and build
COPY . .
RUN npm run build

# Create persistent data dirs and set ownership only on runtime-writable paths
RUN mkdir -p /home/node/workspace /home/node/.cloudcli /home/node/.claude \
  && chown -R node:node /home/node/

USER node

EXPOSE 3001

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