#!/bin/bash

# Debug script to check attendee data for certificate generation

echo "=== Certificate Generation Attendee Debug ==="
echo "Event ID: 6042 (Energy Efficient HVAC)"
echo ""

# Check if we can connect to staging server
echo "Connecting to staging server..."

sshpass -p 'RbkJE?0GZc]_-U_x' ssh -o StrictHostKeyChecking=no upskill@server-974670.cloudwaysapps.com << 'EOF'

echo "=== 1. Checking Event Tickets/Attendees Tables ==="
# Check for different attendee post types
wp db query "SELECT 
    p.ID, 
    p.post_type, 
    p.post_parent, 
    p.post_status,
    p.post_title 
FROM wp_posts p 
WHERE p.post_parent = 6042 
AND p.post_type IN ('tec_tc_attendee', 'tribe_tpp_attendees', 'tribe_rsvp_tickets')
ORDER BY p.post_type, p.ID;" --path=/home/upskill/public_html

echo ""
echo "=== 2. Checking All Posts Related to Event 6042 ==="
wp db query "SELECT 
    p.ID, 
    p.post_type, 
    p.post_parent, 
    p.post_status,
    p.post_title 
FROM wp_posts p 
WHERE p.post_parent = 6042 
ORDER BY p.post_type, p.ID;" --path=/home/upskill/public_html

echo ""
echo "=== 3. Checking Event Details ==="
wp db query "SELECT 
    p.ID, 
    p.post_title,
    p.post_status,
    p.post_author
FROM wp_posts p 
WHERE p.ID = 6042;" --path=/home/upskill/public_html

echo ""
echo "=== 4. Checking Ticket/Product Posts ==="
wp db query "SELECT 
    p.ID, 
    p.post_type, 
    p.post_status,
    p.post_title,
    pm.meta_key,
    pm.meta_value
FROM wp_posts p 
LEFT JOIN wp_postmeta pm ON p.ID = pm.post_id 
WHERE (pm.meta_key = '_EventOrigin' AND pm.meta_value = '6042')
   OR (pm.meta_key = '_tribe_tpp_for_event' AND pm.meta_value = '6042')
   OR (pm.meta_key = '_ticket_event_id' AND pm.meta_value = '6042')
ORDER BY p.post_type, p.ID;" --path=/home/upskill/public_html

echo ""
echo "=== 5. Checking HVAC Attendees Table ==="
wp db query "SELECT * FROM wp_hvac_attendees WHERE event_id = 6042;" --path=/home/upskill/public_html 2>/dev/null || echo "HVAC attendees table does not exist"

echo ""
echo "=== 6. Test the Template Query Directly ==="
wp db query "SELECT 
    p.ID as attendee_id,
    p.post_parent as event_id,
    COALESCE(tec_full_name.meta_value, tpp_full_name.meta_value, tickets_full_name.meta_value, 'Unknown Attendee') as holder_name,
    COALESCE(tec_email.meta_value, tpp_email.meta_value, tickets_email.meta_value, tpp_attendee_email.meta_value, 'no-email@example.com') as holder_email,
    COALESCE(checked_in.meta_value, '0') as check_in
FROM wp_posts p
LEFT JOIN wp_postmeta tec_full_name ON p.ID = tec_full_name.post_id AND tec_full_name.meta_key = '_tec_tickets_commerce_full_name'
LEFT JOIN wp_postmeta tpp_full_name ON p.ID = tpp_full_name.post_id AND tpp_full_name.meta_key = '_tribe_tpp_full_name'
LEFT JOIN wp_postmeta tickets_full_name ON p.ID = tickets_full_name.post_id AND tickets_full_name.meta_key = '_tribe_tickets_full_name'
LEFT JOIN wp_postmeta tec_email ON p.ID = tec_email.post_id AND tec_email.meta_key = '_tec_tickets_commerce_email'
LEFT JOIN wp_postmeta tpp_email ON p.ID = tpp_email.post_id AND tpp_email.meta_key = '_tribe_tpp_email'
LEFT JOIN wp_postmeta tickets_email ON p.ID = tickets_email.post_id AND tickets_email.meta_key = '_tribe_tickets_email'
LEFT JOIN wp_postmeta tpp_attendee_email ON p.ID = tpp_attendee_email.post_id AND tpp_attendee_email.meta_key = '_tribe_tpp_attendee_email'
LEFT JOIN wp_postmeta checked_in ON p.ID = checked_in.post_id AND checked_in.meta_key = '_tribe_tickets_attendee_checked_in'
WHERE p.post_type IN ('tec_tc_attendee', 'tribe_tpp_attendees') 
AND p.post_parent = 6042
ORDER BY p.ID ASC;" --path=/home/upskill/public_html

EOF

echo ""
echo "Debug complete!"