#!/bin/bash
# Verify Incus deployment
# Usage: ./verify-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}🔍 Verifying Incus deployment...${NC}"
echo ""

# Check if containers exist
echo -e "${BLUE}Checking containers...${NC}"
CONTAINERS=("veza-infra" "veza-backend-api" "veza-chat-server" "veza-stream-server" "veza-web" "veza-haproxy")
ALL_EXIST=true

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

echo ""

# Check IP addresses
echo -e "${BLUE}Checking IP addresses...${NC}"
EXPECTED_IPS=(
    "veza-backend-api:10.10.10.2"
    "veza-chat-server:10.10.10.3"
    "veza-stream-server:10.10.10.4"
    "veza-web:10.10.10.5"
    "veza-haproxy:10.10.10.6"
    "veza-infra:10.10.10.10"
)

for ip_config in "${EXPECTED_IPS[@]}"; do
    container=$(echo $ip_config | cut -d':' -f1)
    expected_ip=$(echo $ip_config | cut -d':' -f2)
    
    if incus list -c n4 --format csv | grep -q "^${container},"; then
        actual_ip=$(incus list -c n4 --format csv | grep "^${container}," | cut -d',' -f4 | awk '{print $1}')
        if [ "$actual_ip" = "$expected_ip" ]; then
            echo -e "  ${GREEN}✅ ${container} - ${actual_ip}${NC}"
        else
            echo -e "  ${YELLOW}⚠️  ${container} - ${actual_ip} (expected ${expected_ip})${NC}"
        fi
    fi
done

echo ""

# Check services
echo -e "${BLUE}Checking systemd services...${NC}"
SERVICES=("veza-backend-api" "veza-chat-server" "veza-stream-server")

for service in "${SERVICES[@]}"; do
    container="veza-$(echo $service | sed 's/veza-//')"
    if incus list -c n --format csv | grep -q "^${container}$"; then
        if incus exec ${container} -- systemctl is-active --quiet ${service} 2>/dev/null; then
            echo -e "  ${GREEN}✅ ${service} - ACTIVE${NC}"
        else
            echo -e "  ${YELLOW}⚠️  ${service} - INACTIVE${NC}"
        fi
    fi
done

# Check Apache
if incus list -c n --format csv | 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}"
    fi
fi

# Check haproxy
if incus list -c n --format csv | 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}"
    fi
fi

# Check infrastructure
if incus list -c n --format csv | 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}"
    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}"
    fi
fi

echo ""

# Test network connectivity
echo -e "${BLUE}Testing network connectivity...${NC}"
if incus list -c n --format csv | grep -q "^veza-backend-api$"; then
    if incus exec veza-backend-api -- ping -c 2 -W 2 8.8.8.8 >/dev/null 2>&1; then
        echo -e "  ${GREEN}✅ veza-backend-api - Internet connectivity OK${NC}"
    else
        echo -e "  ${YELLOW}⚠️  veza-backend-api - Internet connectivity FAILED${NC}"
    fi
    
    if incus exec veza-backend-api -- ping -c 2 -W 2 10.10.10.10 >/dev/null 2>&1; then
        echo -e "  ${GREEN}✅ veza-backend-api - Can reach infrastructure${NC}"
    else
        echo -e "  ${YELLOW}⚠️  veza-backend-api - Cannot reach infrastructure${NC}"
    fi
fi

# Test service health endpoints (if services are running)
echo -e "${BLUE}Testing service health endpoints...${NC}"
if incus list -c n --format csv | 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 2 curl -s -f http://localhost:8080/api/v1/health >/dev/null 2>&1 || \
           incus exec veza-backend-api -- timeout 2 curl -s -f http://localhost:8080/health >/dev/null 2>&1; then
            echo -e "  ${GREEN}✅ Backend API health check - OK${NC}"
        else
            echo -e "  ${YELLOW}⚠️  Backend API health check - Service running but endpoint not responding${NC}"
        fi
    else
        echo -e "  ${YELLOW}⚠️  Backend API - Service not running${NC}"
    fi
fi

if incus list -c n --format csv | 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 2 curl -s -f http://localhost:8081/health >/dev/null 2>&1; then
            echo -e "  ${GREEN}✅ Chat Server health check - OK${NC}"
        else
            echo -e "  ${YELLOW}⚠️  Chat Server health check - Service running but endpoint not responding${NC}"
        fi
    else
        echo -e "  ${YELLOW}⚠️  Chat Server - Service not running${NC}"
    fi
fi

if incus list -c n --format csv | 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 2 curl -s -f http://localhost:3002/health >/dev/null 2>&1; then
            echo -e "  ${GREEN}✅ Stream Server health check - OK${NC}"
        else
            echo -e "  ${YELLOW}⚠️  Stream Server health check - Service running but endpoint not responding${NC}"
        fi
    else
        echo -e "  ${YELLOW}⚠️  Stream Server - Service not running${NC}"
    fi
fi

if incus list -c n --format csv | 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 2 curl -s -f http://localhost:80 >/dev/null 2>&1; then
            echo -e "  ${GREEN}✅ Web Frontend - OK${NC}"
        else
            echo -e "  ${YELLOW}⚠️  Web Frontend - Apache running but not responding${NC}"
        fi
    else
        echo -e "  ${YELLOW}⚠️  Web Frontend - Apache not running${NC}"
    fi
fi

if incus list -c n --format csv | 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 2 curl -s -f http://localhost:80 >/dev/null 2>&1; then
            echo -e "  ${GREEN}✅ HAProxy - OK${NC}"
        else
            echo -e "  ${YELLOW}⚠️  HAProxy - Service running but not responding${NC}"
        fi
    else
        echo -e "  ${YELLOW}⚠️  HAProxy - Service not running${NC}"
    fi
fi

echo ""
echo -e "${GREEN}✅ Verification complete!${NC}"
