#!/bin/bash

# Create basic test attendees for certificate testing using a series of WP-CLI commands

echo "=== Creating Test Attendees for Certificate Testing ==="
echo "Remote host: 146.190.76.204"
echo "Remote user: roodev"
echo "==============================="

# Event IDs to work with
EVENT_IDS=("5484" "5485" "5486")
EVENT_NAMES=("HVAC Installation Best Practices" "Commercial HVAC Systems Overview" "HVAC Energy Efficiency Certification")
PRICES=("150" "250" "350")
ATTENDEE_COUNTS=("12" "15" "20")
CHECKIN_COUNTS=("8" "10" "15")

# Execute each command on the server
for i in "${!EVENT_IDS[@]}"; do
    EVENT_ID=${EVENT_IDS[$i]}
    EVENT_NAME=${EVENT_NAMES[$i]}
    PRICE=${PRICES[$i]}
    ATTENDEE_COUNT=${ATTENDEE_COUNTS[$i]}
    CHECKIN_COUNT=${CHECKIN_COUNTS[$i]}
    
    echo "[1;33mProcessing event: $EVENT_NAME (ID: $EVENT_ID)[0m"
    
    # Create ticket for this event
    echo "Creating ticket for event $EVENT_ID..."
    TICKET_ID=$(ssh roodev@146.190.76.204 "cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html && wp post create --post_type=tribe_tpp_tickets --post_title=\"General Admission - $EVENT_NAME\" --post_status=publish --porcelain")
    
    if [ -z "$TICKET_ID" ]; then
        echo "Failed to create ticket for event $EVENT_ID"
        continue
    fi
    
    echo "Created ticket with ID: $TICKET_ID"
    
    # Add ticket meta
    ssh roodev@146.190.76.204 "cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html && wp post meta add $TICKET_ID _tribe_tpp_for_event $EVENT_ID && wp post meta add $TICKET_ID _tribe_tpp_enabled yes && wp post meta add $TICKET_ID _price $PRICE && wp post meta add $TICKET_ID _capacity $((ATTENDEE_COUNT+5)) && wp post meta add $TICKET_ID _stock $((ATTENDEE_COUNT+5))"
    
    # Associate ticket with event
    ssh roodev@146.190.76.204 "cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html && wp post meta add $EVENT_ID _tribe_default_ticket_provider Tribe__Tickets_Plus__Commerce__PayPal__Main"
    
    # Create attendees
    echo "Creating $ATTENDEE_COUNT attendees for event $EVENT_ID..."
    for (( j=1; j<=ATTENDEE_COUNT; j++ )); do
        NAME=$([ "$j" -eq 1 ] && echo "Ben Tester" || echo "Attendee$j Event$EVENT_ID")
        EMAIL=$([ "$j" -eq 1 ] && echo "ben@tealmaker.com" || echo "attendee${j}_event${EVENT_ID}@example.com")
        ORDER_ID="ORDER-${EVENT_ID}-${j}-$(date +%s)"
        
        # Create attendee
        ATTENDEE_ID=$(ssh roodev@146.190.76.204 "cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html && wp post create --post_type=tribe_tpp_attendees --post_title=\"$NAME\" --post_status=publish --porcelain")
        
        if [ -z "$ATTENDEE_ID" ]; then
            echo "Failed to create attendee $j for event $EVENT_ID"
            continue
        fi
        
        # Add attendee meta
        ssh roodev@146.190.76.204 "cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html && wp post meta add $ATTENDEE_ID _tribe_tickets_full_name \"$NAME\" && wp post meta add $ATTENDEE_ID _tribe_tickets_email \"$EMAIL\" && wp post meta add $ATTENDEE_ID _tribe_tpp_full_name \"$NAME\" && wp post meta add $ATTENDEE_ID _tribe_tpp_email \"$EMAIL\" && wp post meta add $ATTENDEE_ID _tribe_tpp_event $EVENT_ID && wp post meta add $ATTENDEE_ID _tribe_tpp_product $TICKET_ID && wp post meta add $ATTENDEE_ID _tribe_tpp_order \"$ORDER_ID\" && wp post meta add $ATTENDEE_ID _tribe_tickets_order_status complete"
        
        # Check in some attendees
        if [ "$j" -le "$CHECKIN_COUNT" ]; then
            ssh roodev@146.190.76.204 "cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html && wp post meta add $ATTENDEE_ID _tribe_tpp_checkin 1 && wp post meta add $ATTENDEE_ID _tribe_tpp_checked_in 1 && wp post meta add $ATTENDEE_ID check_in 1"
            echo "Checked in attendee $ATTENDEE_ID"
        fi
        
        echo "Created attendee $j with ID: $ATTENDEE_ID"
    done
    
    # Update ticket and event counts with both meta fields for compatibility
    ssh roodev@146.190.76.204 "cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html && wp post meta add $TICKET_ID _tribe_tpp_sold $ATTENDEE_COUNT && wp post meta update $TICKET_ID _stock $(($(($ATTENDEE_COUNT+5))-$ATTENDEE_COUNT)) && wp post meta add $EVENT_ID _tribe_ticket_sold_count $ATTENDEE_COUNT && wp post meta add $EVENT_ID _tribe_tickets_sold $ATTENDEE_COUNT && wp post meta add $EVENT_ID _tribe_revenue_total $(($ATTENDEE_COUNT * $PRICE))"
    
    echo "Completed processing event $EVENT_ID"
    echo "----------------------------"
done

echo "[0;32mTest data creation completed![0m"
echo "1. Added tickets and attendees to existing events"
echo "2. Some attendees are marked as checked-in"
echo "3. One attendee for each event has email: ben@tealmaker.com"
echo "4. Checked-in attendees are ready for certificate generation"