<?php
/**
 * Setup test events with tickets and attendees
 * Run this script via WP-CLI: wp eval-file tests/setup-test-events.php
 */

// Exit if not running in WP-CLI
if (!defined('WP_CLI')) {
    echo "This script must be run via WP-CLI\n";
    exit(1);
}

// Get test trainer user
$trainer_user = get_user_by('login', 'test_trainer');
if (!$trainer_user) {
    echo "Error: test_trainer user not found\n";
    exit(1);
}

$trainer_id = $trainer_user->ID;
echo "Found test_trainer user ID: $trainer_id\n";

// Event data
$events = [
    [
        'title' => 'HVAC System Maintenance Workshop',
        'description' => 'Learn essential maintenance techniques for residential and commercial HVAC systems.',
        'price' => 200,
        'attendees' => 5,
        'start_date' => '2025-02-01 09:00:00',
        'end_date' => '2025-02-01 17:00:00',
    ],
    [
        'title' => 'Advanced HVAC Diagnostics Training',
        'description' => 'Master diagnostic tools and techniques for troubleshooting complex HVAC issues.',
        'price' => 500,
        'attendees' => 12,
        'start_date' => '2025-02-15 08:30:00',
        'end_date' => '2025-02-15 18:30:00',
    ],
    [
        'title' => 'HVAC Installation Best Practices',
        'description' => 'Professional installation methods and safety procedures for HVAC technicians.',
        'price' => 100,
        'attendees' => 2,
        'start_date' => '2025-03-01 10:00:00',
        'end_date' => '2025-03-01 16:00:00',
    ],
    [
        'title' => 'Commercial HVAC Systems Overview',
        'description' => 'Understanding large-scale commercial HVAC systems and their components.',
        'price' => 750,
        'attendees' => 8,
        'start_date' => '2025-03-15 09:00:00',
        'end_date' => '2025-03-15 18:00:00',
    ],
    [
        'title' => 'HVAC Energy Efficiency Certification',
        'description' => 'Green HVAC technologies and energy-saving strategies for modern systems.',
        'price' => 1000,
        'attendees' => 20,
        'start_date' => '2025-04-01 08:00:00',
        'end_date' => '2025-04-01 17:00:00',
    ],
];

// First names and last names for random generation
$first_names = ['John', 'Jane', 'Michael', 'Sarah', 'Robert', 'Emily', 'David', 'Jessica', 'James', 'Jennifer',
                'William', 'Linda', 'Richard', 'Barbara', 'Joseph', 'Susan', 'Thomas', 'Karen', 'Charles', 'Nancy'];
$last_names = ['Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Garcia', 'Miller', 'Davis', 'Rodriguez', 'Martinez',
               'Hernandez', 'Lopez', 'Gonzalez', 'Wilson', 'Anderson', 'Thomas', 'Taylor', 'Moore', 'Jackson', 'Martin'];

