# ppy: the single shared image every DevPlace container instance runs.
# Build once with `make ppy`. Context is devplacepy/services/containers/files.
FROM python:3.13-slim-bookworm AS base

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PIP_DEFAULT_TIMEOUT=120 \
    PIP_RETRIES=10 \
    DEBIAN_FRONTEND=noninteractive \
    PLAYWRIGHT_BROWSERS_PATH=/opt/playwright

RUN apt-get update && apt-get install -y --no-install-recommends \
        git curl wget vim ack ca-certificates build-essential libpq-dev \
        tmux apache2-utils procps htop iftop iotop netcat-openbsd zip unzip \
        fakeroot \
    && rm -rf /var/lib/apt/lists/*

FROM base AS deps
RUN pip install \
        pip setuptools wheel packaging \
        requests urllib3 certifi idna charset-normalizer httpx aiohttp \
        pydantic typing-extensions python-dotenv aiofiles \
        python-dateutil six pytz \
        pyyaml toml \
        loguru structlog tenacity rich tqdm click typer
RUN pip install \
        numpy pandas matplotlib plotly \
        sqlalchemy psycopg2-binary redis dataset
RUN pip install \
        flask django fastapi starlette "uvicorn[standard]" gunicorn \
        h2 celery apscheduler watchfiles \
        websocket-client websockets \
        beautifulsoup4 lxml scrapy
RUN pip install \
        cryptography pycryptodome pillow \
        pytest ruff black isort flake8 mypy \
        sphinx mkdocs poetry pipenv hatch \
        reflex
RUN pip install playwright && playwright install --with-deps chromium

FROM deps AS runtime
COPY sudo /usr/local/bin/sudo
COPY aptroot /usr/local/bin/aptroot
COPY pagent /usr/bin/pagent.py
COPY bot.py /usr/bin/botje.py
COPY d.py /usr/bin/d.py
COPY .vimrc /home/pravda/.vimrc
RUN set -eu; \
    chmod 0755 /usr/local/bin/sudo /usr/local/bin/aptroot /usr/bin/pagent.py /usr/bin/botje.py /usr/bin/d.py; \
    ln -sf /usr/local/bin/sudo /usr/bin/sudo || true; \
    for t in apt apt-get dpkg; do ln -sf /usr/local/bin/aptroot "/usr/local/bin/$t"; done; \
    if getent passwd 1000 >/dev/null 2>&1; then \
        existing="$(getent passwd 1000 | cut -d: -f1)"; \
        if [ "$existing" != "pravda" ]; then userdel "$existing" 2>/dev/null || deluser "$existing" 2>/dev/null || true; fi; \
    fi; \
    if ! getent group pravda >/dev/null 2>&1; then groupadd --gid 1000 pravda 2>/dev/null || addgroup -g 1000 pravda 2>/dev/null || true; fi; \
    if ! id pravda >/dev/null 2>&1; then \
        useradd --uid 1000 --gid 1000 --create-home --shell /bin/bash pravda 2>/dev/null \
        || adduser -u 1000 -G pravda -D -s /bin/sh pravda 2>/dev/null || true; \
    fi; \
    mkdir -p /app /home/pravda/.local/bin; \
    for d in /app /home/pravda /usr/local/lib /usr/local/bin /usr/local/share /usr/lib/python3 /opt \
             /usr/lib /usr/bin /usr/sbin /usr/share /usr/include /etc /var/lib /var/cache /var/log /srv; do \
        [ -e "$d" ] && chown -R pravda:pravda "$d" 2>/dev/null || true; \
    done

USER pravda
ENV PATH=/home/pravda/.local/bin:$PATH
WORKDIR /app
CMD ["sleep", "infinity"]
