#!/bin/bash

# FORCE Production Pre-Deployment Cleanup Script
# This version bypasses the confirmation prompt for automated cleanup

source .env

echo "========================================="
echo "🧹 FORCE PRODUCTION PRE-DEPLOYMENT CLEANUP"
echo "========================================="
echo "Target: $UPSKILL_STAGING_IP"
echo "Proceeding with automatic cleanup..."
echo ""

# Upload and execute comprehensive cleanup PHP script directly
sshpass -p "$UPSKILL_STAGING_PASS" scp -o StrictHostKeyChecking=no /dev/stdin $UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP:tmp/force-cleanup-test-data.php << 'PHPEOF'
<?php
/**
 * FORCE Production Pre-Deployment Test Data Cleanup
 * 
 * Removes all test data including:
 * - Test user accounts
 * - Test events and associated data
 * - Test certificates and files
 * - Test attendees and orders
 */

require_once('wp-load.php');

echo "=== Starting FORCE Production Pre-Deployment Cleanup ===\n\n";

// Initialize counters
$deleted_users = 0;
$deleted_events = 0;
$deleted_attendees = 0;
$deleted_certificates = 0;
$deleted_venues = 0;
$deleted_organizers = 0;
$deleted_tickets = 0;
$deleted_files = 0;

// 1. REMOVE TEST USER ACCOUNTS
echo "🗑️  Removing test user accounts...\n";

$test_users = [
    'test_trainer',
    'joemedosch',
    'JoeMedosch@gmail.com'  // search by email
];

foreach ($test_users as $identifier) {
    // Try to find user by login first, then by email
    $user = get_user_by('login', $identifier);
    if (!$user) {
        $user = get_user_by('email', $identifier);
    }
    
    if ($user) {
        echo "  - Removing user: {$user->user_login} ({$user->user_email})\n";
        
        // Remove user and reassign their content to admin (ID 1)
        $admin_user = get_user_by('ID', 1);
        if ($admin_user) {
            wp_delete_user($user->ID, 1); // Reassign to admin
        } else {
            wp_delete_user($user->ID); // Delete without reassignment
        }
        $deleted_users++;
    }
}

// Remove HVAC roles from joe@measurequick.com if they exist
$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');
}

echo "✅ Removed {$deleted_users} test user accounts\n\n";

// 2. REMOVE TEST EVENTS AND ASSOCIATED DATA
echo "🗑️  Removing test events and associated data...\n";

// Get all events that might be test events (look for common test patterns)
$test_event_patterns = [
    'HVAC System Diagnostics',
    'Commercial Refrigeration',
    'Energy Efficient HVAC',
    'Advanced HVAC Troubleshooting',
    'HVAC Energy Efficiency Workshop',
    'Commercial Refrigeration Systems',
    'Residential HVAC Installation Best Practices',
    'HVAC Controls and Automation',
    'AUER STEEL'  // Add this pattern that showed up in verification
];

$all_events = get_posts([
    'post_type' => 'tribe_events',
    'post_status' => 'any',
    'posts_per_page' => -1,
    'meta_query' => [
        'relation' => 'OR',
        [
            'key' => '_eventbrite_event_id',
            'compare' => 'NOT EXISTS'
        ],
        [
            'key' => '_eventbrite_event_id',
            'value' => '',
            'compare' => '='
        ]
    ]
]);

foreach ($all_events as $event) {
    $is_test_event = false;
    
    // Check if event title matches test patterns
    foreach ($test_event_patterns as $pattern) {
        if (stripos($event->post_title, $pattern) !== false) {
            $is_test_event = true;
            break;
        }
    }
    
    // Also check if event was created recently (within last 6 months) and has test-like characteristics
    $event_date = strtotime($event->post_date);
    $six_months_ago = strtotime('-6 months');
    
    if ($event_date > $six_months_ago) {
        // Check for test-like content
        $test_indicators = ['test', 'training center', 'example', 'dummy', 'sample'];
        $event_content = strtolower($event->post_title . ' ' . $event->post_content);
        
        foreach ($test_indicators as $indicator) {
            if (strpos($event_content, $indicator) !== false) {
                $is_test_event = true;
                break;
            }
        }
    }
    
    if ($is_test_event) {
        $event_id = $event->ID;
        echo "  - Removing event: {$event->post_title} (ID: {$event_id})\n";
        
        // Get associated attendees first
        $attendees = get_posts([
            'post_type' => 'tribe_tpp_attendees',
            'meta_query' => [
                [
                    'key' => '_tribe_tpp_event',
                    'value' => $event_id
                ]
            ],
            'posts_per_page' => -1
        ]);
        
        // Remove attendees
        foreach ($attendees as $attendee) {
            wp_delete_post($attendee->ID, true);
            $deleted_attendees++;
        }
        
        // Get associated tickets
        $tickets = get_posts([
            'post_type' => 'tribe_tpp_tickets',
            'meta_query' => [
                [
                    'key' => '_tribe_tpp_for_event',
                    'value' => $event_id
                ]
            ],
            'posts_per_page' => -1
        ]);
        
        // Remove tickets
        foreach ($tickets as $ticket) {
            wp_delete_post($ticket->ID, true);
            $deleted_tickets++;
        }
        
        // Get venue ID before deleting event
        $venue_id = get_post_meta($event_id, '_EventVenueID', true);
        
        // Remove the event
        wp_delete_post($event_id, true);
        $deleted_events++;
        
        // Remove associated venue if it looks like a test venue
        if ($venue_id) {
            $venue = get_post($venue_id);
            if ($venue && $venue->post_type === 'tribe_venue') {
                $test_venue_indicators = ['training center', 'test', 'example', 'demo'];
                $venue_content = strtolower($venue->post_title . ' ' . $venue->post_content);
                
                $is_test_venue = false;
                foreach ($test_venue_indicators as $indicator) {
                    if (strpos($venue_content, $indicator) !== false) {
                        $is_test_venue = true;
                        break;
                    }
                }
                
                if ($is_test_venue) {
                    wp_delete_post($venue_id, true);
                    $deleted_venues++;
                }
            }
        }
    }
}

