# Mirrors memory-service/Dockerfile — own Railway service, root dir gateway/.
FROM python:3.12-slim

WORKDIR /app

RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates && rm -rf /var/lib/apt/lists/*

# Voice deps (Pocket TTS/torch) install by default — this is the prod voice
# gateway. Set --build-arg INSTALL_VOICE=0 for a light chat-only image.
ARG INSTALL_VOICE=1

COPY requirements.txt requirements-voice.txt ./
# Install the light base first. For voice, install CPU-only torch explicitly
# (the default PyPI torch pulls multi-GB CUDA wheels that blow Railway's build
# limits) BEFORE pocket-tts, and keep the CPU index as an extra index so any
# transitive torch pin also resolves to a CPU wheel. This box has no GPU.
RUN pip install --no-cache-dir -r requirements.txt && \
    if [ "$INSTALL_VOICE" = "1" ]; then \
        pip install --no-cache-dir --index-url https://download.pytorch.org/whl/cpu torch && \
        pip install --no-cache-dir --extra-index-url https://download.pytorch.org/whl/cpu -r requirements-voice.txt; \
    fi

# Jarvis voiceprints ship in the image (deploy context copy at gateway/voices/,
# gitignored — canonical files live at repo-root voices/). Set VOICE_DIR=/app/voices.
COPY . .

# Railway sets $PORT; default 8080 for local runs. MUST go through `sh -c` so
# ${PORT} is expanded (Railway execs the start command without a shell, so a bare
# `--port $PORT` arrives literal and uvicorn rejects it). `python -u -m uvicorn`
# keeps logs unbuffered and doesn't depend on a bare `uvicorn` being on PATH.
ENV PORT=8080 PYTHONUNBUFFERED=1
CMD ["sh", "-c", "python -u -m uvicorn main:app --host 0.0.0.0 --port ${PORT} --timeout-keep-alive 75"]
