FROM python:3.11-slim

# Avoid apt interactive prompts (debconf frontend) during image build.
ENV DEBIAN_FRONTEND=noninteractive

# Install system + build dependencies.
# libreoffice-calc + poppler handle Excel→PDF conversions (matches local Windows Excel COM flow).
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    gcc \
    curl \
    ca-certificates \
    libreoffice-calc-nogui \
    fonts-dejavu-core \
 && rm -rf /var/lib/apt/lists/*

# Cap glibc malloc arenas — keeps the uvicorn + libreoffice process under predictable RSS.
ENV MALLOC_ARENA_MAX=2

WORKDIR /app/backend

# Copy requirements first so the dependency layer is cached.
COPY backend/requirements.txt ./

# Install Python dependencies.
RUN pip install --upgrade pip setuptools wheel \
 && pip install --no-cache-dir -r requirements.txt

# Copy backend code.
COPY backend/ ./

# Claim Excel templates live at repo root (claim_excel_service.TEMPLATE_DIR → /app/Claim Template).
COPY ["Claim Template/", "/app/Claim Template/"]

# Persistent upload + attachment directories (mounted as volumes in compose).
RUN mkdir -p /app/backend/claim_files /app/backend/claim_attachments /app/backend/uploads

EXPOSE 8002

CMD ["python", "-m", "uvicorn", "server:app", "--host", "0.0.0.0", "--port", "8002"]
