#!/bin/bash

# Create Test Admin with Master Trainer Role and Seed Events
# This script creates a dedicated test admin user and seeds events for testing

echo "========================================="
echo "CREATING TEST ADMIN & SEEDING EVENTS"
echo "========================================="

# Configuration
STAGING_USER="roodev"
STAGING_IP="146.190.76.204"
STAGING_PASS="uSCO6f1y"
STAGING_PATH="/home/974670.cloudwaysapps.com/uberrxmprk/public_html"

# Test admin credentials
TEST_ADMIN_USER="test_admin"
TEST_ADMIN_EMAIL="test_admin@upskillhvac.com"
TEST_ADMIN_PASS="TestAdmin2025!"

echo ""
echo "📝 Step 1: Creating test admin user..."
echo "   Username: $TEST_ADMIN_USER"
echo "   Email: $TEST_ADMIN_EMAIL"
echo "   Password: $TEST_ADMIN_PASS"
echo ""

# Create user via SSH
SSHPASS=$STAGING_PASS sshpass -e ssh $STAGING_USER@$STAGING_IP << 'EOF'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

# Create test admin user
echo "Creating test_admin user..."
wp user create test_admin test_admin@upskillhvac.com \
  --user_pass=TestAdmin2025! \
  --role=administrator \
  --first_name=Test \
  --last_name=Admin \
  --display_name="Test Admin" \
  2>/dev/null || echo "User may already exist"

# Add HVAC roles
echo "Adding HVAC roles..."
wp user add-role test_admin hvac_trainer 2>/dev/null
wp user add-role test_admin hvac_master_trainer 2>/dev/null

# Verify user
echo "Verifying user..."
wp user get test_admin --fields=ID,user_login,user_email,roles

# Create events owned by test_admin
USER_ID=$(wp user get test_admin --field=ID)
echo "User ID: $USER_ID"

echo ""
echo "📝 Step 2: Creating test events..."

# Create Event 1
EVENT1_ID=$(wp post create \
  --post_type=tribe_events \
  --post_title="HVAC Installation Training - Test Event 1" \
  --post_content="Comprehensive HVAC installation training covering residential and commercial systems. Learn proper techniques for ductwork, electrical connections, and system commissioning." \
  --post_status=publish \
  --post_author=$USER_ID \
  --porcelain)

if [ ! -z "$EVENT1_ID" ]; then
  echo "Created Event 1 (ID: $EVENT1_ID)"
  
  # Add event meta
  wp post meta update $EVENT1_ID _EventStartDate "2025-09-15 09:00:00"
  wp post meta update $EVENT1_ID _EventEndDate "2025-09-15 17:00:00"
  wp post meta update $EVENT1_ID _EventStartDateUTC "2025-09-15 14:00:00"
  wp post meta update $EVENT1_ID _EventEndDateUTC "2025-09-15 22:00:00"
  wp post meta update $EVENT1_ID _EventCost "299"
  wp post meta update $EVENT1_ID _EventCurrencySymbol "$"
  wp post meta update $EVENT1_ID _EventURL "https://example.com/hvac-training"
  wp post meta update $EVENT1_ID _EventShowMap "1"
  wp post meta update $EVENT1_ID _EventShowMapLink "1"
  
  # Create and link venue
  VENUE1_ID=$(wp post create \
    --post_type=tribe_venue \
    --post_title="Dallas Training Center" \
    --post_status=publish \
    --post_author=$USER_ID \
    --porcelain)
  
  if [ ! -z "$VENUE1_ID" ]; then
    wp post meta update $VENUE1_ID _VenueVenue "Dallas Training Center"
    wp post meta update $VENUE1_ID _VenueAddress "123 Main Street"
    wp post meta update $VENUE1_ID _VenueCity "Dallas"
    wp post meta update $VENUE1_ID _VenueState "TX"
    wp post meta update $VENUE1_ID _VenueZip "75201"
    wp post meta update $VENUE1_ID _VenueCountry "United States"
    wp post meta update $VENUE1_ID _VenuePhone "214-555-0100"
    wp post meta update $EVENT1_ID _EventVenueID $VENUE1_ID
    echo "  - Linked venue: Dallas Training Center"
  fi
  
  # Create and link organizer
  ORG1_ID=$(wp post create \
    --post_type=tribe_organizer \
    --post_title="Test HVAC Training Solutions" \
    --post_status=publish \
    --post_author=$USER_ID \
    --porcelain)
  
  if [ ! -z "$ORG1_ID" ]; then
    wp post meta update $ORG1_ID _OrganizerOrganizer "Test HVAC Training Solutions"
    wp post meta update $ORG1_ID _OrganizerEmail "training@test.example.com"
    wp post meta update $ORG1_ID _OrganizerPhone "214-555-0200"
    wp post meta update $ORG1_ID _OrganizerWebsite "https://hvactraining.example.com"
    wp post meta update $EVENT1_ID _EventOrganizerID $ORG1_ID
    echo "  - Linked organizer: Test HVAC Training Solutions"
  fi
