# 1. Basis-Image
FROM python:3.8-slim

# 2. System-Abhängigkeiten installieren (für Chrome etc.)
RUN apt-get update && apt-get install -y \
    wget \
    unzip \
    gnupg \
    ca-certificates \
    fonts-liberation \
    libasound2 \
    libatk-bridge2.0-0 \
    libatk1.0-0 \
    libcups2 \
    libdbus-1-3 \
    libgdk-pixbuf2.0-0 \
    libnspr4 \
    libnss3 \
    libx11-xcb1 \
    libxcomposite1 \
    libxdamage1 \
    libxrandr2 \
    xdg-utils \
    --no-install-recommends && \
    rm -rf /var/lib/apt/lists/*

# 3. Google Chrome Stable installieren
RUN wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add - && \
    echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list && \
    apt-get update && \
    apt-get install -y google-chrome-stable --no-install-recommends && \
    rm -rf /var/lib/apt/lists/*

# 4. Passenden Chromedriver installieren (FINALE METHODE MIT VERSIONSABGLEICH)
# Liest die installierte Chrome-Version aus und sucht den exakt passenden Treiber.
RUN \
    export CHROME_VERSION=$(google-chrome --version | cut -f 3 -d ' ') && \
    DRIVER_URL=$(wget -O- --no-check-certificate "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json" | python -c "import os, json, sys; \
        versions = json.load(sys.stdin)['versions']; \
        target_version = os.environ['CHROME_VERSION']; \
        v_entry = next(v for v in versions if v['version'] == target_version); \
        url = next(d['url'] for d in v_entry['downloads']['chromedriver'] if d['platform'] == 'linux64'); \
        print(url)") && \
    wget --no-check-certificate -q "${DRIVER_URL}" -O /tmp/chromedriver.zip && \
    unzip /tmp/chromedriver.zip -d /usr/local/bin/ && \
    mv /usr/local/bin/chromedriver-linux64/chromedriver /usr/local/bin/ && \
    rm -rf /tmp/chromedriver.zip /usr/local/bin/chromedriver-linux64 && \
    chmod +x /usr/local/bin/chromedriver

# 5. App-Verzeichnis einrichten
WORKDIR /app

# 6. Anwendungsdateien kopieren
COPY . .

# 7. Python-Abhängigkeiten installieren
RUN pip3 install --no-cache-dir -r requirements.txt

# 8. Standard-Startbefehl festlegen
CMD ["python3", "scrape_fotograf.py"]