FROM python:3.11-slim

WORKDIR /app

# ========================================
# ARG para recibir la versión desde CI/CD
# ========================================
ARG APP_VERSION="dev"
ENV APP_VERSION=${APP_VERSION}

# Instalar dependencias del sistema
RUN apt-get update && apt-get install -y \
    gcc \
    postgresql-client \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Instalar dependencias para wkhtmltopdf y reportes PDF
RUN apt-get update \
    && apt-get install -y \
        xvfb \
        fontconfig \
        fonts-dejavu-core \
        libfontconfig1 \
        libxrender1 \
        libxtst6 \
        libxi6 \
        libxrandr2 \
        ca-certificates \
        libjpeg62-turbo \
        libpng16-16 \
    && rm -rf /var/lib/apt/lists/*

# Instalar wkhtmltopdf binario oficial con soporte para footers/headers
RUN curl -k -L -o /tmp/wkhtmltox.deb https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-3/wkhtmltox_0.12.6.1-3.bookworm_amd64.deb \
    && apt-get update \
    && apt-get install -y /tmp/wkhtmltox.deb \
    && rm /tmp/wkhtmltox.deb \
    && rm -rf /var/lib/apt/lists/* \
    && wkhtmltopdf --version


# Copiar requirements
COPY requirements.txt .

# Instalar dependencias Python
RUN pip install --no-cache-dir -r requirements.txt

# Copiar código
COPY . .

# Exponer puerto
EXPOSE 8000

# Comando por defecto
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
