#!/bin/bash
# Script de vérification complète du déploiement Veza
# Usage: ./check-deployment.sh

set -euo pipefail

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

echo -e "${BLUE}🔍 Vérification complète du déploiement Veza${NC}"
echo ""

# 1. Vérifier les conteneurs
echo -e "${BLUE}1. Conteneurs Incus:${NC}"
EXPECTED_CONTAINERS=("veza-infra" "veza-backend-api" "veza-chat-server" "veza-stream-server" "veza-web" "veza-haproxy")
ALL_CONTAINERS_OK=true

for container in "${EXPECTED_CONTAINERS[@]}"; do
    if incus list -c n --format csv 2>/dev/null | grep -q "^${container}$"; then
        STATE=$(incus list -c ns --format csv 2>/dev/null | grep "^${container}," | cut -d',' -f2)
        if [ "$STATE" = "RUNNING" ]; then
            echo -e "  ${GREEN}✅ ${container} - RUNNING${NC}"
        else
            echo -e "  ${YELLOW}⚠️  ${container} - ${STATE}${NC}"
            ALL_CONTAINERS_OK=false
        fi
    else
        echo -e "  ${RED}❌ ${container} - NOT FOUND${NC}"
        ALL_CONTAINERS_OK=false
    fi
done

echo ""

# 2. Vérifier les services systemd
echo -e "${BLUE}2. Services Systemd:${NC}"
ALL_SERVICES_OK=true

# Backend API
if incus list -c n --format csv 2>/dev/null | grep -q "^veza-backend-api$"; then
    if incus exec veza-backend-api -- systemctl is-active --quiet veza-backend-api 2>/dev/null; then
        echo -e "  ${GREEN}✅ veza-backend-api - ACTIVE${NC}"
    else
        echo -e "  ${YELLOW}⚠️  veza-backend-api - INACTIVE${NC}"
        ALL_SERVICES_OK=false
    fi
fi

# Chat Server
if incus list -c n --format csv 2>/dev/null | grep -q "^veza-chat-server$"; then
    if incus exec veza-chat-server -- systemctl is-active --quiet veza-chat-server 2>/dev/null; then
        echo -e "  ${GREEN}✅ veza-chat-server - ACTIVE${NC}"
    else
        echo -e "  ${YELLOW}⚠️  veza-chat-server - INACTIVE${NC}"
        ALL_SERVICES_OK=false
    fi
fi

# Stream Server
if incus list -c n --format csv 2>/dev/null | grep -q "^veza-stream-server$"; then
    if incus exec veza-stream-server -- systemctl is-active --quiet veza-stream-server 2>/dev/null; then
        echo -e "  ${GREEN}✅ veza-stream-server - ACTIVE${NC}"
    else
        echo -e "  ${YELLOW}⚠️  veza-stream-server - INACTIVE${NC}"
        ALL_SERVICES_OK=false
    fi
fi

# Web (Apache)
if incus list -c n --format csv 2>/dev/null | grep -q "^veza-web$"; then
    if incus exec veza-web -- systemctl is-active --quiet apache2 2>/dev/null; then
        echo -e "  ${GREEN}✅ apache2 (veza-web) - ACTIVE${NC}"
    else
        echo -e "  ${YELLOW}⚠️  apache2 (veza-web) - INACTIVE${NC}"
        ALL_SERVICES_OK=false
    fi
fi

# HAProxy
if incus list -c n --format csv 2>/dev/null | grep -q "^veza-haproxy$"; then
    if incus exec veza-haproxy -- systemctl is-active --quiet haproxy 2>/dev/null; then
        echo -e "  ${GREEN}✅ haproxy - ACTIVE${NC}"
    else
        echo -e "  ${YELLOW}⚠️  haproxy - INACTIVE${NC}"
        ALL_SERVICES_OK=false
    fi
fi

# Infrastructure
if incus list -c n --format csv 2>/dev/null | grep -q "^veza-infra$"; then
    if incus exec veza-infra -- systemctl is-active --quiet postgresql 2>/dev/null; then
        echo -e "  ${GREEN}✅ postgresql (veza-infra) - ACTIVE${NC}"
    else
        echo -e "  ${YELLOW}⚠️  postgresql (veza-infra) - INACTIVE${NC}"
        ALL_SERVICES_OK=false
    fi
    
    if incus exec veza-infra -- systemctl is-active --quiet redis-server 2>/dev/null; then
        echo -e "  ${GREEN}✅ redis-server (veza-infra) - ACTIVE${NC}"
    else
        echo -e "  ${YELLOW}⚠️  redis-server (veza-infra) - INACTIVE${NC}"
        ALL_SERVICES_OK=false
    fi
fi

echo ""

# 3. Vérifier la connectivité réseau
echo -e "${BLUE}3. Connectivité réseau:${NC}"

# Test Internet
if incus list -c n --format csv 2>/dev/null | grep -q "^veza-backend-api$"; then
    if incus exec veza-backend-api -- timeout 3 ping -c 2 8.8.8.8 >/dev/null 2>&1; then
        echo -e "  ${GREEN}✅ veza-backend-api - Internet OK${NC}"
    else
        echo -e "  ${RED}❌ veza-backend-api - Internet FAILED${NC}"
    fi
    
    # Test infrastructure
    if incus exec veza-backend-api -- timeout 3 ping -c 2 10.10.10.10 >/dev/null 2>&1; then
        echo -e "  ${GREEN}✅ veza-backend-api - Can reach infrastructure${NC}"
    else
        echo -e "  ${RED}❌ veza-backend-api - Cannot reach infrastructure${NC}"
    fi
fi

echo ""

# 4. Tester les endpoints HTTP
echo -e "${BLUE}4. Endpoints HTTP:${NC}"

