# Sentiment Engine Dockerfile
# Multi-stage build for production

# =============================================================================
# Build stage
# =============================================================================
FROM python:3.12-slim as builder

WORKDIR /app

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

# Install Python dependencies
COPY pyproject.toml .
RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \
    pip install --no-cache-dir .

# =============================================================================
# Runtime stage
# =============================================================================
FROM python:3.12-slim

WORKDIR /app

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

# Copy Python packages from builder
COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin

# Copy application code
COPY src/ ./src/
COPY config/ ./config/
COPY prefect_flows/ ./prefect_flows/
COPY scripts/ ./scripts/

# Create non-root user
RUN useradd -m -u 1000 sentiment && chown -R sentiment:sentiment /app
USER sentiment

# Environment
ENV PYTHONPATH=/app/src
ENV SENTIMENT_CONFIG=/app/config/settings.yaml

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
    CMD curl -f http://localhost:8080/health || exit 1

# Expose ports
EXPOSE 8080 9090

# Entry point
ENTRYPOINT ["python", "-m", "sentiment_engine.main"]
