#!/bin/bash

# ==============================================================================
# Database Sample Export Script (LOKAL VERSİYON)
# Bu script önemli tablolardan örnek veri çeker ve mds/DATABASE_SAMPLES.md'ye yazar
# AI asistanların veritabanı yapısını anlaması için kullanılır
# ==============================================================================

# Kullanım:
# 1. Önce SSM port forwarding başlat: dbconnect
# 2. Yeni terminal aç ve çalıştır: bash scripts/export-db-samples.sh

OUTPUT_FILE="mds/DATABASE_SAMPLES.md"
DB_HOST="localhost"
DB_PORT="15432"
DB_USER="suggestbet"
DB_NAME="boilerplate_db"
PGPASSWORD="SuGGesT2026SecuRe"

export PGPASSWORD

# Veritabanı bağlantı kontrolü
if ! psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "SELECT 1" > /dev/null 2>&1; then
    echo "❌ Veritabanına bağlanılamadı!"
    echo "📌 Önce SSM port forwarding başlat: dbconnect"
    exit 1
fi

echo "📊 Veritabanı örnekleri çekiliyor..."

# Tarih
CURRENT_DATE=$(date '+%Y-%m-%d %H:%M')

# Markdown dosyasını oluştur
cat > $OUTPUT_FILE << EOF
# Database Sample Data

Bu dosya AI asistanların veritabanı yapısını anlaması için örnek veriler içerir.
**Son Güncelleme:** $CURRENT_DATE

> ⚠️ Bu dosya otomatik oluşturulmuştur. Elle düzenlemeyin.
> Script: \`scripts/export-db-samples.sh\`

---

## 📈 Tablo İstatistikleri

| Tablo | Kayıt Sayısı |
|-------|-------------|
EOF

# Tablo sayılarını çek
for table in countries leagues teams players matches predictions odd_categories odd_selections match_team_stats live_matches users app_settings; do
    count=$(psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -t -c "SELECT COUNT(*) FROM $table;" 2>/dev/null | tr -d ' ')
    if [ -n "$count" ]; then
        echo "| $table | $count |" >> $OUTPUT_FILE
    fi
done

echo "" >> $OUTPUT_FILE
echo "---" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE

# Örnek veriler
echo "## 🏟️ Matches (Son 5 Maç)" >> $OUTPUT_FILE
echo '```json' >> $OUTPUT_FILE
psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -t -c "
SELECT json_agg(t) FROM (
    SELECT id, match_name, sport, score_home, score_away, state, 
           to_timestamp(mst_utc/1000) as match_time
    FROM matches 
    ORDER BY mst_utc DESC 
    LIMIT 5
) t;" 2>/dev/null >> $OUTPUT_FILE
echo '```' >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE

echo "## 🏆 Leagues (İlk 10)" >> $OUTPUT_FILE
echo '```json' >> $OUTPUT_FILE
psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -t -c "
SELECT json_agg(t) FROM (
    SELECT id, name, sport, country_id 
    FROM leagues 
    LIMIT 10
) t;" 2>/dev/null >> $OUTPUT_FILE
echo '```' >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE

echo "## ⚽ Teams (İlk 10)" >> $OUTPUT_FILE
echo '```json' >> $OUTPUT_FILE
psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -t -c "
SELECT json_agg(t) FROM (
    SELECT id, name, sport, logo_url 
    FROM teams 
    LIMIT 10
) t;" 2>/dev/null >> $OUTPUT_FILE
echo '```' >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE

echo "## 🌍 Countries (İlk 10)" >> $OUTPUT_FILE
echo '```json' >> $OUTPUT_FILE
psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -t -c "
SELECT json_agg(t) FROM (
    SELECT id, name, flag_url 
    FROM countries 
    LIMIT 10
) t;" 2>/dev/null >> $OUTPUT_FILE
echo '```' >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE

echo "## 🎯 Predictions (Son 5)" >> $OUTPUT_FILE
echo '```json' >> $OUTPUT_FILE
psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -t -c "
SELECT json_agg(t) FROM (
    SELECT match_id, prediction_json, created_at 
    FROM predictions 
    ORDER BY created_at DESC 
    LIMIT 5
) t;" 2>/dev/null >> $OUTPUT_FILE
echo '```' >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE

echo "## 📊 Match Team Stats (Örnek 5)" >> $OUTPUT_FILE
echo '```json' >> $OUTPUT_FILE
psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -t -c "
SELECT json_agg(t) FROM (
    SELECT match_id, team_id, possession_percentage, shots_on_target, 
           shots_off_target, corners, fouls
    FROM match_team_stats 
    LIMIT 5
) t;" 2>/dev/null >> $OUTPUT_FILE
echo '```' >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE

echo "## 💰 Odd Categories (Örnek 5)" >> $OUTPUT_FILE
echo '```json' >> $OUTPUT_FILE
psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -t -c "
SELECT json_agg(t) FROM (
    SELECT db_id, match_id, name, category_json_id 
    FROM odd_categories 
    LIMIT 5
) t;" 2>/dev/null >> $OUTPUT_FILE
echo '```' >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE

echo "## 🎰 Odd Selections (Örnek 10)" >> $OUTPUT_FILE
echo '```json' >> $OUTPUT_FILE
psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -t -c "
SELECT json_agg(t) FROM (
    SELECT db_id, odd_category_db_id, name, odd_value, position 
    FROM odd_selections 
    LIMIT 10
) t;" 2>/dev/null >> $OUTPUT_FILE
echo '```' >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE

echo "## ⚙️ App Settings" >> $OUTPUT_FILE
echo '```json' >> $OUTPUT_FILE
psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -t -c "
SELECT json_agg(t) FROM (
    SELECT key, value 
    FROM app_settings
) t;" 2>/dev/null >> $OUTPUT_FILE
echo '```' >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE

echo "---" >> $OUTPUT_FILE
echo "" >> $OUTPUT_FILE
echo "_Bu dosya \`scripts/export-db-samples.sh\` tarafından oluşturulmuştur._" >> $OUTPUT_FILE

echo "✅ Export tamamlandı: $OUTPUT_FILE"
