# =============================================================================
# VOICE AGENT DOCKERFILE
# =============================================================================
# Multi-stage build for optimized voice agent container

# =============================================================================
# BUILD STAGE
# =============================================================================
FROM python:3.11-slim AS builder

# Set build arguments
ARG DEBIAN_FRONTEND=noninteractive

# Install build dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    python3-dev \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Create virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# Copy requirements and install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# =============================================================================
# RUNTIME STAGE
# =============================================================================
FROM python:3.11-slim AS runtime

# Set environment variables
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PATH="/opt/venv/bin:$PATH" \
    PYTHONPATH="/app/src"

# Create application directory
WORKDIR /app

# Copy virtual environment and application code
COPY --from=builder /opt/venv /opt/venv
COPY src/ ./src/
COPY start_voice.sh ./

# Create non-root user for security
RUN groupadd -r appgroup && \
    useradd -r -g appgroup -u 1000 appuser && \
    chown -R appuser:appgroup /app

# Switch to non-root user
USER appuser

# Expose port
EXPOSE 8082

# Health check - HTTP-based using built-in health endpoint
# Note: When deployed to Kubernetes, the liveness/readiness probes in k8s-manifests.tf
# will be used instead of this Docker HEALTHCHECK. However, this is still useful for:
# - Local development and testing with docker run
# - Non-Kubernetes deployments (Docker Compose, etc.)
# - Consistency validation (both should check the same endpoint)
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:8082/health || exit 1

# Set the entrypoint with correct Python path
CMD ["./start_voice.sh"]

# =============================================================================
# METADATA
# =============================================================================
LABEL maintainer="EverKind Infrastructure Team"
LABEL description="EverKind Voice Agent - LiveKit-based voice processing service"
LABEL version="1.0.0"
LABEL org.opencontainers.image.source="https://github.com/everkind-inc/everkind-api"
LABEL org.opencontainers.image.description="Voice agent for real-time speech processing"
LABEL org.opencontainers.image.licenses="Proprietary"
