FROM continuumio/miniconda3:latest

WORKDIR /app

# System deps for pytesseract (OCR) and pdfplumber
RUN apt-get update && apt-get install -y --no-install-recommends \
    tesseract-ocr \
    libgl1 \
    && rm -rf /var/lib/apt/lists/*

# Install circuitforge-core from sibling directory (compose sets context: ..)
COPY circuitforge-core/ ./circuitforge-core/
RUN conda run -n base pip install --no-cache-dir -e "./circuitforge-core[pdf,vector]"

# Create pagepiper conda env
COPY pagepiper/environment.yml .
RUN conda env create -f environment.yml

COPY pagepiper/ ./pagepiper/

# Remove gitignored secrets — defence-in-depth
RUN rm -f /app/pagepiper/.env /app/pagepiper/config/llm.yaml

# Install cf-core into pagepiper env + the app itself
RUN conda run -n pagepiper pip install --no-cache-dir -e "/app/circuitforge-core[pdf,vector]"
WORKDIR /app/pagepiper
RUN conda run -n pagepiper pip install --no-cache-dir -e .

EXPOSE 8522
CMD ["conda", "run", "--no-capture-output", "-n", "pagepiper", \
     "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8522"]
