<?php
/**
 * Helper function to generate attendee profile links
 *
 * @package HVAC_Community_Events
 * @since 1.0.0
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Generate a link to view an attendee's profile
 *
 * @param int|object $attendee Attendee ID or attendee object
 * @param string $link_text Optional custom link text
 * @param array $classes Optional CSS classes
 * @return string HTML link to attendee profile
 */
function hvac_get_attendee_profile_link($attendee, $link_text = '', $classes = array()) {
    // Get attendee ID
    $attendee_id = 0;
    if (is_object($attendee)) {
        $attendee_id = isset($attendee->attendee_id) ? $attendee->attendee_id : 
                      (isset($attendee->ID) ? $attendee->ID : 0);
    } else {
        $attendee_id = intval($attendee);
    }
    
    if (!$attendee_id) {
        return '';
    }
    
    // Get profile page URL
    $profile_page = get_page_by_path('attendee-profile');
    if (!$profile_page) {
        return '';
    }
    
    $profile_url = add_query_arg('attendee_id', $attendee_id, get_permalink($profile_page->ID));
    
    // Default link text
    if (empty($link_text)) {
        $link_text = __('View Profile', 'hvac-community-events');
    }
    
    // Build classes
    $class_list = array('hvac-attendee-profile-link');
    if (!empty($classes)) {
        $class_list = array_merge($class_list, (array)$classes);
    }
    
    // Generate link
    return sprintf(
        '<a href="%s" class="%s" target="_blank" title="%s">%s <i class="fas fa-external-link-alt"></i></a>',
        esc_url($profile_url),
        esc_attr(implode(' ', $class_list)),
        esc_attr__('View attendee profile', 'hvac-community-events'),
        esc_html($link_text)
    );
}

/**
 * Generate a small icon link to view an attendee's profile
 *
 * @param int|object $attendee Attendee ID or attendee object
 * @return string HTML icon link to attendee profile
 */
function hvac_get_attendee_profile_icon($attendee) {
    // Get attendee ID
    $attendee_id = 0;
    if (is_object($attendee)) {
        $attendee_id = isset($attendee->attendee_id) ? $attendee->attendee_id : 
                      (isset($attendee->ID) ? $attendee->ID : 0);
    } else {
        $attendee_id = intval($attendee);
    }
    
    if (!$attendee_id) {
        return '';
    }
    
    // Get profile page URL
    $profile_page = get_page_by_path('attendee-profile');
    if (!$profile_page) {
        return '';
    }
    
    $profile_url = add_query_arg('attendee_id', $attendee_id, get_permalink($profile_page->ID));
    
    // Generate icon link
    return sprintf(
        '<a href="%s" class="hvac-attendee-profile-icon" target="_blank" title="%s"><i class="fas fa-user-circle"></i></a>',
        esc_url($profile_url),
        esc_attr__('View attendee profile', 'hvac-community-events')
    );
}

/**
 * Add profile link styles to pages that show attendee lists
 */
function hvac_attendee_profile_link_styles() {
    ?>
    <style>
        .hvac-attendee-profile-link {
            color: #007cba;
            text-decoration: none;
            font-size: 14px;
            display: inline-flex;
            align-items: center;
            gap: 5px;
        }
        .hvac-attendee-profile-link:hover {
            text-decoration: underline;
        }
        .hvac-attendee-profile-link i {
            font-size: 12px;
            opacity: 0.7;
        }
        
        .hvac-attendee-profile-icon {
            color: #007cba;
            font-size: 18px;
            text-decoration: none;
            display: inline-block;
            padding: 2px;
            transition: color 0.2s ease;
        }
        .hvac-attendee-profile-icon:hover {
            color: #005a87;
        }
        
        /* Add to table cells */
        .hvac-attendee-name-cell {
            display: flex;
            align-items: center;
            gap: 10px;
        }
    </style>
    <?php
}

// Add styles to relevant pages
add_action('wp_head', function() {
    if (is_page(array('hvac-dashboard', 'event-summary', 'certificate-reports', 'generate-certificates', 'email-attendees'))) {
        hvac_attendee_profile_link_styles();
    }
});