#!/bin/bash

# Exit on error
set -e

# Source environment variables
if [ -f ".env" ]; then
    source .env
else
    echo "Error: .env file not found. Please create it with the required variables."
    exit 1
fi

echo "===== Adding Ticket Sales Data ====="

# Create script to add ticket sales data to existing events
ADD_SALES_SCRIPT="<?php
require_once('wp-load.php');

echo \"Adding ticket sales data to existing events...\n\";

\$user = get_user_by('login', 'test_trainer');
if (!\$user) {
    echo \"test_trainer user not found\n\";
    exit(1);
}

// Get all events by test_trainer
\$events = get_posts(array(
    'post_type' => 'tribe_events',
    'author' => \$user->ID,
    'posts_per_page' => -1,
    'post_status' => array('publish', 'future', 'draft', 'pending', 'private')
));

echo \"Found \" . count(\$events) . \" events for test_trainer\n\";

\$total_added_tickets = 0;
\$total_added_revenue = 0;

foreach (\$events as \$event) {
    // Check if event already has ticket sales data
    \$existing_sold = get_post_meta(\$event->ID, '_tribe_tickets_sold', true);
    \$existing_revenue = get_post_meta(\$event->ID, '_tribe_revenue_total', true);
    
    if (!is_numeric(\$existing_sold) || \$existing_sold == 0) {
        // Add random but realistic ticket sales data
        \$sold = rand(5, 25);
        \$price = rand(75, 200);
        \$revenue = \$sold * \$price;
        
        update_post_meta(\$event->ID, '_tribe_tickets_sold', \$sold);
        update_post_meta(\$event->ID, '_tribe_revenue_total', \$revenue);
        
        \$total_added_tickets += \$sold;
        \$total_added_revenue += \$revenue;
        
        echo \"Event {\$event->ID} ({\$event->post_title}): Added \$sold tickets, $\$revenue revenue\n\";
    } else {
        echo \"Event {\$event->ID} already has sales data: \$existing_sold tickets, $\$existing_revenue revenue\n\";
    }
}

echo \"\\nTotal tickets added: \$total_added_tickets\n\";
echo \"Total revenue added: $\$total_added_revenue\n\";

// Test dashboard again
\$dashboard_data = new HVAC_Dashboard_Data(\$user->ID);

echo \"\\nUpdated Dashboard Results:\n\";
echo \"Total Events: \" . \$dashboard_data->get_total_events_count() . \"\n\";
echo \"Upcoming Events: \" . \$dashboard_data->get_upcoming_events_count() . \"\n\";
echo \"Past Events: \" . \$dashboard_data->get_past_events_count() . \"\n\";
echo \"Total Tickets: \" . \$dashboard_data->get_total_tickets_sold() . \"\n\";
echo \"Total Revenue: $\" . \$dashboard_data->get_total_revenue() . \"\n\";

echo \"\\nTicket sales data addition complete!\n\";
"

# Execute the script
echo "Executing ticket sales data script..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && cat > add-sales-data.php << 'EOF'
$ADD_SALES_SCRIPT
EOF"

sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && php add-sales-data.php"

# Clean up
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" "cd $UPSKILL_STAGING_PATH && rm add-sales-data.php"

echo -e "\n===== Ticket Sales Data Addition Complete ====="
echo "The dashboard should now show tickets sold and revenue data."
echo "Please refresh the dashboard page to see the updated numbers."