#!/bin/bash
# Download Browser Whisper models for local development
# These are bundled during Docker build, but need manual download for dev

set -e

cd "$(dirname "$0")/.."
MODELS_DIR="public/models"

echo "🎙️  Downloading Browser Whisper models..."
echo ""

# Create directories
mkdir -p "$MODELS_DIR/Xenova/whisper-tiny.en/onnx"

# Download transformers.js (worker-compatible build)
echo "📦 Downloading transformers.js..."
curl -L --progress-bar -o "$MODELS_DIR/transformers.min.js" \
  "https://cdn.jsdelivr.net/npm/@xenova/transformers@2.0.0/dist/transformers.min.js"
echo "✅ transformers.min.js ($(du -h $MODELS_DIR/transformers.min.js | cut -f1))"
echo ""

# Download model config files
echo "📝 Downloading model configs..."
cd "$MODELS_DIR/Xenova/whisper-tiny.en"

curl -sL -o config.json \
  "https://huggingface.co/Xenova/whisper-tiny.en/resolve/main/config.json"
echo "✅ config.json ($(du -h config.json | cut -f1))"

curl -sL -o tokenizer.json \
  "https://huggingface.co/Xenova/whisper-tiny.en/resolve/main/tokenizer.json"
echo "✅ tokenizer.json ($(du -h tokenizer.json | cut -f1))"

curl -sL -o preprocessor_config.json \
  "https://huggingface.co/Xenova/whisper-tiny.en/resolve/main/preprocessor_config.json"
echo "✅ preprocessor_config.json ($(du -h preprocessor_config.json | cut -f1))"

curl -sL -o generation_config.json \
  "https://huggingface.co/Xenova/whisper-tiny.en/resolve/main/generation_config.json"
echo "✅ generation_config.json ($(du -h generation_config.json | cut -f1))"
echo ""

# Download ONNX models (large files)
echo "🧠 Downloading encoder model..."
curl -L --progress-bar -o onnx/encoder_model_quantized.onnx \
  "https://huggingface.co/Xenova/whisper-tiny.en/resolve/main/onnx/encoder_model_quantized.onnx"
echo "✅ encoder_model_quantized.onnx ($(du -h onnx/encoder_model_quantized.onnx | cut -f1))"
echo ""

echo "🧠 Downloading decoder model..."
curl -L --progress-bar -o onnx/decoder_model_merged_quantized.onnx \
  "https://huggingface.co/Xenova/whisper-tiny.en/resolve/main/onnx/decoder_model_merged_quantized.onnx"
echo "✅ decoder_model_merged_quantized.onnx ($(du -h onnx/decoder_model_merged_quantized.onnx | cut -f1))"
echo ""

# Show summary
cd - > /dev/null
echo "════════════════════════════════════════"
echo "✅ Browser Whisper models downloaded!"
echo "════════════════════════════════════════"
echo "Total size: $(du -sh $MODELS_DIR | cut -f1)"
echo ""
echo "Files:"
ls -lh "$MODELS_DIR/Xenova/whisper-tiny.en/" | tail -n +2
echo ""
ls -lh "$MODELS_DIR/Xenova/whisper-tiny.en/onnx/" | tail -n +2
echo ""
echo "Models are ready for use. Start server with: npm start"