echo "✅ Removed {$deleted_events} test events, {$deleted_attendees} attendees, {$deleted_tickets} tickets, {$deleted_venues} venues\n\n";

// 3. REMOVE TEST CERTIFICATES AND FILES
echo "🗑️  Removing test certificates and files...\n";

if (class_exists('HVAC_Certificate_Manager')) {
    global $wpdb;
    $certificate_table = $wpdb->prefix . 'hvac_certificates';
    
    // Get all certificates
    $certificates = $wpdb->get_results("SELECT * FROM {$certificate_table}");
    
    foreach ($certificates as $certificate) {
        $should_delete = false;
        
        // Check if associated event was deleted (event doesn't exist anymore)
        $event_exists = get_post($certificate->event_id);
        if (!$event_exists) {
            $should_delete = true;
        }
        
        // Check if certificate file looks like test data
        if (strpos($certificate->file_path, 'test') !== false || 
            strpos($certificate->file_path, 'example') !== false ||
            strpos($certificate->file_path, 'demo') !== false) {
            $should_delete = true;
        }
        
        if ($should_delete) {
            // Remove certificate file
            $upload_dir = wp_upload_dir();
            $full_file_path = $upload_dir['basedir'] . '/' . $certificate->file_path;
            
            if (file_exists($full_file_path)) {
                unlink($full_file_path);
                $deleted_files++;
            }
            
            // Remove certificate record
            $wpdb->delete($certificate_table, ['id' => $certificate->id]);
            $deleted_certificates++;
        }
    }
}

echo "✅ Removed {$deleted_certificates} test certificates and {$deleted_files} certificate files\n\n";

// 4. REMOVE CLEAR TEST ORGANIZERS ONLY
echo "🗑️  Removing clear test organizers...\n";

$organizers = get_posts([
    'post_type' => 'tribe_organizer',
    'post_status' => 'any',
    'posts_per_page' => -1
]);

foreach ($organizers as $organizer) {
    // Only remove organizers with VERY clear test indicators
    $clear_test_indicators = ['test', 'example', 'demo', 'sample', 'bentest'];
    $organizer_content = strtolower($organizer->post_title . ' ' . $organizer->post_content);
    
    $is_clear_test_organizer = false;
    foreach ($clear_test_indicators as $indicator) {
        if (strpos($organizer_content, $indicator) !== false) {
            echo "  - Removing test organizer: {$organizer->post_title} (ID: {$organizer->ID})\n";
            $is_clear_test_organizer = true;
            break;
        }
    }
    
    if ($is_clear_test_organizer) {
        wp_delete_post($organizer->ID, true);
        $deleted_organizers++;
    }
}

echo "✅ Removed {$deleted_organizers} clear test organizers\n\n";

// 5. CLEAN UP ORPHANED META DATA
echo "🗑️  Cleaning up orphaned meta data...\n";

// Clean up post meta for deleted posts
$wpdb->query("DELETE pm FROM {$wpdb->postmeta} pm LEFT JOIN {$wpdb->posts} p ON pm.post_id = p.ID WHERE p.ID IS NULL");

// Clean up user meta for deleted users  
$wpdb->query("DELETE um FROM {$wpdb->usermeta} um LEFT JOIN {$wpdb->users} u ON um.user_id = u.ID WHERE u.ID IS NULL");

echo "✅ Cleaned up orphaned meta data\n\n";

// 6. FINAL CLEANUP
echo "🗑️  Final cleanup tasks...\n";

// Clear any caches
if (function_exists('wp_cache_flush')) {
    wp_cache_flush();
}

// Clear object cache
if (function_exists('wp_cache_delete_group')) {
    wp_cache_delete_group('posts');
    wp_cache_delete_group('users');
}

// Clear transients related to events
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_tribe_events_%'");
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_tribe_events_%'");

echo "✅ Final cleanup completed\n\n";

// SUMMARY
echo "========================================\n";
echo "🎉 CLEANUP SUMMARY\n";
echo "========================================\n";
echo "Users removed: {$deleted_users}\n";
echo "Events removed: {$deleted_events}\n";
echo "Attendees removed: {$deleted_attendees}\n";
echo "Tickets removed: {$deleted_tickets}\n";
echo "Venues removed: {$deleted_venues}\n";
echo "Organizers removed: {$deleted_organizers}\n";
echo "Certificates removed: {$deleted_certificates}\n";
echo "Certificate files removed: {$deleted_files}\n";
echo "========================================\n";
echo "✅ Production cleanup completed successfully!\n";
echo "Staging is now clean and ready for production deployment.\n";
?>
PHPEOF

# Execute the cleanup script on the server
echo "🚀 Executing force cleanup script on staging server..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no $UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP "cd $UPSKILL_STAGING_PATH && php ../tmp/force-cleanup-test-data.php && rm ../tmp/force-cleanup-test-data.php"

echo ""
echo "========================================="
echo "✅ FORCE PRODUCTION CLEANUP COMPLETED!"
echo "========================================="
echo ""
echo "The staging environment has been cleaned of all test data."
echo "🚀 Staging is now ready for production deployment!"