# Dockerfile Backend - FastAPI + Playwright
FROM python:3.11-slim

# Métadonnées
LABEL maintainer="gilles" \
      description="Backend suivi_produit - FastAPI + Playwright scraper"

# Variables d'environnement
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    POETRY_VERSION=1.7.1 \
    POETRY_HOME="/opt/poetry" \
    POETRY_VIRTUALENVS_CREATE=false \
    POETRY_NO_INTERACTION=1

# Dépendances système pour Playwright
RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    # Dépendances Playwright/Chromium
    libnss3 \
    libnspr4 \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libcups2 \
    libdrm2 \
    libdbus-1-3 \
    libxkbcommon0 \
    libatspi2.0-0 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxrandr2 \
    libgbm1 \
    libasound2 \
    libpango-1.0-0 \
    libcairo2 \
    fonts-liberation \
    && rm -rf /var/lib/apt/lists/*

# Installer Poetry
RUN curl -sSL https://install.python-poetry.org | python3 - \
    && ln -s /opt/poetry/bin/poetry /usr/local/bin/poetry

WORKDIR /app

# Copier les fichiers de dépendances
COPY pyproject.toml poetry.lock* ./

# Installer les dépendances Python
RUN poetry install --only main --no-root

# Installer Playwright et le navigateur Chromium
RUN playwright install chromium \
    && playwright install-deps chromium

# Copier le code source
COPY backend/ ./backend/

# Créer les répertoires de données
RUN mkdir -p backend/data backend/logs backend/data/raw

# Exposer le port
EXPOSE 8008

# Healthcheck
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:8008/health || exit 1

# Commande de démarrage
CMD ["uvicorn", "backend.app.main:app", "--host", "0.0.0.0", "--port", "8008"]
