#!/usr/bin/env bash
# Test Explorer Functionality
# Runs basic tests on the deployed explorer

set -euo pipefail

DOMAIN="${DOMAIN:-explorer.d-bis.org}"

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

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

PASSED=0
FAILED=0

test_endpoint() {
    local name="$1"
    local url="$2"
    
    if curl -k -sI "$url" 2>&1 | grep -qi "HTTP.*200"; then
        log_success "$name: Passed"
        ((PASSED++))
        return 0
    else
        log_error "$name: Failed"
        ((FAILED++))
        return 1
    fi
}

echo "════════════════════════════════════════════════════════"
echo "Explorer Functionality Test"
echo "════════════════════════════════════════════════════════"
echo ""

test_endpoint "Home Page" "https://$DOMAIN/"
test_endpoint "API Stats" "https://$DOMAIN/api/v2/stats"

echo ""
echo "Tests Passed: $PASSED"
echo "Tests Failed: $FAILED"

if [ $FAILED -eq 0 ]; then
    exit 0
else
    exit 1
fi

