#!/bin/bash

# Simple Test Attendees Creation Script
# Creates attendees specifically for event ID 6042 to test certificate generation

source .env

echo "=== Creating Test Attendees for Event 6042 ==="
echo "Target: $UPSKILL_STAGING_IP"
echo "============================================="

# Upload and execute PHP script to create attendees for event 6042
sshpass -p "$UPSKILL_STAGING_PASS" scp -o StrictHostKeyChecking=no /dev/stdin $UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP:tmp/create-attendees-6042.php << 'PHPEOF'
<?php
require_once('wp-load.php');

echo "=== Creating Test Attendees for Event 6042 ===\n";

// Target event ID from the dashboard
$event_id = 6042;

// Verify event exists
$event = get_post($event_id);
if (!$event || $event->post_type !== 'tribe_events') {
    die("Event 6042 not found or not a tribe_events post\n");
}

echo "Event found: {$event->post_title} (ID: {$event_id}, Author: {$event->post_author})\n";

// Create 5 test attendees for this event
echo "Creating 5 attendees...\n";

for ($i = 1; $i <= 5; $i++) {
    $attendee_args = [
        'post_title' => "Test Attendee {$i} for Event {$event_id}",
        'post_content' => '',
        'post_status' => 'publish',
        'post_type' => 'tribe_tpp_attendees',
        'post_author' => $event->post_author,  // Same as event author
        'post_parent' => $event_id  // CRITICAL: Link to event
    ];
    
    $attendee_id = wp_insert_post($attendee_args);
    
    if (!is_wp_error($attendee_id)) {
        echo "Created attendee ID: {$attendee_id}\n";
        
        // Add all required meta fields for Event Tickets Plus compatibility
        update_post_meta($attendee_id, '_tribe_tpp_event', $event_id);
        update_post_meta($attendee_id, '_tribe_tpp_full_name', "Test Attendee {$i}");
        update_post_meta($attendee_id, '_tribe_tickets_full_name', "Test Attendee {$i}");
        update_post_meta($attendee_id, '_tribe_tpp_email', "test.attendee.{$i}.{$event_id}@example.com");
        update_post_meta($attendee_id, '_tribe_tickets_email', "test.attendee.{$i}.{$event_id}@example.com");
        update_post_meta($attendee_id, '_tribe_tpp_attendee_email', "test.attendee.{$i}.{$event_id}@example.com");
        update_post_meta($attendee_id, '_tribe_tpp_order', "ORDER-{$event_id}-{$i}");
        update_post_meta($attendee_id, '_tribe_tpp_product', 0); // No specific ticket product
        
        // Check in first 3 attendees, leave 2 unchecked (as per user requirement)
        if ($i <= 3) {
            update_post_meta($attendee_id, '_tribe_tickets_attendee_checked_in', '1');
            update_post_meta($attendee_id, '_tribe_tpp_checkin', '1');
            update_post_meta($attendee_id, 'check_in', '1');
            echo "  ✓ Checked in attendee {$i}\n";
        } else {
            update_post_meta($attendee_id, '_tribe_tickets_attendee_checked_in', '0');
            update_post_meta($attendee_id, '_tribe_tpp_checkin', '0');
            update_post_meta($attendee_id, 'check_in', '0');
            echo "  - Left attendee {$i} not checked in\n";
        }
    } else {
        echo "Failed to create attendee {$i}: " . $attendee_id->get_error_message() . "\n";
    }
}

echo "\n=== Verification Query (Same as Certificate Template) ===\n";

// Run the exact same query that the certificate template uses
global $wpdb;

$attendees = $wpdb->get_results($wpdb->prepare(
    "SELECT 
        p.ID as attendee_id,
        p.post_parent as event_id,
        COALESCE(tec_full_name.meta_value, tpp_full_name.meta_value, tickets_full_name.meta_value, 'Unknown Attendee') as holder_name,
        COALESCE(tec_email.meta_value, tpp_email.meta_value, tickets_email.meta_value, tpp_attendee_email.meta_value, 'no-email@example.com') as holder_email,
        COALESCE(checked_in.meta_value, '0') as check_in
    FROM {$wpdb->posts} p
    LEFT JOIN {$wpdb->postmeta} tec_full_name ON p.ID = tec_full_name.post_id AND tec_full_name.meta_key = '_tec_tickets_commerce_full_name'
    LEFT JOIN {$wpdb->postmeta} tpp_full_name ON p.ID = tpp_full_name.post_id AND tpp_full_name.meta_key = '_tribe_tpp_full_name'
    LEFT JOIN {$wpdb->postmeta} tickets_full_name ON p.ID = tickets_full_name.post_id AND tickets_full_name.meta_key = '_tribe_tickets_full_name'
    LEFT JOIN {$wpdb->postmeta} tec_email ON p.ID = tec_email.post_id AND tec_email.meta_key = '_tec_tickets_commerce_email'
    LEFT JOIN {$wpdb->postmeta} tpp_email ON p.ID = tpp_email.post_id AND tpp_email.meta_key = '_tribe_tpp_email'
    LEFT JOIN {$wpdb->postmeta} tickets_email ON p.ID = tickets_email.post_id AND tickets_email.meta_key = '_tribe_tickets_email'
    LEFT JOIN {$wpdb->postmeta} tpp_attendee_email ON p.ID = tpp_attendee_email.post_id AND tpp_attendee_email.meta_key = '_tribe_tpp_attendee_email'
    LEFT JOIN {$wpdb->postmeta} checked_in ON p.ID = checked_in.post_id AND checked_in.meta_key = '_tribe_tickets_attendee_checked_in'
    WHERE p.post_type IN ('tec_tc_attendee', 'tribe_tpp_attendees') 
    AND p.post_parent = %d
    ORDER BY p.ID ASC",
    $event_id
));

echo "Found " . count($attendees) . " attendees for event {$event_id}:\n";
foreach ($attendees as $attendee) {
    $status = $attendee->check_in ? 'Checked In' : 'Not Checked In';
    echo "  - ID: {$attendee->attendee_id}, Name: {$attendee->holder_name}, Email: {$attendee->holder_email}, Status: {$status}\n";
}

if (count($attendees) > 0) {
    echo "\n✅ SUCCESS: Certificate generation should now work for event 6042!\n";
} else {
    echo "\n❌ FAILED: No attendees found - certificate generation will still show 'no attendees'\n";
}

echo "\nTest Instructions:\n";
echo "1. Go to: https://upskill-staging.measurequick.com/trainer/generate-certificates/?event_id=6042\n";
echo "2. Should see 5 attendees (3 checked in, 2 not checked in)\n";
echo "3. All trainers should be able to generate certificates regardless of check-in status\n";
?>
PHPEOF

# Execute the script on the server
echo "Executing attendee creation script..."
sshpass -p "$UPSKILL_STAGING_PASS" ssh -o StrictHostKeyChecking=no $UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP "cd $UPSKILL_STAGING_PATH && php ../tmp/create-attendees-6042.php && rm ../tmp/create-attendees-6042.php"

echo ""
echo "✅ Test attendee creation for event 6042 completed!"