# =============================================================================
# SKEEN CRM Agent - Production Dockerfile
# =============================================================================
FROM python:3.12-slim AS builder

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

# Install uv (modern Python package manager)
RUN pip install --no-cache-dir uv

# Set workdir
WORKDIR /app

# Copy dependency definitions
COPY pyproject.toml ./

# Create virtual environment and install dependencies
RUN uv venv .venv && \
    uv pip install --no-cache -r pyproject.toml

# =============================================================================
# Production stage
# =============================================================================
FROM python:3.12-slim AS production

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

# Create non-root user
RUN groupadd -r skeen && useradd -r -g skeen skeen

WORKDIR /app

# Copy virtual environment from builder
COPY --from=builder /app/.venv /app/.venv
ENV PATH="/app/.venv/bin:$PATH"

# Copy application code
COPY --chown=skeen:skeen src/ ./src/
COPY --chown=skeen:skeen alembic/ ./alembic/
COPY --chown=skeen:skeen alembic.ini ./

# Switch to non-root user
USER skeen

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

EXPOSE 8000
