#!/bin/bash

# Nginx Proxy Cache Clear Script
# Run this on your NGINX PROXY LXC (not the backend LXC!)

echo "🧹 LuckyChit - Nginx Proxy Cache Clear"
echo "======================================"
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo "⚠️  This script needs sudo privileges"
    echo "Run with: sudo ./nginx-clear-cache.sh"
    exit 1
fi

echo "📊 Step 1/5: Checking nginx status..."
systemctl status nginx --no-pager | head -n 3

echo ""
echo "🔍 Step 2/5: Finding nginx cache directories..."
CACHE_DIRS=$(find /var -name "*cache*" -type d 2>/dev/null | grep nginx)

if [ -z "$CACHE_DIRS" ]; then
    echo "No nginx cache directories found"
else
    echo "Found cache directories:"
    echo "$CACHE_DIRS"
fi

echo ""
echo "🗑️  Step 3/5: Clearing nginx cache..."

# Clear common cache locations
rm -rf /var/cache/nginx/* 2>/dev/null && echo "✅ Cleared /var/cache/nginx/" || echo "⚠️  /var/cache/nginx/ not found"
rm -rf /var/lib/nginx/cache/* 2>/dev/null && echo "✅ Cleared /var/lib/nginx/cache/" || echo "⚠️  /var/lib/nginx/cache/ not found"
rm -rf /var/run/nginx-cache/* 2>/dev/null && echo "✅ Cleared /var/run/nginx-cache/" || echo "⚠️  /var/run/nginx-cache/ not found"
rm -rf /tmp/nginx/* 2>/dev/null && echo "✅ Cleared /tmp/nginx/" || echo "⚠️  /tmp/nginx/ not found"

echo ""
echo "✅ Step 4/5: Testing nginx configuration..."
nginx -t

if [ $? -eq 0 ]; then
    echo ""
    echo "♻️  Step 5/5: Reloading nginx..."
    systemctl reload nginx
    
    if [ $? -eq 0 ]; then
        echo "✅ Nginx reloaded successfully!"
    else
        echo "❌ Failed to reload nginx"
        echo "Try: systemctl restart nginx"
        exit 1
    fi
else
    echo "❌ Nginx configuration has errors!"
    echo "Fix configuration before reloading"
    exit 1
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🧪 Testing Backend Connectivity"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Test backend connectivity
echo ""
echo "Testing backend (192.168.8.148:3000)..."
BACKEND_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://192.168.8.148:3000/health 2>/dev/null)

if [ "$BACKEND_RESPONSE" = "200" ]; then
    echo "✅ Backend API responding: HTTP $BACKEND_RESPONSE"
else
    echo "❌ Backend API not responding: HTTP $BACKEND_RESPONSE"
    echo "   Check if PM2 is running on backend LXC"
fi

echo ""
echo "Testing frontend (192.168.8.148:8080)..."
FRONTEND_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://192.168.8.148:8080 2>/dev/null)

if [ "$FRONTEND_RESPONSE" = "200" ]; then
    echo "✅ Frontend responding: HTTP $FRONTEND_RESPONSE"
else
    echo "❌ Frontend not responding: HTTP $FRONTEND_RESPONSE"
    echo "   Check if PM2 is running on backend LXC"
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "✅ Cache clearing complete!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "🌐 Now test your site:"
echo "   https://chitfund.deepteklabs.com"
echo ""
echo "💡 Don't forget to hard refresh in browser:"
echo "   Windows: Ctrl + Shift + R"
echo "   Mac: Cmd + Shift + R"
echo ""

