# ============================================================
# mercari-scraper – Xvfb + Firefox + noVNC + mercari_server.py
# Synology NAS (x86_64) 向け
# ============================================================

FROM python:3.11-slim-bookworm

ENV DEBIAN_FRONTEND=noninteractive \
    DISPLAY=:99 \
    SCREEN_WIDTH=1920 \
    SCREEN_HEIGHT=1080 \
    SCREEN_DEPTH=24 \
    VNC_PORT=5900 \
    NOVNC_PORT=6080 \
    MERCARI_PORT=5100 \
    LANG=ja_JP.UTF-8 \
    LC_ALL=ja_JP.UTF-8 \
    PYTHONIOENCODING=utf-8

# ── システムパッケージ ──────────────────────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
    # Firefox
    firefox-esr \
    # 仮想ディスプレイ
    xvfb \
    # VNC サーバー
    x11vnc \
    # noVNC (Web VNC クライアント)
    novnc websockify \
    # 日本語フォント + ロケール
    fonts-noto-cjk locales \
    # その他
    curl procps \
    && sed -i '/ja_JP.UTF-8/s/^# //g' /etc/locale.gen \
    && locale-gen ja_JP.UTF-8 \
    && rm -rf /var/lib/apt/lists/*

# ── geckodriver ─────────────────────────────────────────────
ARG GECKODRIVER_VERSION=0.35.0
RUN curl -fsSL "https://github.com/mozilla/geckodriver/releases/download/v${GECKODRIVER_VERSION}/geckodriver-v${GECKODRIVER_VERSION}-linux64.tar.gz" \
    | tar -xz -C /usr/local/bin/ \
    && chmod +x /usr/local/bin/geckodriver

# ── Python パ��ケージ ───────────────────────────────────────
RUN pip install --no-cache-dir selenium requests

# ── 作業ディレクトリ ────────────────────────────────────────
# mercari_server.py はボリュームマウントで /app に配置
WORKDIR /app

# ── 起動スクリプト ──────────────────────────────────────────
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

EXPOSE ${MERCARI_PORT} ${NOVNC_PORT}

HEALTHCHECK --interval=30s --timeout=10s --retries=3 --start-period=60s \
    CMD curl -f http://localhost:${MERCARI_PORT}/health || exit 1

ENTRYPOINT ["/entrypoint.sh"]
