# syntax=docker/dockerfile:1.7
#
# Multi-target build:
#   target=runner               — lean image для backend (uvicorn) и beat. ~1.4 ГБ.
#   target=runner-with-chromium — image для worker (Playwright fetch). ~1.6 ГБ.
#
# Local: docker compose сам выбирает target по сервису (см. docker-compose.yml).
# Prod:  CI пушит 2 GHCR-образа: gendesign-backend (lean) и gendesign-worker (chromium).

# ---- builder ----
FROM python:3.12-slim AS builder

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    UV_COMPILE_BYTECODE=1 \
    UV_LINK_MODE=copy

RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libpq-dev \
    libgdal-dev \
    libproj-dev \
    libgeos-dev

RUN pip install --no-cache-dir uv

WORKDIR /app

COPY pyproject.toml uv.lock* ./
# uv-кеш переживает между билдами: при неизменном lock'е sync ~5 сек вместо 1-2 мин.
RUN --mount=type=cache,target=/root/.cache/uv \
    if [ -f uv.lock ]; then uv sync --frozen --no-dev; else uv sync --no-dev; fi

COPY app ./app
COPY alembic.ini ./
COPY alembic ./alembic


# ---- runner (lean — без Chromium) ----
FROM python:3.12-slim AS runner

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PATH="/app/.venv/bin:$PATH"

RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    apt-get update && apt-get install -y --no-install-recommends \
    libpq5 \
    libgdal36 \
    libproj25 \
    libgeos-c1t64 \
    libcairo2 \
    libpango-1.0-0 \
    libpangoft2-1.0-0 \
    fonts-dejavu-core \
    curl \
    && useradd --create-home --uid 1000 app

WORKDIR /app

COPY --from=builder --chown=app:app /app/.venv /app/.venv
COPY --from=builder --chown=app:app /app/app /app/app
COPY --from=builder --chown=app:app /app/alembic.ini /app/alembic.ini
COPY --from=builder --chown=app:app /app/alembic /app/alembic

USER app

EXPOSE 8000

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]


# ---- runner-with-chromium (для worker) ----
# Наследуется от lean runner, добавляет Chromium-libs + Playwright-binary (~200 MB).
FROM runner AS runner-with-chromium

USER root

RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    apt-get update && apt-get install -y --no-install-recommends \
    # Chromium runtime libs (раньше заходили через `playwright install --with-deps`)
    libnss3 \
    libnspr4 \
    libdbus-1-3 \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libcups2 \
    libdrm2 \
    libxkbcommon0 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxrandr2 \
    libgbm1 \
    libasound2t64 \
    libatspi2.0-0

# Playwright Chromium binary под root → shared dir, чтобы и app-user мог использовать.
ENV PLAYWRIGHT_BROWSERS_PATH=/opt/playwright
RUN mkdir -p $PLAYWRIGHT_BROWSERS_PATH \
    && /app/.venv/bin/playwright install chromium \
    && chmod -R a+rX $PLAYWRIGHT_BROWSERS_PATH

USER app
