# Base runtime image for the FastAPI backend
FROM python:3.11-slim-bookworm

# Python runtime settings:
# - disable .pyc generation
# - flush logs directly to stdout/stderr
# - avoid keeping pip cache in the image
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1

# Set the repository root inside the container
WORKDIR /app

# Install system dependencies required by:
# - geospatial Python packages (GDAL / PROJ / GEOS)
# - native extension builds
# - Docker CLI usage from inside the backend container
# - curl for the container health check
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        build-essential \
        gcc \
        g++ \
        pkg-config \
        ca-certificates \
        python3-dev \
        curl \
        docker.io \
        gdal-bin \
        libgdal-dev \
        libproj-dev \
        proj-bin \
        proj-data \
        libgeos-dev \
    && rm -rf /var/lib/apt/lists/*

# Copy dependency definition first to improve Docker layer caching
COPY requirements.txt ./

# Install Python dependencies
RUN python -m pip install --upgrade pip setuptools wheel \
    && pip install -r requirements.txt

# Copy the application entrypoint and backend source code
COPY main.py ./
COPY backend ./backend

# Expose the FastAPI port
EXPOSE 8000

# Lightweight liveness check for container health monitoring
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
    CMD curl -fsS http://127.0.0.1:8000/health || exit 1

# Start the FastAPI application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]