# 🚀 Suggest-Bet-BE — Deployment Guide

> **Tarih:** 2026-04-03
> **Versiyon:** Sport Partition Release (Futbol/Basketbol Ayrımı)
> **Amaç:** Masaüstü veya sunucuya kurulum adımları

---

## 🔑 Şifreler ve Bağlantı Bilgileri

| Servis | Kullanıcı | Şifre | Host | Port |
|--------|-----------|-------|------|------|
| **PostgreSQL** | `suggestbet` | `SuGGesT2026SecuRe` | `localhost` | `15432` |
| **Redis** | — | `RedisSecure2026` | `localhost` | `6379` |
| **JWT Secret** | — | `9bfa42fbdc6031da6d7c0bd30e9f5b6378a071613d0c02acf95eb576249c3a25` | — | — |

**Database URL:**
```
postgresql://suggestbet:SuGGesT2026SecuRe@localhost:15432/boilerplate_db?schema=public
```

---

## 📋 Gereksinimler

- **Node.js:** v20.19+
- **Docker + Docker Compose:** PostgreSQL + Redis için
- **npm:** Paket yöneticisi

---

## 🔧 Adım Adım Kurulum

### Adım 1: Kodu Çek

```bash
cd ~/Documents/Suggest-Bet-BE
git pull origin main
```

### Adım 2: .env Dosyasını Oluştur

```bash
# /Users/piton/Documents/Suggest-Bet-BE/.env
NODE_ENV=development
PORT=3005
DATABASE_URL="postgresql://suggestbet:SuGGesT2026SecuRe@localhost:15432/boilerplate_db?schema=public"
JWT_SECRET=9bfa42fbdc6031da6d7c0bd30e9f5b6378a071613d0c02acf95eb576249c3a25
JWT_ACCESS_EXPIRATION=7d
JWT_REFRESH_EXPIRATION=7d
REDIS_ENABLED=true
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=RedisSecure2026
DEFAULT_LANGUAGE=en
FALLBACK_LANGUAGE=en
ENABLE_MAIL=false
ENABLE_S3=false
ENABLE_WEBSOCKET=false
ENABLE_MULTI_TENANCY=false
THROTTLE_TTL=60000
THROTTLE_LIMIT=100
ENABLE_GEMINI=true
GOOGLE_API_KEY=your-google-api-key
GEMINI_MODEL=gemini-2.5-flash
AI_ENGINE_URL=http://127.0.0.1:8000
```

### Adım 3: Docker Infrastructure Başlat

```bash
cd ~/Documents/Suggest-Bet-BE
docker compose up -d postgres redis
```

PostgreSQL'in hazır olduğunu kontrol et:
```bash
docker exec -i suggestbet-postgres pg_isready -U suggestbet
# Çıktı: /var/run/postgresql:5432 - accepting connections
```

### Adım 4: Dump'u Restore Et

```bash
# Dump dosyasını container'a kopyala
docker cp /path/to/dump-boilerplate_db-202604020914-v5 suggestbet-postgres:/tmp/dump_file

# Restore et
export PGPASSWORD="SuGGesT2026SecuRe"
docker exec -e PGPASSWORD="$PGPASSWORD" suggestbet-postgres pg_restore \
  -U suggestbet -d boilerplate_db --clean --if-exists /tmp/dump_file
```

### Adım 5: Sport Partition Migration'ını Çalıştır

**Sırayla çalıştır — her biri ayrı transaction:**

```bash
export PGPASSWORD="SuGGesT2026SecuRe"
DB="suggestbet-postgres"
MIGRATION_DIR="prisma/migrations/20260403161000_sport_partition"

# 1. Yeni team stats tabloları oluştur
docker exec -e PGPASSWORD="$PGPASSWORD" -i $DB psql -U suggestbet -d boilerplate_db < $MIGRATION_DIR/01_create_team_stats.sql

# 2. Team stats verilerini kopyala
docker exec -e PGPASSWORD="$PGPASSWORD" -i $DB psql -U suggestbet -d boilerplate_db < $MIGRATION_DIR/02_copy_team_stats.sql

# 3. Yeni AI features tabloları oluştur
docker exec -e PGPASSWORD="$PGPASSWORD" -i $DB psql -U suggestbet -d boilerplate_db < $MIGRATION_DIR/03_create_ai_features.sql

# 4. AI features verilerini kopyala
docker exec -e PGPASSWORD="$PGPASSWORD" -i $DB psql -U suggestbet -d boilerplate_db < $MIGRATION_DIR/04_copy_ai_features.sql

# 5. match_player_stats → basketball_player_stats rename
docker exec -e PGPASSWORD="$PGPASSWORD" -i $DB psql -U suggestbet -d boilerplate_db < $MIGRATION_DIR/05_rename_player_stats.sql

# 6. odd_categories + odd_selections'e sport kolonu ekle
docker exec -e PGPASSWORD="$PGPASSWORD" -i $DB psql -U suggestbet -d boilerplate_db < $MIGRATION_DIR/06_add_sport_to_odds.sql
```

**odd_selections için batch update (14.8M satır — her çalıştır 1M günceller):**

