# ============================================================
# All-in-One Docker image (no Playwright, <200MB target)
# ============================================================
# Build:  docker build -f docker/all-in-one.Dockerfile -t superset-tools:latest .
# Run:    docker run -p 8000:8000 --env-file backend/.env superset-tools:latest
#
# Stages:
#   1. frontend-builder — node:20-alpine, npm ci + build, discarded
#   2. runtime — python:3.11-slim, backend deps (no playwright),
#      copies frontend build, uvicorn serves both SPA + API on :8000

# ── Stage 1: Build Frontend ────────────────────────────────
FROM node:20-alpine AS frontend-builder
ARG APP_VERSION=0.0.0
ENV APP_VERSION=${APP_VERSION}

WORKDIR /app/frontend

# Install deps (layer cache when package.json unchanged)
COPY frontend/package.json frontend/package-lock.json* ./
RUN npm ci --prefer-offline --no-audit --no-fund --legacy-peer-deps

# Build frontend
COPY frontend/ ./
RUN npm run build

# ── Stage 2: Runtime ───────────────────────────────────────
FROM python:3.11-slim AS runtime

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    BACKEND_PORT=8000 \
    DEBIAN_FRONTEND=noninteractive

WORKDIR /app

# System deps (git for GitPython, curl for healthcheck)
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        curl \
        git \
    && rm -rf /var/lib/apt/lists/*

# Python deps (NO playwright — saves ~350MB)
COPY backend/requirements-docker.txt /app/backend/requirements-docker.txt
RUN pip install --no-cache-dir -r /app/backend/requirements-docker.txt

# Backend source
COPY backend/ /app/backend/

# Frontend build (from stage 1)
COPY --from=frontend-builder /app/frontend/build /app/frontend/build

# Entrypoint
COPY docker/backend.entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh

WORKDIR /app/backend

EXPOSE 8000

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD curl -sf http://localhost:8000/ || exit 1

ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["python", "-m", "uvicorn", "src.app:app", "--host", "0.0.0.0", "--port", "8000"]
