#!/bin/bash

echo "🔍 Checking TEC Community Events Status on Staging"
echo "=================================================="

source .env

# SSH and check various aspects
sshpass -p "$STAGING_SSH_PASSWORD" ssh -o StrictHostKeyChecking=no roodev@$UPSKILL_STAGING_IP << 'EOF'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

echo -e "\n1. Plugin Status:"
echo "----------------"
wp plugin list | grep -i "event"

echo -e "\n2. Check if TEC Community Events is active:"
echo "-------------------------------------------"
wp plugin is-active the-events-calendar-community-events && echo "✅ Active" || echo "❌ Not Active"

echo -e "\n3. Check TEC Community Events version and files:"
echo "------------------------------------------------"
if [ -d "wp-content/plugins/the-events-calendar-community-events" ]; then
    echo "Plugin directory exists"
    ls -la wp-content/plugins/the-events-calendar-community-events/ | head -5
    if [ -f "wp-content/plugins/the-events-calendar-community-events/tribe-community-events.php" ]; then
        grep "Version:" wp-content/plugins/the-events-calendar-community-events/tribe-community-events.php | head -1
    fi
else
    echo "Plugin directory NOT found"
fi

echo -e "\n4. Check recent error logs:"
echo "---------------------------"
if [ -f "wp-content/debug.log" ]; then
    echo "Last 20 lines of debug.log related to TEC:"
    tail -50 wp-content/debug.log | grep -i "tribe\|event\|community" | tail -20
else
    echo "No debug.log found"
fi

echo -e "\n5. Check rewrite rules for /events/:"
echo "------------------------------------"
wp rewrite list | grep -i "events" | head -10

echo -e "\n6. Check user capabilities:"
echo "---------------------------"
wp user meta get $(wp user get test_trainer --field=ID) wp_capabilities 2>/dev/null

echo -e "\n7. Check if main TEC plugin is active:"
echo "--------------------------------------"
wp plugin is-active the-events-calendar && echo "✅ Main TEC Active" || echo "❌ Main TEC Not Active"

echo -e "\n8. Check TEC database tables:"
echo "-----------------------------"
wp db query "SHOW TABLES LIKE '%tribe%'" 2>/dev/null | head -10

echo -e "\n9. Check active theme:"
echo "----------------------"
wp theme list --status=active

echo -e "\n10. Check site URL settings:"
echo "----------------------------"
wp option get siteurl
wp option get home

EOF

echo -e "\n✅ Status check complete"