# Build stage
FROM python:3.11-slim AS builder
WORKDIR /app
ENV PYTHONPATH=/app

# Install system dependencies for building
RUN apt-get update && apt-get install -y gcc && rm -rf /var/lib/apt/lists/*

# Copy project files for building
COPY pyproject.toml .
COPY . .

RUN pip install --no-cache-dir build && python -m build --wheel

# Runtime stage
FROM python:3.11-slim
WORKDIR /app
ENV PYTHONPATH=/app

RUN apt-get update && apt-get install -y libpq-dev && rm -rf /var/lib/apt/lists/*

# Install the built package from builder
COPY --from=builder /app/dist/*.whl /tmp/
RUN pip install --no-cache-dir /tmp/*.whl

# Install additional runtime dependencies for demo.py
RUN pip install --no-cache-dir \
    uvicorn[standard]==0.29.0 \
    python-multipart==0.0.20 \
    httpx==0.27.0 \
    opentelemetry-api==1.34.1 \
    opentelemetry-sdk==1.34.1 \
    opentelemetry-exporter-otlp-proto-grpc==1.34.1 \
    opentelemetry-exporter-prometheus==0.55b1 \
    opentelemetry-instrumentation-fastapi==0.55b1 \
    opentelemetry-instrumentation-httpx==0.55b1 \
    protobuf==5.28.3

# Copy the application code if needed
COPY ./amqp ./amqp
COPY ./otdemo ./otdemo
COPY ./demo.py .

# Create directory for uploaded files
RUN mkdir -p /tmp/clevermicro_documents

EXPOSE 8000 8001

CMD ["uvicorn", "demo:app", "--host", "0.0.0.0", "--port", "8000"]
