# syntax=docker/dockerfile:1.7
FROM python:3.12-slim AS base

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1

# Runtime deps: stockfish for eval, tmux for the agent panes, curl for the
# in-container vendor fetch step, jq for pane-agent.sh JSON parse,
# node+npm for the claude-code CLI.
RUN apt-get update && apt-get install -y --no-install-recommends \
    stockfish \
    tmux \
    curl \
    ca-certificates \
    procps \
    jq \
    xz-utils \
    gnupg \
 && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
 && apt-get install -y --no-install-recommends nodejs \
 && rm -rf /var/lib/apt/lists/*

# claude-code CLI — install globally via npm. The version pin keeps the
# image deterministic; bump as needed.
RUN npm install -g --omit=dev @anthropic-ai/claude-code@2.1.141 \
 && claude --version

# codex CLI — official linux release tarball. The musl static binary
# works on Debian.
ARG CODEX_VERSION=0.130.0
RUN curl -fsSL "https://github.com/openai/codex/releases/download/rust-v${CODEX_VERSION}/codex-x86_64-unknown-linux-musl.tar.gz" \
      -o /tmp/codex.tar.gz \
 && mkdir -p /tmp/codex && tar -xzf /tmp/codex.tar.gz -C /tmp/codex --strip-components=0 \
 && find /tmp/codex -type f -name 'codex-*' -exec mv {} /usr/local/bin/codex \; \
 && chmod +x /usr/local/bin/codex \
 && rm -rf /tmp/codex /tmp/codex.tar.gz \
 && codex --version

# Debian's stockfish package installs to /usr/games/stockfish which is not
# on the default PATH for non-login shells. Symlink it so `which stockfish`
# works for both the Python wrapper and any spawn from tmux panes.
RUN ln -s /usr/games/stockfish /usr/local/bin/stockfish && stockfish --help >/dev/null 2>&1 || true

WORKDIR /app

# Copy everything first — pip install -e needs src/ai_chess to exist at
# build time so the editable wheel can find the package.
COPY pyproject.toml README.md ./
COPY src ./src
COPY public ./public
COPY scripts ./scripts
COPY docs ./docs

# Python deps (editable install of the project itself + dev tools)
RUN pip install hatchling \
 && pip install -e ".[dev]"

# Vendor JS/CSS at build time so the container is fully self-contained.
RUN chmod +x scripts/*.sh && ./scripts/fetch-vendor.sh

# Runtime artefacts (games/, logs/, config/) — keep as named mountable dirs.
RUN mkdir -p games logs config

# claude-code refuses to run with --dangerously-skip-permissions as root,
# so the container's main process drops to a non-root user. Codex is also
# fine here. The user's HOME (=/home/chess) is where ~/.codex lives, so
# the docker-compose volume mounts target /home/chess/.codex.
RUN useradd -m -u 1000 -s /bin/bash chess && chown -R chess:chess /app

# Pre-seed claude-code state so its first-run onboarding (theme picker,
# changelog popup, etc.) is skipped — the flag lives in ~/.claude.json
# (a file in HOME, *not* the ~/.claude/ directory). Without this every
# fresh pane-agent.sh hangs at the theme prompt.
RUN mkdir -p /home/chess/.claude && \
    cat > /home/chess/.claude/settings.json <<'EOF'
{
  "theme": "light",
  "permissions": { "allow": ["*"], "defaultMode": "bypassPermissions" },
  "skipDangerousModePermissionPrompt": true
}
EOF
RUN cat > /home/chess/.claude.json <<'EOF'
{
  "hasCompletedOnboarding": true,
  "lastOnboardingVersion": "2.1.141",
  "numStartups": 1,
  "autoUpdates": false,
  "officialMarketplaceAutoInstallAttempted": true,
  "officialMarketplaceAutoInstalled": false,
  "migrationVersion": 13,
  "opusProMigrationComplete": true,
  "sonnet1m45MigrationComplete": true,
  "projects": {
    "/app": {
      "hasTrustDialogAccepted": true,
      "hasClaudeMdExternalIncludesApproved": true,
      "hasClaudeMdExternalIncludesWarningShown": true,
      "projectOnboardingSeenCount": 1,
      "allowedTools": ["*"]
    }
  }
}
EOF
RUN chown -R chess:chess /home/chess
USER chess
WORKDIR /app
ENV HOME=/home/chess

EXPOSE 7833
CMD ["python", "-m", "ai_chess"]
