#!/bin/bash
# 🔍 Vérification de la Configuration Redis Optimisée

echo "╔════════════════════════════════════════════════════════════════╗"
echo "║  📊 Configuration Redis - Features Actives Mode Allégé        ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""

ENV_FILE="apps/api/.env"

if [ ! -f "$ENV_FILE" ]; then
  echo "❌ Fichier .env introuvable: $ENV_FILE"
  exit 1
fi

echo "📋 Feature Flags (apps/api/.env):"
echo ""

# Fonction pour checker un flag
check_flag() {
  local flag_name=$1
  local value=$(grep "^${flag_name}=" "$ENV_FILE" | cut -d'=' -f2)
  
  if [ "$value" = "true" ]; then
    echo "  ✅ ${flag_name}=true"
  elif [ "$value" = "false" ]; then
    echo "  ⏸️  ${flag_name}=false"
  else
    echo "  ⚠️  ${flag_name}=${value:-'non défini'}"
  fi
}

check_flag "ENABLE_AI_WORKER"
check_flag "ENABLE_REDIS_CACHE"
check_flag "ENABLE_AI_CACHE"
check_flag "ENABLE_COLLABORATION"

echo ""
echo "🎯 Configuration Détectée:"
echo ""

# Compter les flags actifs
enabled_count=$(grep -E "^ENABLE_(AI_WORKER|REDIS_CACHE|AI_CACHE|COLLABORATION)=true" "$ENV_FILE" | wc -l | xargs)

if [ "$enabled_count" -eq 4 ]; then
  echo "  📊 Mode: FEATURES ACTIVES (Allégé)"
  echo "  💰 Redis attendu: 10-15k req/jour"
  echo "  ⚡ Performance: Normale (légèrement réduite)"
  echo "  ✅ AI: Actif (1 worker)"
  echo "  ✅ Cache: Actif (TTL 5 min)"
  echo "  ✅ Collaboration: Actif"
  echo ""
  echo "  💡 C'est la config RECOMMANDÉE pour dev !"
elif [ "$enabled_count" -eq 0 ]; then
  echo "  🚨 Mode: URGENCE (Toutes features off)"
  echo "  💰 Redis attendu: <5k req/jour"
  echo "  ⚠️  Performance: Dégradée (pas de cache)"
  echo "  ❌ AI: Désactivé"
  echo "  ❌ Cache: Désactivé"
  echo "  ❌ Collaboration: Désactivé"
  echo ""
  echo "  ⚠️  Utilise seulement si budget Redis dépassé !"
else
  echo "  ⚙️  Mode: CONFIGURATION MIXTE"
  echo "  💰 Redis attendu: 5-20k req/jour (variable)"
  echo "  ⚡ Performance: Variable selon flags"
  echo ""
  echo "  💡 Pour économies max: Tout mettre à 'true' (config allégée auto)"
fi

echo ""
echo "📁 Fichiers de Configuration:"
echo ""

if [ -f "apps/api/src/core/queue/ai-queue.ts" ]; then
  echo "  ✅ ai-queue.ts (polling intervals optimisés)"
else
  echo "  ❌ ai-queue.ts manquant"
fi

if [ -f "apps/api/src/core/queue/ai.worker.ts" ]; then
  echo "  ✅ ai.worker.ts (concurrency réduite)"
else
  echo "  ❌ ai.worker.ts manquant"
fi

if [ -f "apps/api/src/modules/planner/planner.service.ts" ]; then
  echo "  ✅ planner.service.ts (TTL adaptatif)"
else
  echo "  ❌ planner.service.ts manquant"
fi

if [ -f "apps/api/src/modules/ai/ai.repository.ts" ]; then
  echo "  ✅ ai.repository.ts (TTL adaptatif)"
else
  echo "  ❌ ai.repository.ts manquant"
fi

echo ""
echo "📚 Documentation:"
echo ""
echo "  📖 REDIS_OPTIMIZATION_STRATEGY.md → Stratégie complète"
echo "  📖 REDIS_EMERGENCY_FIX.md → Fix d'urgence si besoin"
echo "  📖 docs/REDIS_OPTIMIZATION_GUIDE.md → Guide détaillé"

echo ""
echo "🔍 Commandes Utiles:"
echo ""
echo "  • Monitorer: pnpm --filter '@my-roadtrip/api' tsx scripts/redis-monitor.ts"
echo "  • Cleanup:   ./scripts/cleanup-redis.sh"
echo "  • Vérifier:  ./scripts/check-redis-config.sh (ce script)"

echo ""
echo "✅ Configuration vérifiée avec succès !"
echo ""
