#!/bin/bash

# Load environment variables
if [ -f "./.env" ]; then
    source ./.env
else
    echo "Error: .env file not found!"
    exit 1
fi

# Configuration
SSH_USER="${UPSKILL_STAGING_SSH_USER}"
SSH_HOST="${UPSKILL_STAGING_IP}"
SSH_PASS="${UPSKILL_STAGING_PASS}"
SITE_PATH="${UPSKILL_STAGING_PATH:-/home/974670.cloudwaysapps.com/uberrxmprk/public_html}"

# Check if required variables are set
if [ -z "$SSH_USER" ] || [ -z "$SSH_HOST" ] || [ -z "$SSH_PASS" ]; then
    echo "Error: Required environment variables not set. Please check your .env file."
    exit 1
fi

# Create a test event for the test_trainer user
echo "=== Creating test event for test_trainer ==="
sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SSH_HOST" "cd $SITE_PATH && wp post create --post_type=tribe_events --post_title='HVAC Training Workshop' --post_content='This is a test event for HVAC training.' --post_status=publish --post_author=18 --meta_input='{\"_EventStartDate\":\"$(date -v+1d "+%Y-%m-%d 10:00:00")\",\"_EventEndDate\":\"$(date -v+1d "+%Y-%m-%d 16:00:00")\",\"_EventVenueID\":\"auto\",\"_EventURL\":\"https://upskill-staging.measurequick.com\",\"_EventCurrencySymbol\":\"$\",\"_EventCurrencyPosition\":\"prefix\",\"_EventCost\":\"99.99\",\"_EventTimezone\":\"America/New_York\"}'"

# Create another event with a different date
echo "=== Creating second test event for test_trainer ==="
sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SSH_HOST" "cd $SITE_PATH && wp post create --post_type=tribe_events --post_title='Advanced HVAC Certification' --post_content='An advanced certification course for HVAC professionals.' --post_status=publish --post_author=18 --meta_input='{\"_EventStartDate\":\"$(date -v+7d "+%Y-%m-%d 09:00:00")\",\"_EventEndDate\":\"$(date -v+8d "+%Y-%m-%d 17:00:00")\",\"_EventVenueID\":\"auto\",\"_EventURL\":\"https://upskill-staging.measurequick.com\",\"_EventCurrencySymbol\":\"$\",\"_EventCurrencyPosition\":\"prefix\",\"_EventCost\":\"299.99\",\"_EventTimezone\":\"America/New_York\"}'"

# Create an event for admin_trainer
echo "=== Creating test event for admin_trainer ==="
sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SSH_HOST" "cd $SITE_PATH && wp post create --post_type=tribe_events --post_title='HVAC Business Management' --post_content='Learn how to manage your HVAC business effectively.' --post_status=publish --post_author=19 --meta_input='{\"_EventStartDate\":\"$(date -v+14d "+%Y-%m-%d 13:00:00")\",\"_EventEndDate\":\"$(date -v+14d "+%Y-%m-%d 17:00:00")\",\"_EventVenueID\":\"auto\",\"_EventURL\":\"https://upskill-staging.measurequick.com\",\"_EventCurrencySymbol\":\"$\",\"_EventCurrencyPosition\":\"prefix\",\"_EventCost\":\"149.99\",\"_EventTimezone\":\"America/New_York\"}'"

# Create test attendees
echo "=== Creating test attendees for the first event ==="
# We would normally use The Events Calendar's ticket system for this
# For now, we'll create a custom script to simulate attendees

cat > /tmp/create_attendees.php << 'EOL'
<?php
// Get the WordPress environment
require_once('wp-load.php');

// Find the first event for test_trainer (ID 18)
$events = get_posts(array(
    'post_type' => 'tribe_events',
    'author' => 18,
    'posts_per_page' => 1,
    'orderby' => 'date',
    'order' => 'DESC'
));

if (empty($events)) {
    echo "No events found for test_trainer\n";
    exit(1);
}

$event_id = $events[0]->ID;
echo "Creating attendees for event: " . $events[0]->post_title . " (ID: $event_id)\n";

// Test attendee data
$attendees = array(
    array(
        'name' => 'John Smith',
        'email' => 'john.smith@example.com',
        'phone' => '555-123-4567',
        'paid' => 99.99,
        'status' => 'checked-in'
    ),
    array(
        'name' => 'Jane Doe',
        'email' => 'jane.doe@example.com',
        'phone' => '555-987-6543',
        'paid' => 99.99,
        'status' => 'checked-in'
    ),
    array(
        'name' => 'Bob Johnson',
        'email' => 'bob.johnson@example.com',
        'phone' => '555-456-7890',
        'paid' => 99.99,
        'status' => 'not-checked-in'
    )
);

// Create attendees (this is a simplified example - actual implementation depends on The Events Calendar)
foreach ($attendees as $attendee_data) {
    // Check if we need to integrate with a specific ticket system
    // For now, we'll just create custom post meta to simulate attendees
    
    // Create a unique ID for this attendee
    $attendee_id = 'test_' . md5($attendee_data['email'] . time());
    
    // Add attendee to event (simplified approach)
    add_post_meta($event_id, '_tribe_attendee_' . $attendee_id, array(
        'name' => $attendee_data['name'],
        'email' => $attendee_data['email'],
        'phone' => $attendee_data['phone'],
        'paid' => $attendee_data['paid'],
        'status' => $attendee_data['status'],
        'created' => current_time('mysql')
    ));
    
    echo "Created attendee: " . $attendee_data['name'] . "\n";
}

// Update attendee count in event meta
$attendee_count = count($attendees);
update_post_meta($event_id, '_tribe_attendee_count', $attendee_count);
echo "Updated attendee count to $attendee_count\n";

echo "Done creating test attendees\n";
EOL

# Upload and run the PHP script
sshpass -p "$SSH_PASS" scp -o StrictHostKeyChecking=no /tmp/create_attendees.php "$SSH_USER@$SSH_HOST:$SITE_PATH/create_attendees.php"
sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SSH_HOST" "cd $SITE_PATH && php create_attendees.php"
sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$SSH_HOST" "cd $SITE_PATH && rm create_attendees.php"

echo "=== Test events and attendees created successfully ==="
echo "You can view these events on the HVAC dashboard or in the WordPress admin"