#!/bin/bash
# POI Confidence Fix - Deployment and Seeding Commands
# Date: 2025-11-25

set -e

echo "======================================"
echo "🚀 POI CONFIDENCE FIX - DEPLOYMENT"
echo "======================================"
echo ""

# ====================
# STEP 1: BUILD
# ====================
echo "📦 STEP 1: Building backend..."
cd /Users/floriansola/Documents/MyRoadTrip
pnpm build

echo ""
echo "✅ Build complete!"
echo ""

# ====================
# STEP 2: DEPLOY BACKEND
# ====================
echo "🌐 STEP 2: Deploying backend to GCP..."
echo ""
echo "⚠️  This will deploy the backend with dynamic confidence thresholds:"
echo "    - Rural (>20km): threshold 35"
echo "    - Suburban (10-20km): threshold 45"
echo "    - Urban (<10km): threshold 60"
echo ""
read -p "Press Enter to continue with deployment or Ctrl+C to cancel..."

gcloud app deploy apps/api/app.yaml

echo ""
echo "✅ Backend deployed!"
echo ""

# ====================
# STEP 3: TEST (Optional)
# ====================
echo "🧪 STEP 3: Test deployment (optional)"
echo ""
echo "You can now test a rural search in the app:"
echo "  - Search: 'cascade près de Mende'"
echo "  - Radius: 30km"
echo "  - Expected: 3-10 POIs with confidence 35-60"
echo ""
read -p "Press Enter when ready to continue with seeding..."

# ====================
# STEP 4: SEED NATURAL SPOTS
# ====================
echo ""
echo "======================================"
echo "🌲 NATURAL SPOTS SEEDING"
echo "======================================"
echo ""
echo "This will seed 15,000-25,000 natural spots (1-2h)"
echo ""
echo "Options:"
echo "  1. Run in background (recommended for night)"
echo "  2. Run in foreground (to watch progress)"
echo "  3. Skip seeding (do it later)"
echo ""
read -p "Choose option (1-3): " choice

case $choice in
  1)
    echo ""
    echo "🌙 Launching seeding in background..."
    nohup node scripts/seed-natural-spots.mjs > scripts/seed-natural-spots.log 2>&1 &
    SEED_PID=$!
    echo "✅ Seeding launched! PID: $SEED_PID"
    echo ""
    echo "Monitor progress with:"
    echo "  ./scripts/monitor-seeding.sh"
    echo "  tail -f scripts/seed-natural-spots.log"
    echo ""
    echo "Check if still running:"
    echo "  ps aux | grep seed-natural-spots"
    echo ""
    ;;
  2)
    echo ""
    echo "▶️  Running seeding in foreground..."
    echo "(Press Ctrl+C to cancel)"
    echo ""
    node scripts/seed-natural-spots.mjs
    ;;
  3)
    echo ""
    echo "⏭️  Skipping seeding. Run later with:"
    echo "  nohup node scripts/seed-natural-spots.mjs > scripts/seed-natural-spots.log 2>&1 &"
    echo ""
    exit 0
    ;;
  *)
    echo "Invalid choice. Exiting."
    exit 1
    ;;
esac

# ====================
# STEP 5: RETRY FAILED (After seeding)
# ====================
if [ "$choice" == "2" ]; then
  echo ""
  echo "======================================"
  echo "🔄 RETRY FAILED TASKS"
  echo "======================================"
  echo ""
  
  if [ -f scripts/seed-natural-spots-failed.json ]; then
    FAILED_COUNT=$(jq 'length' scripts/seed-natural-spots-failed.json 2>/dev/null || echo "0")
    
    if [ "$FAILED_COUNT" -gt "0" ]; then
      echo "⚠️  Found $FAILED_COUNT failed tasks"
      read -p "Retry failed tasks? (y/n): " retry
      
      if [ "$retry" == "y" ]; then
        echo "🔄 Retrying failed tasks..."
        node scripts/seed-natural-spots-retry.mjs
      fi
    else
      echo "✅ No failed tasks to retry!"
    fi
  else
    echo "✅ No failed tasks file found"
  fi
fi

# ====================
# STEP 6: DEPLOY INDEXES
# ====================
echo ""
echo "======================================"
echo "📊 FIRESTORE INDEXES"
echo "======================================"
echo ""
echo "After seeding completes, deploy Firestore indexes (10-30 min):"
echo "  firebase deploy --only firestore:indexes"
echo ""
read -p "Deploy indexes now? (y/n): " deploy_indexes

if [ "$deploy_indexes" == "y" ]; then
  echo "📊 Deploying Firestore indexes..."
  firebase deploy --only firestore:indexes
  echo "✅ Indexes deployed! (Build will continue in background)"
fi

echo ""
echo "======================================"
echo "🎉 DEPLOYMENT COMPLETE!"
echo "======================================"
echo ""
echo "Summary:"
echo "  ✅ Backend deployed with dynamic confidence thresholds"
if [ "$choice" == "1" ]; then
  echo "  🌙 Seeding running in background (check: ./scripts/monitor-seeding.sh)"
elif [ "$choice" == "2" ]; then
  echo "  ✅ Seeding completed"
fi
echo ""
echo "Next steps:"
echo "  1. Monitor seeding: ./scripts/monitor-seeding.sh"
echo "  2. Check logs: tail -f scripts/seed-natural-spots.log"
echo "  3. Retry failed: node scripts/seed-natural-spots-retry.mjs"
echo "  4. Deploy indexes: firebase deploy --only firestore:indexes"
echo ""
echo "Expected results:"
echo "  📱 Immediate: Rural searches return 3-10 POIs (no errors)"
echo "  🌲 After seeding: 90% of searches use database (faster, cheaper)"
echo ""
