# Wakelai Voice AI Platform - Dockerfile
FROM python:3.12-slim

WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
    gcc \
    postgresql-client \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements first for caching
COPY requirements.txt .

# Install Python dependencies including gunicorn
RUN pip install --no-cache-dir -r requirements.txt gunicorn

# Copy application files
COPY app/ ./app/
COPY config/ ./config/
COPY voice_engine/ ./app/voice_engine/
COPY migrations/ ./migrations/
COPY run.py .

# Create necessary directories
RUN mkdir -p logs voice_engine/instances

# Set environment variables
ENV FLASK_APP=run.py
ENV FLASK_ENV=production
ENV PYTHONUNBUFFERED=1

# Expose port
EXPOSE 4000

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

# Run the application
CMD ["gunicorn", "--workers", "4", "--threads", "2", "--bind", "0.0.0.0:4000", "--timeout", "120", "--access-logfile", "-", "--error-logfile", "-", "run:app"]
