#!/usr/bin/env bash
# Test cross-system consistency between Cloudflare, Omada, Proxmox, and hardware inventory
# Verifies connectivity and configuration consistency

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

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

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }

# Load inventory
source "$SCRIPT_DIR/load-physical-inventory.sh" 2>/dev/null || {
    log_error "Failed to load physical hardware inventory"
    exit 1
}

log_info "=== Cross-System Consistency Test ==="
echo ""

PASSED=0
FAILED=0
SKIPPED=0

# Test function
test_check() {
    local name="$1"
    local test_cmd="$2"
    
    log_info "Testing: $name"
    if eval "$test_cmd" >/dev/null 2>&1; then
        log_success "  ✓ $name"
        ((PASSED++))
        return 0
    else
        log_error "  ✗ $name"
        ((FAILED++))
        return 1
    fi
}

# Test Omada Controller (Local)
OMADA_IP=$(get_controller_ip)
OMADA_PORT=$(get_controller_port)

test_check "Omada Controller IP from inventory" "[[ '$OMADA_IP' == '192.168.11.8' ]]"
test_check "Omada Controller Port from inventory" "[[ '$OMADA_PORT' == '8043' ]]"

# Test Omada Cloud Controller
OMADA_CLOUD_OAUTH=$(get_omada_cloud_oauth_url)
OMADA_CLOUD_API=$(get_omada_cloud_api_url)
OMADA_CLOUD_ID=$(get_omada_cloud_id)

test_check "Omada Cloud OAuth URL configured" "[[ -n '$OMADA_CLOUD_OAUTH' ]]"
test_check "Omada Cloud API URL configured" "[[ -n '$OMADA_CLOUD_API' ]]"
test_check "Omada Cloud ID configured" "[[ -n '$OMADA_CLOUD_ID' ]]"

# Test ER605 IPs
ER605_1_IP=$(get_gateway_ip "er605-1")
ER605_2_IP=$(get_gateway_ip "er605-2")

test_check "ER605-1 IP from inventory" "[[ '$ER605_1_IP' == '76.53.10.34' ]]"
test_check "ER605-2 IP from inventory" "[[ '$ER605_2_IP' == '76.53.10.41' ]]"

# Test ER605 MAC and Spectrum IP
ER605_1_MAC=$(get_gateway_mac_address "er605-1")
ER605_1_SPECTRUM=$(get_gateway_spectrum_ip "er605-1")

test_check "ER605-1 MAC address configured" "[[ -n '$ER605_1_MAC' ]]"
test_check "ER605-1 Spectrum IP configured" "[[ -n '$ER605_1_SPECTRUM' ]]"

# Test Proxmox Hosts
for host in ml110 r630-01 r630-02 r630-03 r630-04; do
    host_ip=$(get_host_ip "$host")
    if [[ -n "$host_ip" ]]; then
        test_check "Proxmox host $host IP from inventory" "[[ -n '$host_ip' ]]"
    else
        log_warn "  ⚠ Proxmox host $host not found in inventory"
        ((SKIPPED++))
    fi
done

# Test Omada API Credentials
OMADA_API_CLIENT_ID=$(get_omada_api_client_id)
OMADA_API_CLIENT_SECRET=$(get_omada_api_client_secret)

test_check "Omada API Client ID (proxmox-controller) configured" "[[ -n '$OMADA_API_CLIENT_ID' ]]"
test_check "Omada API Client Secret (proxmox-controller) configured" "[[ -n '$OMADA_API_CLIENT_SECRET' ]]"

# Test Cloudflare Tunnel Configurations
TUNNEL_CONFIGS=(
    "scripts/cloudflare-tunnels/configs/tunnel-ml110.yml"
    "scripts/cloudflare-tunnels/configs/tunnel-r630-01.yml"
    "scripts/cloudflare-tunnels/configs/tunnel-r630-02.yml"
    "scripts/cloudflare-tunnels/configs/tunnel-r630-03.yml"
    "scripts/cloudflare-tunnels/configs/tunnel-r630-04.yml"
)

for config in "${TUNNEL_CONFIGS[@]}"; do
    config_path="$PROJECT_ROOT/$config"
    if [[ -f "$config_path" ]]; then
        test_check "Cloudflare tunnel config exists: $(basename $config)" "[[ -f '$config_path' ]]"
    else
        log_warn "  ⚠ Tunnel config not found: $config"
        ((SKIPPED++))
    fi
done

# Summary
echo ""
log_info "=== Test Summary ==="
log_success "Passed: $PASSED"
if [[ $FAILED -gt 0 ]]; then
    log_error "Failed: $FAILED"
fi
if [[ $SKIPPED -gt 0 ]]; then
    log_warn "Skipped: $SKIPPED"
fi

if [[ $FAILED -eq 0 ]]; then
    log_success "✓ All consistency checks passed"
    exit 0
else
    log_error "✗ Some consistency checks failed"
    exit 1
fi
