#!/bin/bash

# Script to test certificate email functionality on staging server

# Load configuration
source bin/deploy-config.sh

# Set email address to test with
TEST_EMAIL="ben@tealmaker.com"

echo "=== Testing Certificate Email Functionality ==="
echo "Remote host: $REMOTE_HOST"
echo "Remote user: $REMOTE_USER"
echo "WordPress path: $REMOTE_PATH_BASE"
echo "Test email: $TEST_EMAIL"
echo "==============================="

# Connect to the remote server and run a WP-CLI command to create a test event, attendee, and certificate
ssh $REMOTE_USER@$REMOTE_HOST "cd $REMOTE_PATH_BASE && \
  wp eval '
    // Create a test event if needed
    \$event_exists = get_posts(array(
      \"post_type\" => \"tribe_events\",
      \"meta_key\" => \"_test_certificate_event\",
      \"meta_value\" => \"yes\",
      \"posts_per_page\" => 1
    ));
    
    if (empty(\$event_exists)) {
      \$event_id = wp_insert_post(array(
        \"post_title\" => \"Test Certificate Event\",
        \"post_content\" => \"This is a test event for certificate generation\",
        \"post_status\" => \"publish\",
        \"post_type\" => \"tribe_events\"
      ));
      update_post_meta(\$event_id, \"_test_certificate_event\", \"yes\");
      echo \"Created test event with ID: {\$event_id}\\n\";
    } else {
      \$event_id = \$event_exists[0]->ID;
      echo \"Using existing test event with ID: {\$event_id}\\n\";
    }
    
    // Create a test attendee
    \$attendee_exists = get_posts(array(
      \"post_type\" => \"tribe_rsvp_attendees\",
      \"meta_key\" => \"_tribe_tickets_email\",
      \"meta_value\" => \"$TEST_EMAIL\",
      \"posts_per_page\" => 1
    ));
    
    if (empty(\$attendee_exists)) {
      \$attendee_id = wp_insert_post(array(
        \"post_title\" => \"Test Attendee\",
        \"post_status\" => \"publish\",
        \"post_type\" => \"tribe_rsvp_attendees\"
      ));
      
      update_post_meta(\$attendee_id, \"_tribe_tickets_email\", \"$TEST_EMAIL\");
      update_post_meta(\$attendee_id, \"_tribe_tickets_full_name\", \"Test Attendee\");
      update_post_meta(\$attendee_id, \"_tribe_rsvp_event\", \$event_id);
      update_post_meta(\$attendee_id, \"_tribe_rsvp_status\", \"yes\");
      update_post_meta(\$attendee_id, \"_tribe_tickets_attendee_status\", \"yes\");
      update_post_meta(\$attendee_id, \"check_in\", \"1\");
      
      echo \"Created test attendee with ID: {\$attendee_id}\\n\";
    } else {
      \$attendee_id = \$attendee_exists[0]->ID;
      echo \"Using existing attendee with ID: {\$attendee_id}\\n\";
    }
    
    // Load certificate manager
    require_once WP_PLUGIN_DIR . \"/hvac-community-events/includes/certificates/class-certificate-manager.php\";
    require_once WP_PLUGIN_DIR . \"/hvac-community-events/includes/certificates/class-certificate-generator.php\";
    
    \$certificate_manager = HVAC_Certificate_Manager::instance();
    \$certificate_generator = HVAC_Certificate_Generator::instance();
    
    // Generate certificate
    \$certificate_exists = \$certificate_manager->get_certificate_by_attendee(\$event_id, \$attendee_id);
    
    if (!\$certificate_exists) {
      \$result = \$certificate_generator->generate_certificate(\$event_id, \$attendee_id);
      if (\$result) {
        \$certificate = \$certificate_manager->get_certificate_by_attendee(\$event_id, \$attendee_id);
        echo \"Generated certificate with ID: {\$certificate->certificate_id}\\n\";
      } else {
        echo \"Failed to generate certificate\\n\";
      }
    } else {
      echo \"Using existing certificate with ID: {\$certificate_exists->certificate_id}\\n\";
    }
    
    // Get existing certificate
    \$certificate = \$certificate_manager->get_certificate_by_attendee(\$event_id, \$attendee_id);
    if (!\$certificate) {
      echo \"No certificate found\\n\";
      exit;
    }
    
    // Email certificate
    require_once WP_PLUGIN_DIR . \"/hvac-community-events/includes/certificates/class-certificate-security.php\";
    \$certificate_security = HVAC_Certificate_Security::instance();
    
    // Generate secure download URL
    \$event = get_post(\$certificate->event_id);
    \$attendee_name = get_post_meta(\$certificate->attendee_id, \"_tribe_tickets_full_name\", true);
    
    \$certificate_data = array(
      \"file_path\" => \$certificate->file_path,
      \"event_name\" => \$event->post_title,
      \"attendee_name\" => \$attendee_name
    );
    
    \$download_url = \$certificate_security->generate_download_token(\$certificate->certificate_id, \$certificate_data, 7 * DAY_IN_SECONDS);
    
    if (!\$download_url) {
      echo \"Failed to generate download URL\\n\";
      exit;
    }
    
    // Email subject
    \$subject = sprintf(
      \"Your Certificate for %s\",
      \$event->post_title
    );
    
    // Email body
    \$message = sprintf(
      \"Hello %s,\\n\\nThank you for attending %s.\\n\\nYour certificate of completion is now available. Please click the link below to download your certificate:\\n\\n%s\\n\\nThis link will expire in 7 days.\\n\\nRegards,\\nTest System\",
      \$attendee_name,
      \$event->post_title,
      \$download_url
    );
    
    // Send email
    \$headers = array(\"Content-Type: text/plain; charset=UTF-8\");
    \$sent = wp_mail(\"$TEST_EMAIL\", \$subject, \$message, \$headers);
    
    if (\$sent) {
      // Record email sent
      \$certificate_manager->mark_certificate_emailed(\$certificate->certificate_id);
      echo \"Certificate email sent successfully to $TEST_EMAIL\\n\";
    } else {
      echo \"Failed to send certificate email\\n\";
    }
  '
"

echo "Email verification complete. Please check $TEST_EMAIL for the certificate email."