# Multi-stage build for aw8s - A k9s-style terminal UI for Argo Workflows
#
# USAGE:
#
# Build the image:
#   docker build -t aw8s:latest .
#
# Run with default help:
#   docker run -it --rm aw8s:latest
#
# Run with environment variables:
#   docker run -it --rm \
#     -e ARGO_SERVER=https://your-argo-server:2746 \
#     -e ARGO_TOKEN=your-token \
#     -e ARGO_NAMESPACE=your-namespace \
#     -e ARGO_INSECURE=false \
#     -e ARGO_NS_PREFIX=my-prefix \
#     aw8s:latest
#
# Run with command-line arguments:
#   docker run -it --rm aw8s:latest \
#     --server https://your-argo-server:2746 \
#     --token your-token \
#     --namespace your-namespace \
#     --insecure
#
# Available CLI options:
#   -s, --server      Argo Workflows server URL
#   -t, --token       Argo auth token
#   -n, --namespace   Initial namespace
#   -k, --insecure    Skip TLS certificate verification
#   -P, --ns-prefix   Namespace prefix filter for discovery
#
# Using docker-compose:
#   Create a .env file with your configuration, then run:
#   docker-compose run --rm aw8s
#
# NOTES:
# - The container requires interactive terminal (-it) to display the TUI
# - Use --rm to automatically remove the container after exit
# - Configure via environment variables or command-line arguments
# - Environment variables: ARGO_SERVER, ARGO_TOKEN, ARGO_NAMESPACE,
#   ARGO_INSECURE, ARGO_NS_PREFIX

# Build stage
FROM python:3.12-slim AS builder

WORKDIR /build

# Install build dependencies
RUN pip install --no-cache-dir hatchling

# Copy project files
COPY pyproject.toml uv.lock* ./
COPY src/ ./src/
COPY tests/ ./tests/

# Build the wheel
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /build/wheels .

# Runtime stage
FROM python:3.12-slim

# Set environment variables
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

# Create a non-root user
RUN useradd --create-home --shell /bin/bash aw8s

WORKDIR /home/aw8s

# Copy the built wheel from builder stage
COPY --from=builder /build/wheels/*.whl /tmp/

# Install the application
RUN pip install --no-cache-dir /tmp/*.whl && \
    rm -rf /tmp/*.whl

# Switch to non-root user
USER aw8s

# Set the entrypoint to the aw8s command
ENTRYPOINT ["aw8s"]

# Default command (can be overridden)
CMD ["--help"]
