# ============================================================
# ipat-purchase – Xvfb + Chrome + noVNC + ipat_purchase.py
# Synology NAS / Lightsail / Windows Docker 向け
# ============================================================

FROM python:3.11-slim-bookworm

ENV DEBIAN_FRONTEND=noninteractive \
    DISPLAY=:99 \
    SCREEN_WIDTH=1280 \
    SCREEN_HEIGHT=900 \
    SCREEN_DEPTH=24 \
    VNC_PORT=5900 \
    NOVNC_PORT=6180 \
    IPAT_PORT=5200 \
    LANG=ja_JP.UTF-8 \
    LC_ALL=ja_JP.UTF-8 \
    TZ=Asia/Tokyo \
    PYTHONIOENCODING=utf-8

# ── システムパッケージ ──────────────────────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
    # 仮想ディスプレイ
    xvfb \
    # VNC サーバー
    x11vnc \
    # noVNC (Web VNC クライアント)
    novnc websockify \
    # 日本語フォント + ロケール
    fonts-noto-cjk locales \
    # Chrome依存
    wget gnupg2 unzip \
    # タイムゾーン
    tzdata \
    # その他
    curl procps \
    && sed -i '/ja_JP.UTF-8/s/^# //g' /etc/locale.gen \
    && locale-gen ja_JP.UTF-8 \
    && ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime \
    && echo "Asia/Tokyo" > /etc/timezone \
    && rm -rf /var/lib/apt/lists/*

# ── Google Chrome ──────────────────────────────────────────
RUN wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/google-chrome.gpg \
    && echo "deb [arch=amd64 signed-by=/usr/share/keyrings/google-chrome.gpg] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list \
    && apt-get update \
    && apt-get install -y --no-install-recommends google-chrome-stable \
    && rm -rf /var/lib/apt/lists/*

# ── Python パッケージ ───────────────────────────────────────
RUN pip install --no-cache-dir selenium flask

# ── 作業ディレクトリ ────────────────────────────────────────
WORKDIR /app

# ── Chromeプロファイル永続化用 ──────────────────────────────
RUN mkdir -p /data/chrome_profile /data/screenshots /data/logs /app/scripts

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

ENV IPAT_SCRIPT_DIR=/app/scripts

EXPOSE ${IPAT_PORT} ${NOVNC_PORT}

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

ENTRYPOINT ["/entrypoint.sh"]
