#!/bin/bash
#
# Script de Vérification Post-Migration
# Vérifie que la migration s'est bien passée
#

set -e

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

echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║         VÉRIFICATION POST-MIGRATION RPA VISION V3            ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""

NEW_V3_DIR="$HOME/ai/rpa_vision_v3"
ERRORS=0

# Fonction de vérification
check_item() {
    local item=$1
    local path="$NEW_V3_DIR/$item"
    
    if [ -e "$path" ]; then
        echo -e "${GREEN}✓${NC} $item"
        return 0
    else
        echo -e "${RED}✗${NC} $item (manquant)"
        ((ERRORS++))
        return 1
    fi
}

# Vérifier que le répertoire existe
echo -e "${BLUE}[1/5]${NC} Vérification du répertoire principal..."
if [ -d "$NEW_V3_DIR" ]; then
    echo -e "${GREEN}✓${NC} Répertoire trouvé: $NEW_V3_DIR"
else
    echo -e "${RED}✗${NC} Répertoire non trouvé: $NEW_V3_DIR"
    exit 1
fi

# Vérifier la structure core
echo ""
echo -e "${BLUE}[2/5]${NC} Vérification de la structure core/..."
check_item "core/models/raw_session.py"
check_item "core/models/screen_state.py"
check_item "core/models/ui_element.py"
check_item "core/models/state_embedding.py"
check_item "core/models/workflow_graph.py"
check_item "core/detection/ui_detector.py"
check_item "core/embedding/clip_embedder.py"
check_item "core/embedding/fusion_engine.py"
check_item "core/graph/graph_builder.py"
check_item "core/graph/node_matcher.py"
check_item "core/execution/action_executor.py"
check_item "core/execution/error_handler.py"

# Vérifier la documentation
echo ""
echo -e "${BLUE}[3/5]${NC} Vérification de la documentation..."
check_item "README.md"
check_item "docs/reference/ARCHITECTURE_VISION_COMPLETE.md"
check_item "docs/specs/ROADMAP.md"
check_item "STATUS_24NOV.md"
check_item "PRODUCTION_READY.md"

# Vérifier les tests
echo ""
echo -e "${BLUE}[4/5]${NC} Vérification des tests..."
check_item "tests/unit/test_error_handler.py"
check_item "tests/unit/test_ui_element.py"
check_item "tests/unit/test_state_embedding.py"
check_item "tests/integration/test_error_recovery.py"

# Vérifier les scripts
echo ""
echo -e "${BLUE}[5/5]${NC} Vérification des scripts..."
check_item "run.sh"
check_item "setup.py"
check_item "requirements.txt"

# Vérifier les imports Python
echo ""
echo -e "${BLUE}Bonus${NC} Vérification des imports Python..."
cd "$NEW_V3_DIR"

if command -v python3 &> /dev/null; then
    if python3 -c "import sys; sys.path.insert(0, '.'); from core.models import workflow_graph" 2>/dev/null; then
        echo -e "${GREEN}✓${NC} Imports Python fonctionnels"
    else
        echo -e "${YELLOW}⚠${NC}  Imports Python à vérifier (normal si venv non activé)"
    fi
else
    echo -e "${YELLOW}⚠${NC}  Python3 non trouvé, skip test imports"
fi

# Résumé
echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
if [ $ERRORS -eq 0 ]; then
    echo "║              VÉRIFICATION RÉUSSIE ✅                         ║"
else
    echo "║              VÉRIFICATION ÉCHOUÉE ❌                         ║"
fi
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""

if [ $ERRORS -eq 0 ]; then
    echo -e "${GREEN}✓${NC} Tous les fichiers critiques sont présents"
    echo -e "${GREEN}✓${NC} La migration s'est bien déroulée"
    echo ""
    echo -e "${BLUE}Prochaine étape:${NC}"
    echo "  Ouvrir le projet dans ton IDE:"
    echo "  ${YELLOW}code $NEW_V3_DIR${NC}"
    echo ""
else
    echo -e "${RED}✗${NC} $ERRORS fichier(s) manquant(s)"
    echo -e "${YELLOW}⚠${NC}  Vérifier la sauvegarde et relancer la migration si nécessaire"
    echo ""
fi

exit $ERRORS
