# PyStator Dockerfile
# Multi-stage build for API, Worker, and UI services
#
# Usage:
#   docker build --target production --build-arg SERVICE=api -t pystator-api .
#   docker build --target production --build-arg SERVICE=worker -t pystator-worker .
#   docker build --target production-ui --build-arg SERVICE=ui -t pystator-ui .

# =============================================================================
# STAGE 1: Node.js builder (UI static export)
# =============================================================================
FROM node:20-alpine AS node-builder

WORKDIR /ui

# Copy package files first for layer caching
COPY src/pystator/ui/package.json src/pystator/ui/package-lock.json* ./
RUN npm ci

# Copy UI source and build static export
COPY src/pystator/ui/ ./
ENV NODE_ENV=production
RUN npm run build
# Next.js with output: 'export' produces out/ directory

# =============================================================================
# STAGE 2: Python builder
# =============================================================================
FROM python:3.13-slim AS python-builder

ARG SERVICE=api

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1

# System build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libpq-dev \
    && rm -rf /var/lib/apt/lists/*

# Create virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
RUN pip install --upgrade pip setuptools wheel

WORKDIR /build

# Copy project metadata and source
COPY pyproject.toml setup.py README.md ./
COPY src/ ./src/

# Install based on SERVICE arg
RUN if [ "$SERVICE" = "api" ]; then \
      pip install --no-cache-dir ".[api,postgres]"; \
    elif [ "$SERVICE" = "worker" ]; then \
      pip install --no-cache-dir ".[worker,postgres]"; \
    elif [ "$SERVICE" = "ui" ]; then \
      pip install --no-cache-dir ".[ui]"; \
    else \
      pip install --no-cache-dir "."; \
    fi

# =============================================================================
# STAGE 3a: Production (API / Worker)
# =============================================================================
FROM python:3.13-slim AS production

ARG SERVICE=api

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PATH="/opt/venv/bin:$PATH" \
    SERVICE=${SERVICE}

# Runtime system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    libpq5 \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Copy virtual environment from builder
COPY --from=python-builder /opt/venv /opt/venv

# Create non-root user
RUN groupadd -r appuser && useradd -r -g appuser -d /app appuser

WORKDIR /app

# Copy application source
COPY --chown=appuser:appuser src/ ./src/
COPY --chown=appuser:appuser pyproject.toml setup.py ./

# Copy entrypoint script
COPY --chown=appuser:appuser docker-entrypoint.sh ./
RUN chmod +x docker-entrypoint.sh

# Create necessary directories
RUN mkdir -p /app/logs /app/data && chown -R appuser:appuser /app

USER appuser

EXPOSE 8004 3004

HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8004/health')"

ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["api"]

# =============================================================================
# STAGE 3b: Production UI (includes built static assets)
# =============================================================================
FROM production AS production-ui

# Copy built Next.js static export from node-builder
COPY --from=node-builder --chown=appuser:appuser /ui/out/ ./src/pystator/ui/static/

ENV SERVICE=ui

HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:3004/health')"

CMD ["ui"]
