#!/bin/bash

# Deploy Firestore indexes for gamification system
# This script deploys composite indexes to optimize query performance

echo "=========================================="
echo "🔥 Deploying Firestore Indexes"
echo "=========================================="
echo ""

# Check if Firebase CLI is installed
if ! command -v firebase &> /dev/null; then
    echo "❌ Firebase CLI not found"
    echo "Install with: npm install -g firebase-tools"
    exit 1
fi

# Check if logged in
if ! firebase projects:list &> /dev/null; then
    echo "❌ Not logged in to Firebase"
    echo "Login with: firebase login"
    exit 1
fi

echo "📋 Deploying indexes from firebase/firestore-gamification.indexes.json"
echo ""

# Deploy indexes
firebase deploy --only firestore:indexes --config firebase/firestore-gamification.indexes.json

if [ $? -eq 0 ]; then
    echo ""
    echo "=========================================="
    echo "✅ Indexes deployed successfully!"
    echo "=========================================="
    echo ""
    echo "⏳ Index build progress:"
    echo "   1. Go to Firebase Console"
    echo "   2. Select your project"
    echo "   3. Navigate to Firestore Database > Indexes"
    echo "   4. Wait for all indexes to show 'Enabled'"
    echo ""
    echo "📊 Expected performance gains:"
    echo "   - Active season query: 10x faster"
    echo "   - Quest list query: 5x faster"
    echo "   - Season leaderboard: 20x faster"
    echo "   - Quest templates: 3x faster"
    echo ""
else
    echo ""
    echo "❌ Index deployment failed"
    echo "Check Firebase Console for errors"
    exit 1
fi
