#!/bin/bash
# BTCPay Server Diagnostic Script
# Run this from your local machine

echo "=== BTCPay Server Remote Diagnostics ==="
echo "Testing: thebankofdebbie.giize.com"
echo "Date: $(date)"
echo ""

# 1. Test DNS resolution
echo "1. DNS Resolution:"
nslookup thebankofdebbie.giize.com | grep -A1 "Name:"
echo ""

# 2. Test HTTP/HTTPS connectivity
echo "2. HTTP/HTTPS Status:"
echo -n "   HTTP (80): "
curl -s -o /dev/null -w "%{http_code}" -m 5 http://thebankofdebbie.giize.com
echo ""
echo -n "   HTTPS (443): "
curl -s -o /dev/null -w "%{http_code}" -m 5 https://thebankofdebbie.giize.com
echo ""

# 3. Check what's actually being served
echo "3. Server Response Headers:"
curl -I -s https://thebankofdebbie.giize.com | head -10
echo ""

# 4. Test specific BTCPay endpoints
echo "4. BTCPay Endpoints:"
echo -n "   /api/v1/health: "
curl -s -o /dev/null -w "%{http_code}" -m 5 https://thebankofdebbie.giize.com/api/v1/health
echo ""
echo -n "   /api/v1/server/info: "
curl -s -o /dev/null -w "%{http_code}" -m 5 https://thebankofdebbie.giize.com/api/v1/server/info
echo ""

# 5. Check error details
echo "5. Error Details (if any):"
curl -s -m 5 https://thebankofdebbie.giize.com 2>&1 | grep -E "502|503|504|Bad Gateway|Service Unavailable" | head -5
echo ""

# 6. Test SSH connectivity
echo "6. SSH Connectivity Tests:"
echo -n "   Port 22: "
nc -zv -w 2 thebankofdebbie.giize.com 22 2>&1 | grep -o "succeeded\|refused\|timed out"
echo -n "   Port 2255: "
nc -zv -w 2 thebankofdebbie.giize.com 2255 2>&1 | grep -o "succeeded\|refused\|timed out"
echo ""

# 7. Try emergency access instructions
echo "7. Manual Access Instructions:"
echo "   If you can access via console/VNC from Hostinger panel:"
echo "   a) Login as root with password: Th3fa1r13sd1d1t."
echo "   b) Run: docker ps -a"
echo "   c) Run: cd /opt/btcpayserver-docker && ./btcpay-restart.sh"
echo "   d) Check logs: docker logs generated_btcpayserver_1 --tail 50"
echo ""

# 8. Alternative access methods
echo "8. Alternative Access Methods:"
echo "   - Hostinger Control Panel: https://hpanel.hostinger.com/"
echo "   - VNC/Console access from control panel"
echo "   - Support ticket if server is down"
echo ""

echo "=== Summary ==="
if curl -s -o /dev/null -w "%{http_code}" https://thebankofdebbie.giize.com | grep -q "502"; then
    echo "STATUS: Bad Gateway (502) - BTCPay container likely down"
    echo "ACTION: Need to restart BTCPay services via console access"
elif curl -s -o /dev/null -w "%{http_code}" https://thebankofdebbie.giize.com | grep -q "200"; then
    echo "STATUS: Site appears to be working (200 OK)"
else
    echo "STATUS: Unknown issue - check manually"
fi