#!/bin/bash

echo "════════════════════════════════════════════════════════════════"
echo "  🔍 VÉRIFICATION DE LA MISE À JOUR"
echo "════════════════════════════════════════════════════════════════"
echo

# Couleurs
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

# 1. Vérifier les services Docker
echo "1️⃣  Services Docker"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if curl -s http://localhost:8007/health > /dev/null 2>&1; then
    echo -e "${GREEN}✓${NC} Backend: http://localhost:8007"
else
    echo -e "${RED}✗${NC} Backend: http://localhost:8007 (DOWN)"
fi

if curl -s http://localhost:8087 > /dev/null 2>&1; then
    echo -e "${GREEN}✓${NC} Frontend: http://localhost:8087"
else
    echo -e "${RED}✗${NC} Frontend: http://localhost:8087 (DOWN)"
fi
echo

# 2. Vérifier le script bench.sh
echo "2️⃣  Script Benchmark"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if grep -q "Core(s) per socket" scripts/bench.sh; then
    echo -e "${GREEN}✓${NC} Correctif CPU cores appliqué"
else
    echo -e "${RED}✗${NC} Correctif CPU cores manquant"
fi
echo

# 3. Vérifier le frontend JS
echo "3️⃣  Frontend JavaScript"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if grep -q "snapshot.cpu_cores != null" frontend/js/device_detail.js; then
    echo -e "${GREEN}✓${NC} Amélioration affichage CPU appliquée"
else
    echo -e "${RED}✗${NC} Amélioration affichage CPU manquante"
fi

if grep -q "snapshot.ram_used_mb != null" frontend/js/device_detail.js; then
    echo -e "${GREEN}✓${NC} Amélioration affichage RAM appliquée"
else
    echo -e "${RED}✗${NC} Amélioration affichage RAM manquante"
fi
echo

# 4. Tester la collecte CPU cores localement
echo "4️⃣  Test Collecte CPU Cores (machine actuelle)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
export LC_ALL=C
cores_per_socket=$(lscpu | awk -F: '/Core\(s\) per socket/ {gsub(/^[ \t]+/,"",$2); print $2}')
sockets=$(lscpu | awk -F: '/Socket\(s\)/ {gsub(/^[ \t]+/,"",$2); print $2}')
cores=$((${cores_per_socket:-1} * ${sockets:-1}))
threads=$(nproc)

echo "  Cores physiques: $cores"
echo "  Threads logiques: $threads"
echo

# 5. Vérifier les données actuelles dans l'API
echo "5️⃣  Données API Actuelles"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Device 1 (lenovo-bureau):"
curl -s http://localhost:8007/api/devices/1 2>/dev/null | jq -r '
  "  cpu_cores: \(.last_hardware_snapshot.cpu_cores // "null")",
  "  cpu_threads: \(.last_hardware_snapshot.cpu_threads // "null")",
  "  ram_used_mb: \(.last_hardware_snapshot.ram_used_mb // "null")",
  "  ram_free_mb: \(.last_hardware_snapshot.ram_free_mb // "null")"
' 2>/dev/null || echo -e "${RED}  Erreur: API non accessible${NC}"
echo

echo "Device 2 (aorus):"
curl -s http://localhost:8007/api/devices/2 2>/dev/null | jq -r '
  "  cpu_cores: \(.last_hardware_snapshot.cpu_cores // "null")",
  "  cpu_threads: \(.last_hardware_snapshot.cpu_threads // "null")",
  "  ram_used_mb: \(.last_hardware_snapshot.ram_used_mb // "null")",
  "  ram_free_mb: \(.last_hardware_snapshot.ram_free_mb // "null")"
' 2>/dev/null || echo -e "${RED}  Erreur: API non accessible${NC}"
echo

# 6. Instructions
echo "════════════════════════════════════════════════════════════════"
echo "  🎯 PROCHAINE ÉTAPE"
echo "════════════════════════════════════════════════════════════════"
echo
echo "Pour mettre à jour les données avec les correctifs :"
echo
echo "  ${YELLOW}sudo bash scripts/bench.sh${NC}"
echo
echo "Ensuite, rafraîchir la page :"
echo "  http://localhost:8087/device_detail.html?id=1"
echo
echo "════════════════════════════════════════════════════════════════"