```bash
# Bunu "remaining = 0" olana kadar tekrar tekrar çalıştır
export PGPASSWORD="SuGGesT2026SecuRe"
docker exec -e PGPASSWORD="$PGPASSWORD" -i suggestbet-postgres psql -U suggestbet -d boilerplate_db -c "
WITH t AS (
  SELECT os.db_id, oc.sport 
  FROM odd_selections os 
  JOIN odd_categories oc ON os.odd_category_db_id = oc.db_id 
  WHERE os.sport IS NULL 
  LIMIT 1000000
)
UPDATE odd_selections SET sport = t.sport FROM t WHERE odd_selections.db_id = t.db_id;

SELECT COUNT(*) as remaining FROM odd_selections WHERE sport IS NULL;
"
```

**Kalan satırlar bitince index oluştur:**
```bash
export PGPASSWORD="SuGGesT2026SecuRe"
docker exec -e PGPASSWORD="$PGPASSWORD" -i suggestbet-postgres psql -U suggestbet -d boilerplate_db -c "
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_odd_selections_sport ON odd_selections(sport) WHERE sport IS NOT NULL;
"
```

### Adım 6: Bağımlılıkları Yükle + Prisma Generate

```bash
cd ~/Documents/Suggest-Bet-BE

# Bağımlılıkları yükle
npm ci

# Prisma client oluştur
npx prisma generate
```

### Adım 7: Build + Başlat

```bash
# Build
npm run build

# Başlat
npm run start:prod
```

### Adım 8: Doğrulama

```bash
# Sağlık kontrolü
curl http://localhost:3005/api/health

# Swagger UI
open http://localhost:3005/api/docs

# Yeni tabloları kontrol et
export PGPASSWORD="SuGGesT2026SecuRe"
docker exec -e PGPASSWORD="$PGPASSWORD" -i suggestbet-postgres psql -U suggestbet -d boilerplate_db -c "
SELECT 'football_team_stats' as tbl, COUNT(*) FROM football_team_stats
UNION ALL SELECT 'basketball_team_stats', COUNT(*) FROM basketball_team_stats
UNION ALL SELECT 'basketball_player_stats', COUNT(*) FROM basketball_player_stats
UNION ALL SELECT 'odd_categories (sport)', COUNT(*) FROM odd_categories WHERE sport IS NOT NULL
UNION ALL SELECT 'odd_selections (sport)', COUNT(*) FROM odd_selections WHERE sport IS NOT NULL;
"
```

---

## 🤖 AI Engine (Opsiyonel)

```bash
cd ~/Documents/Suggest-Bet-BE/ai-engine

# Bağımlılıklar
pip install -r requirements.txt

# Başlat
uvicorn main:app --host 0.0.0.0 --port 8000
```

---

## ✅ Tablo Durumu (Migration Sonrası)

| Tablo | Satır (~) | Durum |
|-------|-----------|-------|
| `football_team_stats` | 217,956 | ✅ Yeni |
| `basketball_team_stats` | 48,824 | ✅ Yeni |
| `basketball_player_stats` | 273,140 | ✅ Rename edildi |
| `football_ai_features` | 0 | ⚠️ Feeder dolduracak |
| `basketball_ai_features` | 0 | ⚠️ Feeder dolduracak |
| `odd_categories (sport)` | 2,695,511 | ✅ Güncellendi |
| `odd_selections (sport)` | 14,810,396 | ✅ Güncellendi |
| `match_team_stats` (ESKİ) | 266,780 | 🗑️ Silinebilir (yedek olarak kalsın) |
| `match_ai_features` (ESKİ) | 0 | 🗑️ Silinebilir |

---

## 🗑️ Eski Tabloları Silme (Opsiyonel)

**SADECE her şey çalıştığını doğruladıktan sonra:**

```bash
export PGPASSWORD="SuGGesT2026SecuRe"
docker exec -e PGPASSWORD="$PGPASSWORD" -i suggestbet-postgres psql -U suggestbet -d boilerplate_db -c "
DROP TABLE IF EXISTS match_team_stats CASCADE;
DROP TABLE IF EXISTS match_ai_features CASCADE;
"
```

---

## 🔧 Sorun Giderme

### PostgreSQL başlamıyor (postmaster.pid hatası)
```bash
docker compose stop postgres
docker compose rm -f postgres
docker volume rm suggest-bet-be_pgml_data
docker compose up -d postgres
# Sonra dump + migration tekrar
```

### Docker Desktop başlamıyor (disk dolu)
```bash
# Büyük dosyaları temizle
rm -rf ~/Library/Caches/Homebrew/*
rm -rf ~/.npm/_cacache
docker system prune -af
df -h /  # En az 3-4GB boş olmalı
```

### Feeder çalışmıyor
```bash
# Logları kontrol et
tail -f logs/app.log  # veya docker logs suggestbet-app

# Manuel feeder çalıştır
npm run feeder:live
```

---

## 📝 Notlar

- **Veri kaybolmaz** — eski tablolar migration sonrası silinmez, yedek olarak kalır
- **Feeder** otomatik yeni tablolara yazar (`footballTeamStats`, `basketballTeamStats`, vb.)
- **Redis** opsiyonel — `REDIS_ENABLED=false` yapabilirsin (in-memory fallback)
- **Swagger** sadece development modunda aktif
