FROM python:3.13-slim

# Install FFmpeg and system dependencies
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        ffmpeg \
        ca-certificates \
        && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Set Python environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1

# Set working directory
WORKDIR /app

# ------------------------------------------------------------
# Copy and install Python dependencies
# ------------------------------------------------------------
COPY web/backend/requirements-web.txt .
RUN pip install --no-cache-dir -r requirements-web.txt

# ------------------------------------------------------------
# Copy the backend application code
# ------------------------------------------------------------
COPY web/backend /app/backend

# ------------------------------------------------------------
# Copy and install the audio_splitter package
# ------------------------------------------------------------
COPY audio_splitter /app/audio_splitter
COPY setup.py pyproject.toml README.md /app/

# Install audio_splitter as a package
RUN pip install --no-cache-dir /app

# ------------------------------------------------------------
# Create a non-root user
# ------------------------------------------------------------
RUN addgroup --system --gid 1000 appgroup && \
    adduser --system --uid 1000 --ingroup appgroup appuser && \
    chown -R appuser:appgroup /app

USER appuser

# ------------------------------------------------------------
# Expose port and start
# ------------------------------------------------------------
EXPOSE 8000

# PYTHONPATH is already /app by default because we set WORKDIR /app
CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8000"]
