#!/bin/bash
# Build and deploy Soprano + RVC Docker containers

set -e  # Exit on error

echo "🎤 Building Soprano + RVC Docker Containers"
echo "============================================"

# Check prerequisites
echo ""
echo "📋 Checking prerequisites..."

if ! command -v docker &> /dev/null; then
    echo "❌ Docker not found. Please install Docker first."
    exit 1
fi

if ! command -v docker-compose &> /dev/null; then
    echo "❌ docker-compose not found. Please install docker-compose first."
    exit 1
fi

# Check NVIDIA runtime
if ! docker run --rm --gpus all nvidia/cuda:11.8.0-base-ubuntu22.04 nvidia-smi &> /dev/null; then
    echo "⚠️  Warning: NVIDIA Container Toolkit test failed. Soprano container may not work."
    echo "   Install with: sudo apt-get install nvidia-container-toolkit"
fi

# Check ROCm devices
if [ ! -e /dev/kfd ] || [ ! -e /dev/dri ]; then
    echo "⚠️  Warning: ROCm devices not found. RVC container may not work."
    echo "   Ensure ROCm drivers are installed and /dev/kfd, /dev/dri exist."
fi

# Check required files
echo ""
echo "📁 Checking required files..."

if [ ! -d "soprano" ]; then
    echo "❌ soprano/ directory not found."
    echo "   Clone with: git clone https://github.com/ekwek1/soprano.git"
    exit 1
fi

if [ ! -f "soprano_server.py" ]; then
    echo "❌ soprano_server.py not found."
    exit 1
fi

if [ ! -d "Retrieval-based-Voice-Conversion-WebUI" ]; then
    echo "❌ RVC WebUI directory not found."
    echo "   Clone with: git clone https://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI.git"
    exit 1
fi

if [ ! -f "soprano_rvc_api.py" ]; then
    echo "❌ soprano_rvc_api.py not found."
    exit 1
fi

if [ ! -f "soprano_rvc_config.json" ]; then
    echo "❌ soprano_rvc_config.json not found."
    exit 1
fi

# Check model files
if [ ! -f "models/MikuAI_e210_s6300.pth" ]; then
    echo "⚠️  Warning: RVC model not found at models/MikuAI_e210_s6300.pth"
fi

if [ ! -f "models/added_IVF512_Flat_nprobe_1_MikuAI_v2.index" ]; then
    echo "⚠️  Warning: RVC index not found at models/added_IVF512_Flat_nprobe_1_MikuAI_v2.index"
fi

echo "✅ Prerequisites check complete"

# Build containers
echo ""
echo "🔨 Building Docker containers..."
echo ""

# Soprano container
echo "Building Soprano container (CUDA + Python 3.11)..."
if docker-compose build soprano; then
    echo "✅ Soprano container built successfully"
else
    echo "❌ Soprano container build failed"
    exit 1
fi

echo ""

# RVC container
echo "Building RVC container (ROCm + Python 3.10)..."
if docker-compose build rvc; then
    echo "✅ RVC container built successfully"
else
    echo "❌ RVC container build failed"
    exit 1
fi

echo ""
echo "============================================"
echo "✅ Build complete!"
echo ""
echo "Next steps:"
echo "  1. Review GPU device IDs in docker-compose.yml"
echo "  2. Start services: docker-compose up -d"
echo "  3. Check health: curl http://localhost:8765/health"
echo "  4. Test API: curl -X POST http://localhost:8765/api/speak \\"
echo "                    -H 'Content-Type: application/json' \\"
echo "                    -d '{\"text\": \"Hello, I am Miku!\"}' \\"
echo "                    -o test.wav"
echo ""
echo "For more information, see DOCKER_SETUP.md"
