FROM rust:1.91-bookworm

WORKDIR /app

# Install dependencies
RUN apt-get update && apt-get install -y \
    pkg-config \
    libssl-dev \
    curl \
    python3 \
    python3-pip \
    && pip3 install --break-system-packages huggingface_hub[cli] \
    && rm -rf /var/lib/apt/lists/*

# Copy and run model download script
COPY makima/sh/download-models.sh /app/download-models.sh
RUN chmod +x /app/download-models.sh

ARG MODEL_BASE_URL
ENV MODEL_BASE_URL=${MODEL_BASE_URL}
ENV MODELS_DIR=/app/models
ENV QWEN3_TTS_DIR=/app/models/qwen3-tts
RUN /app/download-models.sh echo "Models downloaded"

# Copy workspace files
COPY Cargo.toml Cargo.lock ./
COPY makima ./makima
COPY vendor ./vendor
COPY tools/stt-client ./tools/stt-client
COPY voices ./voices

# Build release binary
RUN cargo build --release --package makima --bin makima
RUN cp /app/target/release/makima /makima

# Clean up build artifacts to reduce image size
RUN rm -rf /app/target /app/makima/src /app/vendor /app/tools /usr/local/cargo/registry

# Set default environment variables
ENV PORT=8080
ENV RUST_LOG=makima=info,tower_http=info
ENV PARAKEET_MODEL_DIR=/app/models/parakeet-tdt-0.6b-v3
ENV PARAKEET_EOU_DIR=/app/models/realtime_eou_120m-v1-onnx
ENV SORTFORMER_MODEL_PATH=/app/models/diarization/diar_streaming_sortformer_4spk-v2.1.onnx
ENV QWEN3_TTS_DIR=/app/models/qwen3-tts

EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
    CMD curl -f http://localhost:${PORT}/api/v1/healthcheck || exit 1

CMD ["/makima", "server"]
