#!/bin/bash

echo "🔄 Cache busting nginx reverse proxy..."

# Clear nginx cache if it exists (common locations)
ssh mjack@metajack.org '
# Try common nginx cache locations
sudo find /var/cache/nginx -type f -delete 2>/dev/null || echo "No nginx cache at /var/cache/nginx"
sudo find /tmp/nginx -type f -delete 2>/dev/null || echo "No nginx cache at /tmp/nginx"
sudo find /var/lib/nginx/cache -type f -delete 2>/dev/null || echo "No nginx cache at /var/lib/nginx/cache"

# Reload nginx to clear any in-memory cache
sudo nginx -s reload 2>/dev/null || echo "Could not reload nginx (may need different command)"

echo "✅ Nginx cache clearing attempted"
'

echo "🔧 Adding cache-busting headers to Laravel..."

# Add cache-busting to the application
ssh mjack@metajack.org '
cd ~/private

# Add a timestamp query parameter to force cache refresh
echo "Adding cache bust parameter..."
/usr/local/bin/php8.2 artisan tinker --execute="
use Illuminate\Support\Facades\Cache;
Cache::put(\"app_version\", time(), 3600);
echo \"Cache bust version: \" . time() . PHP_EOL;
"
'

echo "🌐 Test with these cache-busting URLs:"
echo "https://xeek.com/?v=$(date +%s)"
echo "Or use Ctrl+Shift+R (hard refresh) in your browser"