// Create events
foreach ($events as $index => $event_data) {
    echo "\nCreating event: {$event_data['title']}\n";
    
    // Create the event post
    $event_args = [
        'post_title' => $event_data['title'],
        'post_content' => $event_data['description'],
        'post_status' => 'publish',
        'post_type' => 'tribe_events',
        'post_author' => $trainer_id,
        'meta_input' => [
            '_EventStartDate' => $event_data['start_date'],
            '_EventEndDate' => $event_data['end_date'],
            '_EventStartDateUTC' => $event_data['start_date'],
            '_EventEndDateUTC' => $event_data['end_date'],
            '_EventCost' => $event_data['price'],
            '_EventCurrencySymbol' => '$',
            '_EventTimezone' => 'America/New_York',
            '_EventShowMap' => 1,
            '_EventShowMapLink' => 1,
        ],
    ];
    
    $event_id = wp_insert_post($event_args);
    
    if (is_wp_error($event_id)) {
        echo "Error creating event: " . $event_id->get_error_message() . "\n";
        continue;
    }
    
    echo "Created event ID: $event_id\n";
    
    // Create venue
    $venue_args = [
        'post_title' => 'HVAC Training Center ' . ($index + 1),
        'post_status' => 'publish',
        'post_type' => 'tribe_venue',
        'post_author' => $trainer_id,
        'meta_input' => [
            '_VenueAddress' => ($index + 1) . '23 Training Boulevard',
            '_VenueCity' => 'Training City',
            '_VenueState' => 'NY',
            '_VenueZip' => '12345',
            '_VenueCountry' => 'United States',
            '_VenuePhone' => '555-' . str_pad($index + 1, 4, '0', STR_PAD_LEFT),
        ],
    ];
    
    $venue_id = wp_insert_post($venue_args);
    update_post_meta($event_id, '_EventVenueID', $venue_id);
    
    // Create organizer
    $organizer_args = [
        'post_title' => 'HVAC Training Organization',
        'post_status' => 'publish',
        'post_type' => 'tribe_organizer',
        'post_author' => $trainer_id,
        'meta_input' => [
            '_OrganizerEmail' => 'trainer@hvactraining.com',
            '_OrganizerPhone' => '555-0000',
            '_OrganizerWebsite' => 'https://hvactraining.com',
        ],
    ];
    
    $organizer_id = wp_insert_post($organizer_args);
    update_post_meta($event_id, '_EventOrganizerID', $organizer_id);
    
    // Create tickets using Event Tickets
    if (class_exists('Tribe__Tickets__Main')) {
        $ticket_args = [
            'post_title' => 'General Admission',
            'post_content' => 'Standard ticket for ' . $event_data['title'],
            'post_status' => 'publish',
            'post_type' => 'tribe_tpp_tickets',
            'post_author' => $trainer_id,
            'post_parent' => $event_id,
            'meta_input' => [
                '_price' => $event_data['price'],
                '_stock' => 50,
                '_capacity' => 50,
                '_ticket_start_date' => date('Y-m-d H:i:s'),
                '_ticket_end_date' => $event_data['start_date'],
                '_tribe_tpp_for_event' => $event_id,
            ],
        ];
        
        $ticket_id = wp_insert_post($ticket_args);
        echo "Created ticket ID: $ticket_id\n";
        
        // Create attendees
        for ($i = 1; $i <= $event_data['attendees']; $i++) {
            $first_name = $first_names[array_rand($first_names)];
            $last_name = $last_names[array_rand($last_names)];
            $email = strtolower($first_name . '.' . $last_name . '.event' . $event_id . '@test.com');
            
            // Create attendee post
            $attendee_args = [
                'post_title' => $first_name . ' ' . $last_name,
                'post_status' => 'publish',
                'post_type' => 'tribe_tpp_attendees',
                'post_author' => $trainer_id,
                'meta_input' => [
                    '_tribe_tpp_event' => $event_id,
                    '_tribe_tpp_product' => $ticket_id,
                    '_tribe_tpp_full_name' => $first_name . ' ' . $last_name,
                    '_tribe_tpp_email' => $email,
                    '_tribe_tpp_attendee_user_id' => 0,
                    '_tribe_tpp_order_status' => 'completed',
                    '_tribe_tpp_security_code' => wp_generate_password(10, false),
                    '_paid_price' => $event_data['price'],
                ],
            ];
            
            $attendee_id = wp_insert_post($attendee_args);
            
            // Update ticket sales count
            $current_sales = get_post_meta($ticket_id, '_tribe_ticket_sales_count', true);
            update_post_meta($ticket_id, '_tribe_ticket_sales_count', $current_sales + 1);
            
            echo "Created attendee: $first_name $last_name (ID: $attendee_id)\n";
        }
    } else {
        echo "Event Tickets plugin not found - skipping ticket creation\n";
    }
    
    echo "Created {$event_data['attendees']} attendees for event: {$event_data['title']}\n";
}

echo "\nTest data setup complete!\n";
echo "Created " . count($events) . " events with tickets and attendees\n";

// Display summary
echo "\nEvent Summary:\n";
foreach ($events as $index => $event_data) {
    echo "- {$event_data['title']}: {$event_data['attendees']} attendees @ \${$event_data['price']} each\n";
}