#!/bin/bash

# WP-CLI Test Data Cleanup Script
# Uses WordPress CLI commands to clean up test data safely

source .env

echo "========================================="
echo "🧹 WP-CLI TEST DATA CLEANUP"
echo "========================================="
echo "Target: $UPSKILL_STAGING_IP"
echo ""

# Function to run WP-CLI commands on staging
run_wp_cli() {
    local cmd="$1"
    echo "🔧 Running: wp $cmd"
    ssh -o StrictHostKeyChecking=no $UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP "cd $UPSKILL_STAGING_PATH && wp $cmd"
}

echo "🚀 Starting WP-CLI cleanup process..."
echo ""

# 1. Remove test users
echo "👥 REMOVING TEST USERS"
echo "======================="

# Remove test_trainer user
echo "Removing test_trainer user..."
run_wp_cli "user delete test_trainer --reassign=1 --yes"

# Remove joemedosch user  
echo "Removing joemedosch user..."
run_wp_cli "user delete joemedosch --reassign=1 --yes"

# Remove user by email if username doesn't work
echo "Removing JoeMedosch@gmail.com..."
run_wp_cli "user list --field=ID --user_email=JoeMedosch@gmail.com" | while read user_id; do
    if [ -n "$user_id" ]; then
        run_wp_cli "user delete $user_id --reassign=1 --yes"
    fi
done

# Remove HVAC roles from joe@measurequick.com
echo "Removing HVAC roles from joe@measurequick.com..."
run_wp_cli "user remove-role joe@measurequick.com hvac_trainer" || true
run_wp_cli "user remove-role joe@measurequick.com hvac_master_trainer" || true

echo ""

# 2. Remove test events with specific patterns
echo "📅 REMOVING TEST EVENTS"
echo "======================="

# Get event IDs for known test patterns
test_patterns=("HVAC System Diagnostics" "Commercial Refrigeration" "Energy Efficient HVAC" "Advanced HVAC Troubleshooting" "HVAC Energy Efficiency Workshop" "AUER STEEL")

for pattern in "${test_patterns[@]}"; do
    echo "Looking for events with pattern: $pattern"
    # Get post IDs matching the pattern
    ssh -o StrictHostKeyChecking=no $UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP "cd $UPSKILL_STAGING_PATH && wp post list --post_type=tribe_events --s='$pattern' --field=ID --format=csv" | while read event_id; do
        if [ -n "$event_id" ] && [ "$event_id" != "ID" ]; then
            echo "Removing event ID: $event_id"
            
            # Remove associated attendees first
            run_wp_cli "post list --post_type=tribe_tpp_attendees --meta_key=_tribe_tpp_event --meta_value=$event_id --field=ID --format=csv" | while read attendee_id; do
                if [ -n "$attendee_id" ] && [ "$attendee_id" != "ID" ]; then
                    run_wp_cli "post delete $attendee_id --force"
                fi
            done
            
            # Remove associated tickets
            run_wp_cli "post list --post_type=tribe_tpp_tickets --meta_key=_tribe_tpp_for_event --meta_value=$event_id --field=ID --format=csv" | while read ticket_id; do
                if [ -n "$ticket_id" ] && [ "$ticket_id" != "ID" ]; then
                    run_wp_cli "post delete $ticket_id --force"
                fi
            done
            
            # Remove the event
            run_wp_cli "post delete $event_id --force"
        fi
    done
done

echo ""

# 3. Remove test attendees with @example.com emails
echo "🎟️  REMOVING TEST ATTENDEES"
echo "==========================="

echo "Removing attendees with @example.com emails..."
ssh -o StrictHostKeyChecking=no $UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP "cd $UPSKILL_STAGING_PATH && wp post list --post_type=tribe_tpp_attendees --meta_key=_tribe_tickets_email --meta_value='@example.com' --meta_compare=LIKE --field=ID --format=csv" | while read attendee_id; do
    if [ -n "$attendee_id" ] && [ "$attendee_id" != "ID" ]; then
        echo "Removing test attendee ID: $attendee_id"
        run_wp_cli "post delete $attendee_id --force"
    fi
done

echo ""

# 4. Remove obvious test organizers
echo "🏢 REMOVING TEST ORGANIZERS"
echo "==========================="

# Remove organizers with title exactly "Test" or "BenTest"
echo "Removing 'Test' organizer..."
ssh -o StrictHostKeyChecking=no $UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP "cd $UPSKILL_STAGING_PATH && wp post list --post_type=tribe_organizer --post_title='Test' --field=ID --format=csv" | while read org_id; do
    if [ -n "$org_id" ] && [ "$org_id" != "ID" ]; then
        echo "Removing Test organizer ID: $org_id"
        run_wp_cli "post delete $org_id --force"
    fi
done

echo "Removing 'BenTest' organizer..."
ssh -o StrictHostKeyChecking=no $UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP "cd $UPSKILL_STAGING_PATH && wp post list --post_type=tribe_organizer --s='BenTest' --field=ID --format=csv" | while read org_id; do
    if [ -n "$org_id" ] && [ "$org_id" != "ID" ]; then
        echo "Removing BenTest organizer ID: $org_id"
        run_wp_cli "post delete $org_id --force"
    fi
done

echo ""

# 5. Remove test venues
echo "🏛️  REMOVING TEST VENUES"
echo "========================"

echo "Removing 'Woolworth Training Center' venue..."
ssh -o StrictHostKeyChecking=no $UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP "cd $UPSKILL_STAGING_PATH && wp post list --post_type=tribe_venue --s='Woolworth Training Center' --field=ID --format=csv" | while read venue_id; do
    if [ -n "$venue_id" ] && [ "$venue_id" != "ID" ]; then
        echo "Removing test venue ID: $venue_id"
        run_wp_cli "post delete $venue_id --force"
    fi
done

echo ""

# 6. Clear caches
echo "🧽 CLEARING CACHES"
echo "=================="
run_wp_cli "cache flush"
run_wp_cli "breeze purge --all" || true

echo ""
echo "========================================="
echo "✅ WP-CLI CLEANUP COMPLETED!"
echo "========================================="
echo ""

# Run verification to see results
echo "🔍 Running verification to check results..."
./scripts/verify-test-data.sh