#!/bin/bash

# Create tickets and attendees for test events on staging server

# Variables
SSH_USER="roodev"
SSH_HOST="146.190.76.204"

echo "=== Creating Test Tickets on Staging Server ==="
echo "Remote host: $SSH_HOST"
echo "Remote user: $SSH_USER"
echo "==============================="

# Create PHP script to run on server
cat << 'EOF' > create-tickets.php
<?php
// Load WordPress
require_once dirname(dirname(__FILE__)) . '/public_html/wp-load.php';

// Ensure Event Tickets plugin is active
if (!class_exists('Tribe__Tickets__Main')) {
    die("Event Tickets plugin is not active");
}

// Event data with IDs from our created events
$events = [
    5482 => [ // HVAC System Maintenance Workshop
        'price' => 200,
        'attendees' => 5,
        'capacity' => 50
    ],
    5483 => [ // Advanced Diagnostics Training
        'price' => 350,
        'attendees' => 8,
        'capacity' => 30
    ],
    5484 => [ // Energy Efficiency Certification
        'price' => 500,
        'attendees' => 12,
        'capacity' => 40
    ],
    5485 => [ // Refrigeration Masterclass
        'price' => 300,
        'attendees' => 15,
        'capacity' => 50
    ],
    5486 => [ // HVAC Business Development Summit
        'price' => 1000,
        'attendees' => 7,
        'capacity' => 100
    ]
];

foreach ($events as $event_id => $event_data) {
    // Check if event exists
    $event = get_post($event_id);
    if (!$event) {
        echo "Event $event_id not found, skipping\n";
        continue;
    }
    
    echo "Processing event: {$event->post_title} (ID: $event_id)\n";
    
    // Create ticket for this event
    $ticket_args = [
        'post_type' => 'tribe_tpp_tickets',
        'post_status' => 'publish',
        'post_title' => "{$event->post_title} - General Admission",
        'meta_input' => [
            '_tribe_tpp_enabled' => 'yes',
            '_price' => $event_data['price'],
            '_stock' => $event_data['capacity'],
            '_capacity' => $event_data['capacity'],
            '_manage_stock' => 'yes',
            '_ticket_start_date' => '2025-01-01 00:00:00',
            '_ticket_end_date' => '2025-12-31 23:59:59',
        ]
    ];
    
    $ticket_id = wp_insert_post($ticket_args);
    
    if (is_wp_error($ticket_id)) {
        echo "Failed to create ticket for event $event_id: " . $ticket_id->get_error_message() . "\n";
        continue;
    }
    
    // Associate ticket with event
    update_post_meta($ticket_id, '_tribe_tpp_for_event', $event_id);
    update_post_meta($event_id, '_tribe_default_ticket_provider', 'Tribe__Tickets_Plus__Commerce__PayPal__Main');
    
    echo "Created ticket ID: $ticket_id\n";
    
    // Create attendees
    for ($i = 1; $i <= $event_data['attendees']; $i++) {
        $attendee_args = [
            'post_type' => 'tribe_tpp_attendees',
            'post_status' => 'publish',
            'post_title' => "Attendee $i for {$event->post_title}",
            'meta_input' => [
                '_tribe_tpp_event' => $event_id,
                '_tribe_tpp_product' => $ticket_id,
                '_tribe_tpp_ticket' => $ticket_id,
                '_tribe_tpp_price_paid' => $event_data['price'],
                '_tribe_tpp_order_status' => 'completed',
                '_tribe_tpp_security_code' => wp_generate_password(10, false),
                '_tribe_tpp_attendee_user_id' => 0,
                '_tribe_tpp_attendee_email' => "attendee{$i}_{$event_id}@test.com",
                '_tribe_tpp_attendee_full_name' => "Test Attendee $i",
                '_tribe_tpp_checked_in' => '',
                '_tribe_deleted' => 0,
            ]
        ];
        
        $attendee_id = wp_insert_post($attendee_args);
        
        if (is_wp_error($attendee_id)) {
            echo "Failed to create attendee for event $event_id: " . $attendee_id->get_error_message() . "\n";
        } else {
            echo "Created attendee ID: $attendee_id\n";
            
            // Update event attendance
            $current_count = get_post_meta($event_id, '_tribe_ticket_sold_count', true);
            update_post_meta($event_id, '_tribe_ticket_sold_count', intval($current_count) + 1);
        }
    }
    
    // Update ticket stock
    $remaining_stock = $event_data['capacity'] - $event_data['attendees'];
    update_post_meta($ticket_id, '_stock', $remaining_stock);
    update_post_meta($ticket_id, '_total_sales', $event_data['attendees']);
    
    echo "Completed processing event $event_id\n\n";
}

echo "All tickets and attendees created successfully\n";
EOF

# Copy PHP script to server and execute
sshpass -p "$SSH_PASS" scp create-tickets.php $SSH_USER@$SSH_HOST:$WP_ROOT/
sshpass -p "$SSH_PASS" ssh $SSH_USER@$SSH_HOST "cd $WP_ROOT && /usr/bin/php create-tickets.php"

# Clean up
rm create-tickets.php
sshpass -p "$SSH_PASS" ssh $SSH_USER@$SSH_HOST "rm $WP_ROOT/create-tickets.php"

echo -e "\033[0;32mTicket creation completed!\033[0m"