# Base image
FROM python:3.12-slim

ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONUNBUFFERED=1

# Install system dependencies
RUN apt-get update && apt-get install -y \
    libmagic1 gcc make nano jq \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# -------------------- Copy only files needed for pip --------------------
COPY mail-pdf-extractor/requirements.txt /app/requirements.txt
COPY peepdf-3 /app/peepdf-3

# -------------------- Install Python dependencies --------------------
RUN pip install --upgrade pip
RUN pip install /app/peepdf-3
RUN pip install -r /app/requirements.txt

# -------------------- Copy the rest of the project --------------------
COPY mail-pdf-extractor /app/mail-pdf-extractor
COPY fetch_email /app/fetch_email
COPY peepdf /app/peepdf
RUN chmod +x /app/fetch_email /app/peepdf
ENV PATH="/app:${PATH}"

# -------------------- Non-root user --------------------
ARG UID=1000
ARG GID=1000
RUN groupadd -g ${GID} user && \
    useradd -m -u ${UID} -g ${GID} user && \
    chown -R user:user /app
USER user

CMD ["bash"]