# Backend API
if incus list -c n --format csv 2>/dev/null | grep -q "^veza-backend-api$"; then
    if incus exec veza-backend-api -- systemctl is-active --quiet veza-backend-api 2>/dev/null; then
        if incus exec veza-backend-api -- timeout 3 curl -s -f http://localhost:8080/health >/dev/null 2>&1 || \
           incus exec veza-backend-api -- timeout 3 curl -s -f http://localhost:8080/api/v1/health >/dev/null 2>&1; then
            echo -e "  ${GREEN}✅ Backend API (http://10.10.10.2:8080) - OK${NC}"
        else
            echo -e "  ${YELLOW}⚠️  Backend API - Service running but endpoint not responding${NC}"
        fi
    else
        echo -e "  ${RED}❌ Backend API - Service not running${NC}"
    fi
fi

# Chat Server
if incus list -c n --format csv 2>/dev/null | grep -q "^veza-chat-server$"; then
    if incus exec veza-chat-server -- systemctl is-active --quiet veza-chat-server 2>/dev/null; then
        if incus exec veza-chat-server -- timeout 3 curl -s -f http://localhost:8081/health >/dev/null 2>&1; then
            echo -e "  ${GREEN}✅ Chat Server (http://10.10.10.3:8081) - OK${NC}"
        else
            echo -e "  ${YELLOW}⚠️  Chat Server - Service running but endpoint not responding${NC}"
        fi
    else
        echo -e "  ${RED}❌ Chat Server - Service not running${NC}"
    fi
fi

# Stream Server
if incus list -c n --format csv 2>/dev/null | grep -q "^veza-stream-server$"; then
    if incus exec veza-stream-server -- systemctl is-active --quiet veza-stream-server 2>/dev/null; then
        if incus exec veza-stream-server -- timeout 3 curl -s -f http://localhost:3002/health >/dev/null 2>&1; then
            echo -e "  ${GREEN}✅ Stream Server (http://10.10.10.4:3002) - OK${NC}"
        else
            echo -e "  ${YELLOW}⚠️  Stream Server - Service running but endpoint not responding${NC}"
        fi
    else
        echo -e "  ${RED}❌ Stream Server - Service not running${NC}"
    fi
fi

# Web Frontend
if incus list -c n --format csv 2>/dev/null | grep -q "^veza-web$"; then
    if incus exec veza-web -- systemctl is-active --quiet apache2 2>/dev/null; then
        if incus exec veza-web -- timeout 3 curl -s -f http://localhost:80 >/dev/null 2>&1; then
            echo -e "  ${GREEN}✅ Web Frontend (http://10.10.10.5:80) - OK${NC}"
        else
            echo -e "  ${YELLOW}⚠️  Web Frontend - Apache running but not responding${NC}"
        fi
    else
        echo -e "  ${RED}❌ Web Frontend - Apache not running${NC}"
    fi
fi

# HAProxy
if incus list -c n --format csv 2>/dev/null | grep -q "^veza-haproxy$"; then
    if incus exec veza-haproxy -- systemctl is-active --quiet haproxy 2>/dev/null; then
        if incus exec veza-haproxy -- timeout 3 curl -s -f http://localhost:80 >/dev/null 2>&1; then
            echo -e "  ${GREEN}✅ HAProxy (http://10.10.10.6:80) - OK${NC}"
        else
            echo -e "  ${YELLOW}⚠️  HAProxy - Service running but not responding${NC}"
        fi
    else
        echo -e "  ${RED}❌ HAProxy - Service not running${NC}"
    fi
fi

echo ""

# 5. Test d'accès depuis l'hôte
echo -e "${BLUE}5. Accès depuis l'hôte:${NC}"

# Test HAProxy depuis l'hôte
if timeout 3 curl -s -f http://10.10.10.6:80 >/dev/null 2>&1; then
    echo -e "  ${GREEN}✅ HAProxy accessible depuis l'hôte (http://10.10.10.6:80)${NC}"
else
    echo -e "  ${RED}❌ HAProxy non accessible depuis l'hôte${NC}"
fi

# Test Backend API depuis l'hôte
if timeout 3 curl -s -f http://10.10.10.2:8080/health >/dev/null 2>&1 || \
   timeout 3 curl -s -f http://10.10.10.2:8080/api/v1/health >/dev/null 2>&1; then
    echo -e "  ${GREEN}✅ Backend API accessible depuis l'hôte (http://10.10.10.2:8080)${NC}"
else
    echo -e "  ${YELLOW}⚠️  Backend API non accessible depuis l'hôte${NC}"
fi

# Test Web Frontend depuis l'hôte
if timeout 3 curl -s -f http://10.10.10.5:80 >/dev/null 2>&1; then
    echo -e "  ${GREEN}✅ Web Frontend accessible depuis l'hôte (http://10.10.10.5:80)${NC}"
else
    echo -e "  ${YELLOW}⚠️  Web Frontend non accessible depuis l'hôte${NC}"
fi

echo ""

# Résumé
echo -e "${BLUE}=== RÉSUMÉ ===${NC}"
if [ "$ALL_CONTAINERS_OK" = true ] && [ "$ALL_SERVICES_OK" = true ]; then
    echo -e "${GREEN}✅ Déploiement complet et fonctionnel!${NC}"
    exit 0
else
    echo -e "${YELLOW}⚠️  Déploiement incomplet ou certains services ne fonctionnent pas${NC}"
    echo ""
    echo "Pour corriger:"
    echo "  1. Vérifier les logs: make incus-logs SERVICE=<service-name>"
    echo "  2. Redémarrer les services: make incus-start-all"
    echo "  3. Redéployer: make deploy-incus"
    exit 1
fi
