#!/bin/bash
set -e

# TEC Setup Verification Script
# Checks TEC Community Events plugin setup and URL availability

# Load environment variables
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

if [ -f "$PROJECT_ROOT/.env" ]; then
    export $(cat "$PROJECT_ROOT/.env" | sed 's/#.*//g' | xargs)
fi

ENVIRONMENT="${1:-staging}"

if [ "$ENVIRONMENT" = "staging" ]; then
    SERVER_IP=$UPSKILL_STAGING_IP
    SSH_USER=$UPSKILL_STAGING_SSH_USER
    SSH_PASS=$UPSKILL_STAGING_PASS
    SERVER_PATH=$UPSKILL_STAGING_PATH
    SITE_URL=$UPSKILL_STAGING_URL
else
    SERVER_IP=$UPSKILL_PROD_IP
    SSH_USER=$UPSKILL_PROD_SSH_USER
    SSH_PASS=$UPSKILL_PROD_SSH_PASS
    SERVER_PATH=$UPSKILL_PROD_PATH
    SITE_URL=$UPSKILL_PROD_URL
fi

echo "=== TEC Community Events Setup Verification ==="
echo "Environment: $ENVIRONMENT"
echo "Site URL: $SITE_URL"
echo ""

echo "Step 1: Checking TEC plugin status..."
sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SERVER_IP" "
cd $SERVER_PATH

echo 'Active TEC plugins:'
wp plugin list --status=active | grep -i 'events\|calendar\|community' || echo 'No TEC plugins found active'

echo ''
echo 'TEC Community Events plugin status:'
wp plugin list --name='the-events-calendar-community-events' --format=table 2>/dev/null || echo 'Plugin not found'

echo ''
echo 'All Events-related plugins:'
wp plugin list | grep -i 'events\|calendar\|community' || echo 'No events plugins found'
"

echo ""
echo "Step 2: Checking TEC Community Events settings and URLs..."
sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SERVER_IP" "
cd $SERVER_PATH

echo 'TEC Community Events options:'
wp option get tribe_events_calendar_options 2>/dev/null | grep -A 5 -B 5 'community\|submit' || echo 'No community options found'

echo ''
echo 'Checking rewrite rules:'
wp rewrite list | grep -i 'event\|community' | head -10 || echo 'No event rewrite rules found'

echo ''
echo 'Looking for TEC Community template files:'
find wp-content/themes -name '*community*' -o -name '*edit-event*' 2>/dev/null || echo 'No community templates found in themes'

echo ''
echo 'Checking TEC Community Events plugin files:'
ls -la wp-content/plugins/ | grep -i 'community\|events' || echo 'No TEC plugins found'

if [ -d 'wp-content/plugins/the-events-calendar-community-events' ]; then
    echo 'TEC Community Events plugin directory contents:'
    ls -la wp-content/plugins/the-events-calendar-community-events/ | head -10
fi
"

echo ""
echo "Step 3: Testing TEC URLs via HTTP..."

URLS_TO_TEST=(
    "/events/"
    "/events/add/"
    "/events/community/"
    "/events/community/add/"
    "/community/events/"
    "/community/events/add/"
    "/submit-event/"
    "/event-submission/"
    "/add-event/"
)

for url in "${URLS_TO_TEST[@]}"; do
    echo "Testing: $SITE_URL$url"
    response=$(curl -s -o /dev/null -w "%{http_code}" -L "$SITE_URL$url" || echo "000")
    if [ "$response" = "200" ]; then
        echo "✅ $url - Accessible (200)"
        # Check for TEC form content
        content=$(curl -s -L "$SITE_URL$url" | grep -i "event.*form\|community.*event\|submit.*event" | head -3 || echo "")
        if [ ! -z "$content" ]; then
            echo "   📝 Contains event form content"
        fi
    elif [ "$response" = "404" ]; then
        echo "❌ $url - Not Found (404)"
    else
        echo "⚠️  $url - Status: $response"
    fi
done

echo ""
echo "Step 4: Checking template override installation..."
sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SERVER_IP" "
cd $SERVER_PATH

ACTIVE_THEME=\$(wp option get stylesheet 2>/dev/null | tr -d '\n')
echo \"Active theme: \$ACTIVE_THEME\"

if [ -f \"wp-content/themes/\$ACTIVE_THEME/tribe-events/community/edit-event.php\" ]; then
    echo '✅ Enhanced template override is installed'
    echo 'Template file info:'
    ls -la \"wp-content/themes/\$ACTIVE_THEME/tribe-events/community/edit-event.php\"
    
    echo 'Template content check (first 10 lines):'
    head -10 \"wp-content/themes/\$ACTIVE_THEME/tribe-events/community/edit-event.php\" | grep -i 'hvac\|enhanced' || echo 'No HVAC/Enhanced markers found'
else
    echo '❌ Enhanced template override not found'
fi
"

echo ""
echo "Step 5: Manual URL suggestions..."
echo "Try these URLs manually in browser:"
echo "1. Main events page: $SITE_URL/events/"
echo "2. Community add: $SITE_URL/events/community/add/"
echo "3. Simple add: $SITE_URL/events/add/"
echo "4. Dashboard: $SITE_URL/trainer/dashboard/"
echo ""
echo "Look for 'Submit Event' or 'Add Event' links on the events page."