# ============================================================
# Acupedia Platform API — Backend Dockerfile
# Python 3.12 + FastAPI + Uvicorn + Bootstrap CLI
# ============================================================

FROM python:3.12-slim AS backend

WORKDIR /app

# Install system dependencies for asyncpg, psycopg, and psql
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    libpq-dev \
    postgresql-client \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Prefer IPv4 over unrouted IPv6 in glibc to prevent PyPI connection timeouts
RUN echo "precedence ::ffff:0:0/96 100" >> /etc/gai.conf

# Use Astral's uv package manager for fast, parallel, cached installations
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
ENV UV_HTTP_TIMEOUT=300

# Copy pyproject.toml first
COPY pyproject.toml ./
COPY README.md ./

# Copy schema and source code
COPY docs/accupedia_db_v5.sql /app/schema/accupedia_db_v5.sql
COPY app ./app

# Copy git info metadata if generated on host
COPY .git_api_info* /app/.git_api_info

# Install project and CLI scripts into system PATH using persistent BuildKit cache
RUN --mount=type=cache,target=/root/.cache/uv uv pip install --system -e .

EXPOSE 8000

HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
    CMD curl -f http://localhost:8000/ || exit 1

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