fi

# Create Event 2
EVENT2_ID=$(wp post create \
  --post_type=tribe_events \
  --post_title="EPA 608 Certification Prep - Test Event 2" \
  --post_content="Prepare for EPA 608 certification. Covers Types I, II, III, and Universal certification requirements including refrigerant handling and recovery procedures." \
  --post_status=publish \
  --post_author=$USER_ID \
  --porcelain)

if [ ! -z "$EVENT2_ID" ]; then
  echo "Created Event 2 (ID: $EVENT2_ID)"
  
  wp post meta update $EVENT2_ID _EventStartDate "2025-09-22 08:00:00"
  wp post meta update $EVENT2_ID _EventEndDate "2025-09-22 16:00:00"
  wp post meta update $EVENT2_ID _EventStartDateUTC "2025-09-22 13:00:00"
  wp post meta update $EVENT2_ID _EventEndDateUTC "2025-09-22 21:00:00"
  wp post meta update $EVENT2_ID _EventCost "399"
  wp post meta update $EVENT2_ID _EventCurrencySymbol "$"
  wp post meta update $EVENT2_ID _EventURL "https://example.com/epa-certification"
  wp post meta update $EVENT2_ID _EventVenueID $VENUE1_ID
  wp post meta update $EVENT2_ID _EventOrganizerID $ORG1_ID
fi

# Create Event 3
EVENT3_ID=$(wp post create \
  --post_type=tribe_events \
  --post_title="Commercial HVAC Maintenance - Test Event 3" \
  --post_content="Advanced maintenance techniques for commercial HVAC systems. Topics include troubleshooting, preventive maintenance, and energy efficiency." \
  --post_status=publish \
  --post_author=$USER_ID \
  --porcelain)

if [ ! -z "$EVENT3_ID" ]; then
  echo "Created Event 3 (ID: $EVENT3_ID)"
  
  wp post meta update $EVENT3_ID _EventStartDate "2025-10-01 09:00:00"
  wp post meta update $EVENT3_ID _EventEndDate "2025-10-02 17:00:00"
  wp post meta update $EVENT3_ID _EventStartDateUTC "2025-10-01 14:00:00"
  wp post meta update $EVENT3_ID _EventEndDateUTC "2025-10-02 22:00:00"
  wp post meta update $EVENT3_ID _EventCost "599"
  wp post meta update $EVENT3_ID _EventCurrencySymbol "$"
  wp post meta update $EVENT3_ID _EventURL "https://example.com/commercial-hvac"
  wp post meta update $EVENT3_ID _EventVenueID $VENUE1_ID
  wp post meta update $EVENT3_ID _EventOrganizerID $ORG1_ID
fi

echo ""
echo "📝 Step 3: Verifying created events..."
wp post list --post_type=tribe_events --author=$USER_ID --fields=ID,post_title,post_status --format=table

echo ""
echo "📝 Step 4: Flushing rewrite rules..."
wp rewrite flush

echo ""
echo "✅ SETUP COMPLETE!"
echo ""
echo "Test Admin Credentials:"
echo "  Username: test_admin"
echo "  Email: test_admin@upskillhvac.com"
echo "  Password: TestAdmin2025!"
echo "  Roles: administrator, hvac_trainer, hvac_master_trainer"
echo ""
echo "You can now login at:"
echo "  https://upskill-staging.measurequick.com/wp-login.php"
echo "  OR"
echo "  https://upskill-staging.measurequick.com/training-login"
EOF

echo ""
echo "========================================="
echo "✅ SCRIPT COMPLETE"
echo "========================================="
echo ""
echo "Test Admin Created:"
echo "  Username: test_admin"
echo "  Password: TestAdmin2025!"
echo ""
echo "Next Steps:"
echo "1. Login with test_admin credentials"
echo "2. Navigate to Events > All Events"
echo "3. Edit any event to verify field population"
echo "4. Make changes and verify they persist"
echo "========================================="