FROM python:3.11-slim

# Set environment variables
ENV PYTHONDONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PYTHONIOENCODING=UTF-8 \
    LANG=C.UTF-8 \
    LC_ALL=C.UTF-8 \
    TZ=UTC \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PYTHONPATH=/app

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    ffmpeg \
    libmagic1 \
    gcc \
    python3-dev \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Set work directory
WORKDIR /app

# Copy requirements and setup files first (for better layer caching)
COPY requirements.txt setup.py ./

# Install Python dependencies and the package in development mode
RUN pip install --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt && \
    pip install -e .

# Copy application code
COPY . .

# Create necessary directories with correct permissions
RUN mkdir -p /data /temp /config && \
    chmod -R 777 /data /temp /config

# Set volume mounts
VOLUME ["/data", "/temp", "/config"]

# Expose the port the app runs on
EXPOSE 6544

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

# Command to run the application
CMD ["python", "-m", "backend.spotspot"]
