#!/bin/bash

# Setup test data for trainer journey tests
# This script creates test events, tickets, and attendees

# Configuration
STAGING_URL="https://wordpress-974670-5399585.cloudwaysapps.com"
MYSQL_DB="uqlfiqglqg"
MYSQL_USER="uqlfiqglqg"
MYSQL_PASS=""
MYSQL_HOST="mysql-db6de9adb-wordpress-974670-5399585.cloudwaysapps.com"

echo "Setting up test data for trainer journey..."

# Function to execute MySQL commands
mysql_exec() {
    mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" -p"$MYSQL_PASS" "$MYSQL_DB" -e "$1"
}

# Get the test trainer user ID
TRAINER_USER_ID=$(mysql_exec "SELECT ID FROM wp_users WHERE user_login = 'test_trainer' LIMIT 1;" | tail -n 1)

if [ -z "$TRAINER_USER_ID" ]; then
    echo "Error: test_trainer user not found"
    exit 1
fi

echo "Found test_trainer user ID: $TRAINER_USER_ID"

# Create 5 test events
EVENTS=(
    "HVAC System Maintenance Workshop|Learn essential maintenance techniques|200|5|2025-02-01 09:00:00|2025-02-01 17:00:00"
    "Advanced HVAC Diagnostics Training|Master diagnostic tools and techniques|500|12|2025-02-15 08:30:00|2025-02-15 18:30:00"
    "HVAC Installation Best Practices|Professional installation methods|100|2|2025-03-01 10:00:00|2025-03-01 16:00:00"
    "Commercial HVAC Systems Overview|Understanding large-scale systems|750|8|2025-03-15 09:00:00|2025-03-15 18:00:00"
    "HVAC Energy Efficiency Certification|Green HVAC technologies|1000|20|2025-04-01 08:00:00|2025-04-01 17:00:00"
)

# Current timestamp
NOW=$(date '+%Y-%m-%d %H:%M:%S')

# Array to store event IDs
EVENT_IDS=()

# Create events
for event_data in "${EVENTS[@]}"; do
    IFS='|' read -r title description price attendees start_date end_date <<< "$event_data"
    
    echo "Creating event: $title"
    
    # Insert event post
    mysql_exec "INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_status, post_type, post_name, post_modified, post_modified_gmt) 
                VALUES ($TRAINER_USER_ID, '$NOW', '$NOW', '$description', '$title', 'publish', 'tribe_events', LOWER(REPLACE('$title', ' ', '-')), '$NOW', '$NOW');"
    
    EVENT_ID=$(mysql_exec "SELECT LAST_INSERT_ID();" | tail -n 1)
    EVENT_IDS+=($EVENT_ID)
    
    echo "Created event ID: $EVENT_ID"
    
    # Add event meta data
    mysql_exec "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES 
                ($EVENT_ID, '_EventStartDate', '$start_date'),
                ($EVENT_ID, '_EventEndDate', '$end_date'),
                ($EVENT_ID, '_EventStartDateUTC', '$start_date'),
                ($EVENT_ID, '_EventEndDateUTC', '$end_date'),
                ($EVENT_ID, '_EventDuration', TIMESTAMPDIFF(SECOND, '$start_date', '$end_date')),
                ($EVENT_ID, '_EventCost', '$price'),
                ($EVENT_ID, '_EventURL', ''),
                ($EVENT_ID, '_EventTimezone', 'America/New_York'),
                ($EVENT_ID, '_tribe_events_status', 'publish'),
                ($EVENT_ID, '_EventShowMap', '1'),
                ($EVENT_ID, '_EventShowMapLink', '1');"
    
    # Create test venue
    mysql_exec "INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_status, post_type, post_name) 
                VALUES ($TRAINER_USER_ID, '$NOW', '$NOW', '', 'HVAC Training Center $EVENT_ID', 'publish', 'tribe_venue', 'hvac-training-center-$EVENT_ID');"
    
    VENUE_ID=$(mysql_exec "SELECT LAST_INSERT_ID();" | tail -n 1)
    
    mysql_exec "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES 
                ($VENUE_ID, '_VenueAddress', '123 Training St'),
                ($VENUE_ID, '_VenueCity', 'Training City'),
                ($VENUE_ID, '_VenueState', 'NY'),
                ($VENUE_ID, '_VenueZip', '12345'),
                ($VENUE_ID, '_VenueCountry', 'United States'),
                ($EVENT_ID, '_EventVenueID', '$VENUE_ID');"
    
    # Create test organizer
    mysql_exec "INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_status, post_type, post_name) 
                VALUES ($TRAINER_USER_ID, '$NOW', '$NOW', '', 'Test Trainer Organization', 'publish', 'tribe_organizer', 'test-trainer-org-$EVENT_ID');"
    
    ORGANIZER_ID=$(mysql_exec "SELECT LAST_INSERT_ID();" | tail -n 1)
    
    mysql_exec "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES 
                ($ORGANIZER_ID, '_OrganizerEmail', 'trainer@test.com'),
                ($ORGANIZER_ID, '_OrganizerPhone', '555-1234'),
                ($EVENT_ID, '_EventOrganizerID', '$ORGANIZER_ID');"
    
    # Create tickets for this event (if Event Tickets is installed)
    # Note: This part depends on Event Tickets Plus plugin structure
    
    # Create attendees with fabricated information
    for ((i=1; i<=attendees; i++)); do
        FIRST_NAME="Attendee${i}"
        LAST_NAME="Event${EVENT_ID}"
        EMAIL="attendee${i}_event${EVENT_ID}@test.com"
        
        # Create user account for attendee
        mysql_exec "INSERT INTO wp_users (user_login, user_pass, user_email, user_registered, display_name, user_nicename) 
                    VALUES ('attendee_${EVENT_ID}_${i}', MD5('password123'), '$EMAIL', '$NOW', '$FIRST_NAME $LAST_NAME', 'attendee-${EVENT_ID}-${i}');"
        
        ATTENDEE_ID=$(mysql_exec "SELECT LAST_INSERT_ID();" | tail -n 1)
        
        # Add user meta
        mysql_exec "INSERT INTO wp_usermeta (user_id, meta_key, meta_value) VALUES 
                    ($ATTENDEE_ID, 'first_name', '$FIRST_NAME'),
                    ($ATTENDEE_ID, 'last_name', '$LAST_NAME'),
                    ($ATTENDEE_ID, 'wp_capabilities', 'a:1:{s:10:\"subscriber\";b:1;}'),
                    ($ATTENDEE_ID, 'wp_user_level', '0');"
        
        # Create ticket/order record (simplified - actual implementation depends on Event Tickets structure)
        mysql_exec "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES 
                    ($EVENT_ID, '_tribe_tickets_attendee_${i}', '$ATTENDEE_ID'),
                    ($EVENT_ID, '_tribe_tickets_sold', '$i');"
    done
    
    echo "Created $attendees attendees for event: $title"
done

echo "Test data setup complete!"
echo "Created ${#EVENT_IDS[@]} events with attendees"

# Display summary
echo -e "\nEvent Summary:"
for i in "${!EVENTS[@]}"; do
    IFS='|' read -r title description price attendees start_date end_date <<< "${EVENTS[$i]}"
    echo "- $title (ID: ${EVENT_IDS[$i]}): $attendees attendees @ \$$price each"
done