#!/bin/bash

# Simple Test Data Cleanup via SSH
# Uses WordPress CLI commands to clean up test data

source .env

echo "========================================="
echo "🧹 SIMPLE TEST DATA CLEANUP"
echo "========================================="
echo "Target: $UPSKILL_STAGING_IP"
echo ""

# Create a simple PHP cleanup script
cat > /tmp/cleanup-test-data.php << 'EOF'
<?php
require_once('wp-load.php');

echo "=== Simple Cleanup Starting ===\n";

// 1. Remove test users
$test_users = ['test_trainer', 'joemedosch'];
foreach ($test_users as $username) {
    $user = get_user_by('login', $username);
    if ($user) {
        echo "Removing user: {$username}\n";
        wp_delete_user($user->ID, 1); // Reassign content to admin
    }
}

// Remove JoeMedosch by email
$joe_user = get_user_by('email', 'JoeMedosch@gmail.com');
if ($joe_user) {
    echo "Removing JoeMedosch@gmail.com\n";
    wp_delete_user($joe_user->ID, 1);
}

// Remove HVAC roles from joe@measurequick.com
$joe_mq = get_user_by('email', 'joe@measurequick.com');
if ($joe_mq) {
    echo "Removing HVAC roles from joe@measurequick.com\n";
    $user = new WP_User($joe_mq->ID);
    $user->remove_role('hvac_trainer');
    $user->remove_role('hvac_master_trainer');
}

// 2. Remove test events by title patterns
$test_patterns = ['HVAC System Diagnostics', 'Commercial Refrigeration', 'Energy Efficient HVAC', 'AUER STEEL'];
foreach ($test_patterns as $pattern) {
    $events = get_posts([
        'post_type' => 'tribe_events',
        's' => $pattern,
        'posts_per_page' => -1
    ]);
    
    foreach ($events as $event) {
        echo "Removing event: {$event->post_title}\n";
        
        // Remove associated attendees
        $attendees = get_posts([
            'post_type' => 'tribe_tpp_attendees',
            'meta_query' => [['key' => '_tribe_tpp_event', 'value' => $event->ID]],
            'posts_per_page' => -1
        ]);
        foreach ($attendees as $attendee) {
            wp_delete_post($attendee->ID, true);
        }
        
        // Remove associated tickets
        $tickets = get_posts([
            'post_type' => 'tribe_tpp_tickets',
            'meta_query' => [['key' => '_tribe_tpp_for_event', 'value' => $event->ID]],
            'posts_per_page' => -1
        ]);
        foreach ($tickets as $ticket) {
            wp_delete_post($ticket->ID, true);
        }
        
        wp_delete_post($event->ID, true);
    }
}

// 3. Remove test attendees with @example.com emails
$attendees = get_posts([
    'post_type' => 'tribe_tpp_attendees',
    'meta_query' => [['key' => '_tribe_tickets_email', 'value' => '@example.com', 'compare' => 'LIKE']],
    'posts_per_page' => -1
]);
foreach ($attendees as $attendee) {
    echo "Removing test attendee: {$attendee->post_title}\n";
    wp_delete_post($attendee->ID, true);
}

// 4. Remove obvious test organizers
$organizers = get_posts(['post_type' => 'tribe_organizer', 'posts_per_page' => -1]);
foreach ($organizers as $organizer) {
    if (stripos($organizer->post_title, 'test') !== false || 
        stripos($organizer->post_title, 'bentest') !== false) {
        echo "Removing test organizer: {$organizer->post_title}\n";
        wp_delete_post($organizer->ID, true);
    }
}

// 5. Remove test venues
$venues = get_posts(['post_type' => 'tribe_venue', 'posts_per_page' => -1]);
foreach ($venues as $venue) {
    if (stripos($venue->post_title, 'training center') !== false || 
        stripos($venue->post_title, 'test') !== false) {
        echo "Removing test venue: {$venue->post_title}\n";
        wp_delete_post($venue->ID, true);
    }
}

wp_cache_flush();
echo "=== Simple Cleanup Complete ===\n";
?>
EOF

# Upload and execute the cleanup script
echo "📤 Uploading cleanup script..."
scp /tmp/cleanup-test-data.php $UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP:~/

echo "🧹 Executing cleanup..."
ssh $UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP "cd $UPSKILL_STAGING_PATH && php ~/cleanup-test-data.php && rm ~/cleanup-test-data.php"

# Clean up local temp file
rm /tmp/cleanup-test-data.php

echo ""
echo "✅ Simple cleanup completed!"