FROM mcr.microsoft.com/devcontainers/base:debian

ARG FORGEJO_USERNAME
ENV FORGEJO_USERNAME=${FORGEJO_USERNAME}

# SSH (missing from base image)
RUN apt-get update \
    && apt-get install -y --no-install-recommends openssh-client \
    && rm -rf /var/lib/apt/lists/*

# Node.js 22 + npm (npm tree copied; bin symlinks recreated - COPY dereferences them)
COPY --from=node:22-slim /usr/local/bin/node /usr/local/bin/
COPY --from=node:22-slim /usr/local/lib/node_modules /usr/local/lib/node_modules
RUN ln -s ../lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm \
    && ln -s ../lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx

# uv (from distroless)
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv

# Python 3.12 (system-shared location; default /root is unreadable for vscode)
ENV UV_PYTHON_INSTALL_DIR=/opt/uv/python
RUN uv python install 3.12 \
    && ln -s /opt/uv/python/cpython-3.12-linux-x86_64-gnu/bin/python3.12 /usr/local/bin/python3.12 \
    && ln -s python3.12 /usr/local/bin/python3 \
    && ln -s python3 /usr/local/bin/python

# Trust the workspace bind mount (source on Windows FS -> uid mismatch)
RUN git config --system --add safe.directory /workspaces/kirche-einfach

# Pre-create named-volume targets (volumes inherit image content on first mount).
# The backend venv is created as `vscode` so VS Code's Python extension finds an
# interpreter immediately on first attach, before postCreateCommand runs `uv sync`.
USER vscode
RUN mkdir -p /workspaces/kirche-einfach/backend \
    && uv venv /workspaces/kirche-einfach/backend/.venv \
    && mkdir -p /workspaces/kirche-einfach/frontend/node_modules
USER root

# opencode (as non-root user)
USER vscode
RUN curl -fsSL https://opencode.ai/install | bash
USER root
RUN ln -s /home/vscode/.opencode/bin/opencode /usr/local/bin/opencode

# Fix ownership (home + volume targets so vscode user can write)
RUN chown -R vscode:vscode /home/vscode /workspaces

CMD ["sleep", "infinity"]
