FROM python:3.11-slim

# System deps needed for native extensions:
#   gcc, libpq-dev  → asyncpg (Postgres driver)
#   libffi-dev      → cryptography
#   build-essential → general C compilation fallback
#   curl            → healthcheck probe
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc build-essential libpq-dev libffi-dev curl \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Upgrade pip + install wheel first so pre-built wheels are preferred
RUN pip install --no-cache-dir --upgrade pip wheel setuptools

# Install Python deps (separate layer for cache efficiency)
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# App source
COPY . .

# Make entrypoint executable
RUN chmod +x entrypoint.sh

# Non-root user for security
RUN useradd -m appuser && chown -R appuser /app
USER appuser

EXPOSE 8000

# Runs: alembic upgrade head → uvicorn
ENTRYPOINT ["./entrypoint.sh"]
