#!/bin/bash
# Analyze all Cloudflare domains for tunnel configurations and issues

set -e

echo "═══════════════════════════════════════════════════════════"
echo "  Cloudflare Domains Analysis"
echo "═══════════════════════════════════════════════════════════"
echo ""

DOMAINS=(
    "commcourts.org"
    "d-bis.org"
    "defi-oracle.io"
    "ibods.org"
    "mim4u.org"
    "sankofa.nexus"
)

echo "Domains to analyze:"
for domain in "${DOMAINS[@]}"; do
    echo "  - $domain"
done
echo ""

# Check if Cloudflare API credentials are available
if [ -z "$CLOUDFLARE_API_TOKEN" ] && [ -z "$CLOUDFLARE_EMAIL" ] || [ -z "$CLOUDFLARE_API_KEY" ]; then
    echo "⚠️  Cloudflare API credentials not found in environment"
    echo ""
    echo "To use API analysis, set:"
    echo "  export CLOUDFLARE_API_TOKEN=your-token"
    echo "  # OR"
    echo "  export CLOUDFLARE_EMAIL=your-email"
    echo "  export CLOUDFLARE_API_KEY=your-key"
    echo ""
    echo "Continuing with DNS-based analysis..."
    echo ""
fi

# Analyze each domain
for domain in "${DOMAINS[@]}"; do
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "Analyzing: $domain"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo ""
    
    # Check DNS records
    echo "DNS Records:"
    if command -v dig &> /dev/null; then
        # Get NS records
        NS_RECORDS=$(dig +short NS "$domain" 2>/dev/null | head -2)
        if [ -n "$NS_RECORDS" ]; then
            echo "  Name Servers:"
            echo "$NS_RECORDS" | while read ns; do
                echo "    - $ns"
            done
        fi
        
        # Get A records
        A_RECORDS=$(dig +short A "$domain" 2>/dev/null)
        if [ -n "$A_RECORDS" ]; then
            echo "  A Records:"
            echo "$A_RECORDS" | while read ip; do
                echo "    - $ip"
            done
        fi
        
        # Get CNAME records (for subdomains)
        CNAME_COUNT=$(dig +short "$domain" ANY 2>/dev/null | grep -c "CNAME" || echo "0")
        if [ "$CNAME_COUNT" -gt 0 ]; then
            echo "  CNAME Records: $CNAME_COUNT found"
        fi
    else
        echo "  ⚠️  'dig' not available - install bind-utils or dnsutils"
    fi
    
    echo ""
    
    # Check for tunnel references
    echo "Tunnel Analysis:"
    case "$domain" in
        "d-bis.org")
            echo "  ✅ Analyzed - See DNS_ANALYSIS.md"
            echo "  ⚠️  Issues: Shared tunnel down, low TTL"
            ;;
        "mim4u.org")
            echo "  ⚠️  CONFLICT: Also exists as subdomain mim4u.org.d-bis.org"
            echo "  Action: Resolve naming conflict"
            ;;
        "sankofa.nexus")
            echo "  ℹ️  Matches infrastructure naming"
            echo "  Potential: Infrastructure management domain"
            ;;
        *)
            echo "  ❓ Not yet analyzed"
            ;;
    esac
    
    echo ""
    
    # Check if domain is accessible
    echo "Connectivity:"
    if command -v curl &> /dev/null; then
        HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "https://$domain" 2>/dev/null || echo "000")
        if [ "$HTTP_CODE" != "000" ] && [ "$HTTP_CODE" != "000" ]; then
            echo "  ✅ HTTPS accessible (HTTP $HTTP_CODE)"
        else
            echo "  ⚠️  HTTPS not accessible or timeout"
        fi
    else
        echo "  ⚠️  'curl' not available"
    fi
    
    echo ""
    echo ""
done

echo "═══════════════════════════════════════════════════════════"
echo "  Analysis Complete"
echo "═══════════════════════════════════════════════════════════"
echo ""
echo "Next Steps:"
echo "  1. Review ALL_DOMAINS_ANALYSIS.md for detailed findings"
echo "  2. Fix d-bis.org issues: ./fix-shared-tunnel.sh"
echo "  3. Resolve mim4u.org conflict"
echo "  4. Analyze remaining domains in Cloudflare Dashboard"
echo ""
