FROM python:3.12-slim

# System dependencies for psycopg2, OCR, and PDF processing
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    libpq-dev \
    tesseract-ocr \
    poppler-utils \
    && rm -rf /var/lib/apt/lists/*

# Create and activate virtualenv
ENV VIRTUAL_ENV=/opt/venv
RUN python -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

WORKDIR /app

# Install Python dependencies from pyproject.toml
# Copy only the dependency manifest first to leverage Docker layer cache
COPY pyproject.toml ./
# Create a minimal package layout so `pip install -e .` doesn't fail before
# source is copied; then do the real install.
RUN mkdir -p app && touch app/__init__.py
RUN pip install --upgrade pip && pip install -e ".[dev]"

# Copy the rest of the application source
COPY . .

EXPOSE 8000

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