# 🚀 Quick Start - POI Confidence Fix

## TL;DR

**Problem**: Rural searches return "No POIs returned from AI" error
**Fix**: Dynamic confidence thresholds (35 for rural, 45 for suburban, 60 for urban)
**Status**: ✅ Code ready, ⏳ needs deployment

---

## Option 1: Full Auto (Recommended)

```bash
./DEPLOY_AND_SEED_COMMANDS.sh
```

Script will:
1. ✅ Build backend
2. 🌐 Deploy to GCP
3. 🌲 Seed natural spots (interactive choice)
4. 📊 Deploy Firestore indexes

---

## Option 2: Manual Steps

### 1. Deploy Backend (5-10 min)

```bash
cd /Users/floriansola/Documents/MyRoadTrip
pnpm build
gcloud app deploy apps/api/app.yaml
```

**Test**: Search "cascade près de Mende" with 30km radius → should return 3-10 POIs ✅

---

### 2. Seed Natural Spots (Tonight - 1-2h)

```bash
# Background mode (recommended for night)
nohup node scripts/seed-natural-spots.mjs > scripts/seed-natural-spots.log 2>&1 &

# Monitor progress
./scripts/monitor-seeding.sh

# Watch logs
tail -f scripts/seed-natural-spots.log

# Check if running
ps aux | grep seed-natural-spots
```

**What happens**:
- 72 geographical chunks (1.5°×1.5° each)
- 10 priority categories (viewpoint, peak, hiking, waterfall, park, lake, beach, cave, glacier, spring)
- 3 Overpass instances with smart queue
- Auto-wait 90s if all instances busy
- 15s pause between chunks
- Failed tasks saved to `seed-natural-spots-failed.json`

**Expected**: 15,000-25,000 natural spots

---

### 3. Retry Failed Tasks (If needed)

```bash
# Check failed tasks
cat scripts/seed-natural-spots-failed.json | jq length

# Retry
node scripts/seed-natural-spots-retry.mjs
```

---

### 4. Deploy Firestore Indexes (10-30 min)

```bash
firebase deploy --only firestore:indexes
```

**Required indexes**:
- `poi_database`: geohash4, geohash5, geohash6 + categories (composite)

---

## Monitoring Commands

```bash
# Check if seeding is running
ps aux | grep seed-natural-spots

# Kill seeding (if needed)
pkill -f seed-natural-spots

# View logs
tail -f scripts/seed-natural-spots.log

# Count POIs in database
# (requires Firestore access)

# Monitor script
./scripts/monitor-seeding.sh
```

---

## Expected Timeline

| Time | Action | Duration |
|------|--------|----------|
| **Now** | Deploy backend | 5-10 min |
| **Test** | Verify rural searches work | 2 min |
| **23:00** | Launch seeding (background) | 1-2h |
| **01:00** | Seeding completes | - |
| **Next morning** | Retry failed tasks (if any) | 5-15 min |
| **Next morning** | Deploy Firestore indexes | 10-30 min |

---

## Success Indicators

### After Backend Deploy
- ✅ Rural searches return 3-10 POIs (no errors)
- ✅ POIs have confidence 35-60
- ✅ No "No POIs returned from AI" errors

### After Seeding
- ✅ 15,000-25,000 POIs in `poi_database` collection
- ✅ 90% of rural searches use database (skip AI)
- ✅ Faster responses (no OpenAI call)
- ✅ Lower costs (no API calls)

### After Indexes
- ✅ Searches are fast (< 100ms)
- ✅ No "index required" errors in logs

---

## Troubleshooting

### Seeding stuck at 0%
```bash
# Check logs
tail -f scripts/seed-natural-spots.log

# Look for rate limit messages
grep -i "rate" scripts/seed-natural-spots.log

# Overpass instances busy - wait 90s auto-retry
```

### Rate limited immediately
```bash
# Check Overpass status manually
curl "https://overpass-api.de/api/status"
curl "https://overpass.kumi.systems/api/status"
curl "https://overpass.openstreetmap.fr/api/status"

# All busy? Wait and retry tomorrow morning
```

### Failed tasks
```bash
# Check count
cat scripts/seed-natural-spots-failed.json | jq length

# See details
cat scripts/seed-natural-spots-failed.json | jq '.[0]'

# Retry all
node scripts/seed-natural-spots-retry.mjs
```

### Backend deployment fails
```bash
# Check if already deployed
gcloud app versions list --service=default

# Check logs
gcloud app logs tail -s default

# Rollback if needed
gcloud app versions list
gcloud app services set-traffic default --splits=VERSION=1.0
```

---

## What Changed

**File**: `apps/api/src/modules/ai/poi.processor.ts` (lines 714-730)

**Before**:
```typescript
if (poi.confidence < MIN_POI_CONFIDENCE) { // Always 60
  return false
}
```

**After**:
```typescript
let dynamicThreshold = MIN_POI_CONFIDENCE // 60
if (radiusKm > 20) {
  dynamicThreshold = 35  // Rural
} else if (radiusKm > 10) {
  dynamicThreshold = 45  // Suburban
}

if (poi.confidence < dynamicThreshold) {
  return false
}
```

---

## Contact/Questions

See `POI_CONFIDENCE_FIX.md` for detailed technical explanation.
