# syntax=docker/dockerfile:1
#
# Build without proxy:
# DOCKER_BUILDKIT=1 docker build -t prettymaps-docker-test .
#
# Build with SOCKS5 proxy:
# DOCKER_BUILDKIT=1 docker build --network=host \
#     --build-arg PROXY_SOCKS5=$PROXY_IP:$PROXY_PORT \
#     -t prettymaps-docker-test .
#
# Do not upgrade pip (newer pip+urllib3 may break SOCKS).
# Pre-install shapely to avoid resolver loops on shapely[vectorized].

# --------------------------------------------------------------------------
# Stage 1: builder
# --------------------------------------------------------------------------

FROM debian:bookworm-slim AS builder

ARG PROXY_SOCKS5=""
ARG DEBIAN_FRONTEND=noninteractive

ENV LANG=C.UTF-8 \
    LC_ALL=C.UTF-8 \
    PYTHONUNBUFFERED=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PIP_DEFAULT_TIMEOUT=120 \
    PIP_PREFER_BINARY=1

RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    rm -f /etc/apt/apt.conf.d/docker-clean && \
    apt-get update && apt-get install -y --no-install-recommends \
        ca-certificates \
        curl \
        python3 \
        python3-pip \
        python3-venv \
        python3-dev \
        python3-socks \
        build-essential \
        gdal-bin \
        libgdal-dev \
        libgeos-dev \
        libproj-dev \
        libspatialindex-dev \
        git \
        fonts-dejavu-core

# Python environment
RUN python3 -m venv --system-site-packages /opt/venv

ENV PATH="/opt/venv/bin:$PATH"

# Optional SOCKS5 proxy.
# Empty PROXY_SOCKS5 disables the proxy.
# Proxy settings apply only to the builder stage.

ENV ALL_PROXY=${PROXY_SOCKS5:+socks5h://}${PROXY_SOCKS5}

ENV HTTP_PROXY=${ALL_PROXY} \
    HTTPS_PROXY=${ALL_PROXY} \
    http_proxy=${ALL_PROXY} \
    https_proxy=${ALL_PROXY} \
    no_proxy=localhost,127.0.0.1

# --------------------------------------------------------------------------
# Python dependencies
# --------------------------------------------------------------------------

# 1. Base tooling, NumPy and Shapely

RUN --mount=type=cache,target=/root/.cache/pip \
    pip install \
        setuptools \
        wheel \
        pysocks \
        "numpy>=1.26.4" \
        "shapely>=2.0.0"

# 2. Prettymaps runtime dependencies
# Streamlit and Marimo are intentionally excluded.

RUN --mount=type=cache,target=/root/.cache/pip \
    pip install \
        "matplotlib>=3.9.0" \
        "osmnx>=2.0.5" \
        "rasterio>=1.4.3" \
        "rioxarray>=0.18.2" \
        "opencv-python-headless>=4.11.0.86" \
        "scikit-image>=0.25.2" \
        "thefuzz>=0.22.1" \
        "elevation>=1.1.3" \
        "tqdm>=4.70.0" \
        "scikit-learn>=1.6.1" \
        "vsketch>=1.0.0" \
        "ipython" \
        "ipykernel>=6.29.5"

# 3. Prettymaps without optional dependencies

RUN --mount=type=cache,target=/root/.cache/pip \
    pip install prettymaps --no-deps

# --------------------------------------------------------------------------
# Stage 2: runtime
# --------------------------------------------------------------------------

FROM debian:bookworm-slim AS runtime

ARG DEBIAN_FRONTEND=noninteractive

ENV LANG=C.UTF-8 \
    LC_ALL=C.UTF-8 \
    PYTHONUNBUFFERED=1 \
    PATH="/opt/venv/bin:$PATH"

RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    rm -f /etc/apt/apt.conf.d/docker-clean && \
    apt-get update && apt-get install -y --no-install-recommends \
        ca-certificates \
        python3 \
        python3-socks \
        gdal-bin \
        libgdal32 \
        libgeos-c1v5 \
        libproj25 \
        libspatialindex6 \
        fonts-dejavu-core \
        libgl1 \
        libglib2.0-0 \
        libxkbcommon0 \
        libdbus-1-3 \
        libxcb-cursor0 \
        libxcb-xinerama0

# Copy Python virtual environment
COPY --from=builder /opt/venv /opt/venv

# Application
WORKDIR /work

VOLUME /work

COPY generate.py /opt/generate.py

# Prefer user-provided script from /work.
# Fall back to the bundled script.

ENTRYPOINT ["sh", "-c", "script=generate.py; if [ -f \"/work/$script\" ]; then exec python3 \"/work/$script\" \"$@\"; else exec python3 \"/opt/$script\" \"$@\"; fi", "--"]

CMD []