<?php
/**
 * Template Name: Trainer Profile
 * Description: Template for viewing trainer profile
 */

// Define constant to indicate we are in a page template
define('HVAC_IN_PAGE_TEMPLATE', true);

get_header();
?>


<div class="hvac-page-wrapper hvac-trainer-profile-page">
    <?php
    // Display trainer navigation menu
    if (class_exists('HVAC_Menu_System')) {
        HVAC_Menu_System::instance()->render_trainer_menu();
    }
    ?>
    
    <?php
    // Display breadcrumbs
    if (class_exists('HVAC_Breadcrumbs')) {
        echo HVAC_Breadcrumbs::instance()->render_breadcrumbs();
    }
    ?>
    <div class="container">
        <?php
        // Check if user is logged in and has proper permissions
        if (!is_user_logged_in()) {
            echo '<p>You must be logged in to view this page.</p>';
            get_footer();
            return;
        }

        if (!current_user_can('hvac_trainer') && !current_user_can('hvac_master_trainer') && !current_user_can('administrator')) {
            echo '<p>You must be a trainer to view this page.</p>';
            get_footer();
            return;
        }

        $user_id = get_current_user_id();

        // Get trainer profile using new system
        $profile_manager = HVAC_Trainer_Profile_Manager::get_instance();
        $profile = $profile_manager->get_trainer_profile($user_id);

        if (!$profile) {
            echo '<p>No trainer profile found. Please contact an administrator.</p>';
            get_footer();
            return;
        }

        // Get profile metadata
        $profile_meta = $profile_manager->get_profile_meta($profile->ID);
        $user = get_userdata($user_id);

        // Get coordinates if available
        $geocoding_service = HVAC_Geocoding_Service::get_instance();
        $coordinates = $geocoding_service->get_coordinates($profile->ID);
        ?>
        
        <div class="hvac-trainer-profile-view">
            <div class="hvac-page-header">
                <h1>Trainer Profile</h1>
                <div class="hvac-page-header-actions">
                    <a href="/trainer/profile/edit/" class="hvac-button hvac-button-primary">Edit Profile</a>
                    <button type="button" class="hvac-button hvac-button-secondary hvac-share-profile-btn" data-profile-id="<?php echo esc_attr($profile->ID); ?>">
                        <span class="dashicons dashicons-share"></span> Share Profile
                    </button>
                </div>
            </div>
            
            <div class="hvac-profile-content">
                <div class="hvac-profile-sidebar">
                    <div class="hvac-profile-photo">
                        <?php if (has_post_thumbnail($profile->ID)): ?>
                            <?php echo get_the_post_thumbnail($profile->ID, 'medium', ['alt' => $user->display_name]); ?>
                        <?php else: ?>
                            <div class="hvac-profile-photo-placeholder">
                                <span><?php echo esc_html(substr($user->first_name, 0, 1) . substr($user->last_name, 0, 1)); ?></span>
                            </div>
                        <?php endif; ?>
                    </div>
                    
                    <div class="hvac-profile-stats">
                        <div class="hvac-stat-item">
                            <span class="hvac-stat-value"><?php echo count_user_posts($user_id, 'tribe_events'); ?></span>
                            <span class="hvac-stat-label">Events Created</span>
                        </div>
                        <?php if (!empty($profile_meta['years_experience'])): ?>
                        <div class="hvac-stat-item">
                            <span class="hvac-stat-value"><?php echo esc_html($profile_meta['years_experience']); ?></span>
                            <span class="hvac-stat-label">Years Experience</span>
                        </div>
                        <?php endif; ?>
                        <?php if ($coordinates): ?>
                        <div class="hvac-stat-item">
                            <span class="hvac-stat-value">📍</span>
                            <span class="hvac-stat-label">Location Verified</span>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="hvac-profile-main">
                    <?php
                    // Get certifications from new system (with fallback to legacy)
                    $certifications = [];
                    $has_legacy_cert = !empty($profile_meta['certification_status']) || !empty($profile_meta['certification_type']) || !empty($profile_meta['date_certified']);
                    
                    if (class_exists('HVAC_Trainer_Certification_Manager')) {
                        $cert_manager = HVAC_Trainer_Certification_Manager::instance();
                        $trainer_certifications = $cert_manager->get_trainer_certifications($user_id);
                        
                        foreach ($trainer_certifications as $cert) {
                            $cert_type = get_post_meta($cert->ID, 'certification_type', true);
                            $status = get_post_meta($cert->ID, 'status', true) ?: 'active';
                            $issue_date = get_post_meta($cert->ID, 'issue_date', true);
                            $expiration_date = get_post_meta($cert->ID, 'expiration_date', true);
                            $certificate_number = get_post_meta($cert->ID, 'certificate_number', true);
                            
                            // Check expiration
                            $is_expired = false;
                            $expiration_status = '';
                            if ($expiration_date) {
                                $exp_timestamp = strtotime($expiration_date);
                                $now = time();
                                $days_until_expiry = ceil(($exp_timestamp - $now) / (24 * 60 * 60));
                                
                                if ($exp_timestamp < $now) {
                                    $is_expired = true;
                                    $expiration_status = 'Expired';
                                } elseif ($days_until_expiry <= 30) {
                                    $expiration_status = "Expires in {$days_until_expiry} days";
                                } else {
                                    $expiration_status = "Valid until " . date('F j, Y', $exp_timestamp) . " ({$days_until_expiry} days remaining)";
                                }
                            }
                            
                            $certifications[] = [
                                'type' => $cert_type,
                                'status' => $status,
                                'issue_date' => $issue_date,
                                'expiration_date' => $expiration_date,
                                'expiration_status' => $expiration_status,
                                'certificate_number' => $certificate_number,
                                'is_expired' => $is_expired
                            ];
                        }
                    }
                    
                    // Show certifications section if we have new certifications or legacy data
                    if (!empty($certifications) || $has_legacy_cert):
                    ?>
                    <div class="hvac-profile-section hvac-certification-section">
                        <h2>Certification Information</h2>
                        
                        <?php if (!empty($certifications)): ?>
                            <div class="hvac-certifications-grid">
                                <?php foreach ($certifications as $cert): ?>
                                    <div class="hvac-certification-card hvac-cert-<?php echo esc_attr(strtolower(str_replace(['measureQuick Certified ', ' '], ['', '-'], $cert['type']))); ?>">
                                        <div class="hvac-certification-card-header">
                                            <h3 class="hvac-certification-title"><?php echo esc_html($cert['type']); ?></h3>
                                            <span class="hvac-certification-status-badge status-<?php echo esc_attr(strtolower($cert['status'])); ?><?php echo $cert['is_expired'] ? ' status-expired' : ''; ?>">
                                                <?php echo $cert['is_expired'] ? 'Expired' : ucfirst(esc_html($cert['status'])); ?>
                                            </span>
                                        </div>
                                        
                                        <div class="hvac-certification-details">
                                            <?php if ($cert['certificate_number']): ?>
                                            <div class="hvac-certification-detail">
                                                <span class="hvac-certification-detail-label">Number:</span>
                                                <span class="hvac-certification-detail-value"><?php echo esc_html($cert['certificate_number']); ?></span>
                                            </div>
                                            <?php endif; ?>
                                            
                                            <?php if ($cert['issue_date']): ?>
                                            <div class="hvac-certification-detail">
                                                <span class="hvac-certification-detail-label">Issue Date:</span>
                                                <span class="hvac-certification-detail-value"><?php echo esc_html(date('M j, Y', strtotime($cert['issue_date']))); ?></span>
                                            </div>
                                            <?php endif; ?>
                                        </div>
                                        
                                        <?php if ($cert['expiration_status']): ?>
                                        <div class="hvac-certification-expiration <?php echo $cert['is_expired'] ? 'expiration-expired' : (strpos($cert['expiration_status'], 'Expires in') !== false ? 'expiration-expiring' : 'expiration-valid'); ?>">
                                            <?php echo esc_html($cert['expiration_status']); ?>
                                        </div>
                                        <?php endif; ?>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php else: ?>
                            <!-- Legacy certification display -->
                            <div class="hvac-profile-details">
                                <?php if (!empty($profile_meta['certification_status'])): ?>
                                <div class="hvac-detail-row">
                                    <span class="hvac-detail-label">Certification Status:</span>
                                    <span class="hvac-detail-value hvac-cert-status hvac-cert-status-<?php echo esc_attr(strtolower($profile_meta['certification_status'])); ?>">
                                        <?php echo esc_html($profile_meta['certification_status']); ?>
                                    </span>
                                </div>
                                <?php endif; ?>
                                <?php if (!empty($profile_meta['certification_type'])): ?>
                                <div class="hvac-detail-row">
                                    <span class="hvac-detail-label">Certification Type:</span>
                                    <span class="hvac-detail-value"><?php echo esc_html($profile_meta['certification_type']); ?></span>
                                </div>
                                <?php endif; ?>
                                <?php if (!empty($profile_meta['date_certified'])): ?>
                                <div class="hvac-detail-row">
                                    <span class="hvac-detail-label">Date Certified:</span>
                                    <span class="hvac-detail-value"><?php echo esc_html(date('F j, Y', strtotime($profile_meta['date_certified']))); ?></span>
                                </div>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                    <?php endif; ?>
                    
                    <div class="hvac-profile-section">
                        <h2>Personal Information</h2>
                        <div class="hvac-profile-details">
                            <div class="hvac-detail-row">
                                <span class="hvac-detail-label">Display Name:</span>
                                <span class="hvac-detail-value"><?php echo esc_html($profile_meta['trainer_display_name'] ?? $user->display_name); ?></span>
                            </div>
                            <div class="hvac-detail-row">
                                <span class="hvac-detail-label">Full Name:</span>
                                <span class="hvac-detail-value"><?php echo esc_html(($profile_meta['trainer_first_name'] ?? $user->first_name) . ' ' . ($profile_meta['trainer_last_name'] ?? $user->last_name)); ?></span>
                            </div>
                            <div class="hvac-detail-row">
                                <span class="hvac-detail-label">Email:</span>
                                <span class="hvac-detail-value"><?php echo esc_html($user->user_email); ?></span>
                            </div>
                            <?php if (!empty($profile_meta['role'])): ?>
                            <div class="hvac-detail-row">
                                <span class="hvac-detail-label">Role:</span>
                                <span class="hvac-detail-value"><?php echo esc_html(ucwords($profile_meta['role'])); ?></span>
                            </div>
                            <?php endif; ?>
                            <?php if (!empty($profile_meta['years_experience'])): ?>
                            <div class="hvac-detail-row">
                                <span class="hvac-detail-label">Years Experience:</span>
                                <span class="hvac-detail-value"><?php echo esc_html($profile_meta['years_experience']); ?> years</span>
                            </div>
                            <?php endif; ?>
                            <?php 
                            $location_parts = array_filter([
                                $profile_meta['trainer_city'] ?? '',
                                $profile_meta['trainer_state'] ?? '',
                                $profile_meta['trainer_country'] ?? ''
                            ]);
                            if (!empty($location_parts)):
                            ?>
                            <div class="hvac-detail-row">
                                <span class="hvac-detail-label">Location:</span>
                                <span class="hvac-detail-value"><?php echo esc_html(implode(', ', $location_parts)); ?></span>
                            </div>
                            <?php endif; ?>
                            <?php if (!empty($profile_meta['linkedin_profile_url'])): ?>
                            <div class="hvac-detail-row">
                                <span class="hvac-detail-label">LinkedIn:</span>
                                <span class="hvac-detail-value">
                                    <a href="<?php echo esc_url($profile_meta['linkedin_profile_url']); ?>" target="_blank">View Profile</a>
                                </span>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <?php if (!empty($profile->post_content)): ?>
                    <div class="hvac-profile-section">
                        <h2>About</h2>
                        <div class="hvac-profile-bio">
                            <?php echo wp_kses_post(wpautop($profile->post_content)); ?>
                        </div>
                    </div>
                    <?php endif; ?>
                    
                    <?php
                    // Get business type
                    $business_terms = get_the_terms($profile->ID, 'business_type');
                    $has_business_info = ($business_terms && !is_wp_error($business_terms)) || 
                                       !empty($profile_meta['annual_revenue_target']) ||
                                       !empty($profile_meta['application_details']);
                    if ($has_business_info):
                    ?>
                    <div class="hvac-profile-section">
                        <h2>Business Information</h2>
                        <div class="hvac-profile-details">
                            <?php if ($business_terms && !is_wp_error($business_terms)): ?>
                            <div class="hvac-detail-row">
                                <span class="hvac-detail-label">Business Type:</span>
                                <span class="hvac-detail-value"><?php echo esc_html($business_terms[0]->name); ?></span>
                            </div>
                            <?php endif; ?>
                            <?php if (!empty($profile_meta['annual_revenue_target'])): ?>
                            <div class="hvac-detail-row">
                                <span class="hvac-detail-label">Annual Revenue Target:</span>
                                <span class="hvac-detail-value">$<?php echo esc_html(number_format($profile_meta['annual_revenue_target'])); ?></span>
                            </div>
                            <?php endif; ?>
                            <?php if (!empty($profile_meta['application_details'])): ?>
                            <div class="hvac-detail-row">
                                <span class="hvac-detail-label">Application Details:</span>
                                <span class="hvac-detail-value"><?php echo esc_html($profile_meta['application_details']); ?></span>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php endif; ?>
                    
                    <?php
                    // Training Information Section
                    $has_training_info = !empty($profile_meta['training_audience']) ||
                                       !empty($profile_meta['training_formats']) ||
                                       !empty($profile_meta['training_locations']) ||
                                       !empty($profile_meta['training_resources']) ||
                                       !empty($profile_meta['personal_accreditation']);
                    if ($has_training_info):
                    ?>
                    <div class="hvac-profile-section">
                        <h2>Training Information</h2>
                        <div class="hvac-profile-details">
                            <?php if (!empty($profile_meta['training_audience'])): ?>
                            <div class="hvac-detail-row">
                                <span class="hvac-detail-label">Training Audience:</span>
                                <span class="hvac-detail-value"><?php echo esc_html($profile_meta['training_audience']); ?></span>
                            </div>
                            <?php endif; ?>
                            <?php if (!empty($profile_meta['training_formats'])): ?>
                            <div class="hvac-detail-row">
                                <span class="hvac-detail-label">Training Formats:</span>
                                <span class="hvac-detail-value"><?php echo esc_html($profile_meta['training_formats']); ?></span>
                            </div>
                            <?php endif; ?>
                            <?php if (!empty($profile_meta['training_locations'])): ?>
                            <div class="hvac-detail-row">
                                <span class="hvac-detail-label">Training Locations:</span>
                                <span class="hvac-detail-value"><?php echo esc_html($profile_meta['training_locations']); ?></span>
                            </div>
                            <?php endif; ?>
                            <?php if (!empty($profile_meta['training_resources'])): ?>
                            <div class="hvac-detail-row">
                                <span class="hvac-detail-label">Training Resources:</span>
                                <span class="hvac-detail-value"><?php echo esc_html($profile_meta['training_resources']); ?></span>
                            </div>
                            <?php endif; ?>
                            <?php if (!empty($profile_meta['personal_accreditation'])): ?>
                            <div class="hvac-detail-row">
                                <span class="hvac-detail-label">Personal Accreditation:</span>
                                <span class="hvac-detail-value"><?php echo esc_html($profile_meta['personal_accreditation']); ?></span>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Share Profile Modal -->
<div id="hvac-share-profile-modal" class="hvac-share-modal" style="display: none;">
    <div class="hvac-share-modal-content">
        <!-- Close Button -->
        <button class="hvac-modal-close" aria-label="Close">
            <span class="dashicons dashicons-no"></span>
        </button>
        
        <!-- Modal Title -->
        <h2 class="hvac-share-modal-title">Share Your Profile</h2>
        
        <!-- Modal Description -->
        <p class="hvac-share-description">
            Get more training requests by sharing your trainer profile on your website, social media, or email!
        </p>
        
        <!-- Share URL Section -->
        <div class="hvac-share-url-section">
            <label for="hvac-share-url" class="hvac-share-url-label">
                <strong>Your personal training profile link:</strong>
            </label>
            <div class="hvac-share-url-container">
                <input type="text" id="hvac-share-url" class="hvac-share-url-input" readonly value="" placeholder="Loading...">
                <button type="button" class="hvac-copy-url-btn" title="Copy to clipboard">
                    Copy
                </button>
            </div>
        </div>
        
        <!-- Profile Card Section -->
        <div class="hvac-share-card-section">
            <p class="hvac-share-card-description">Or screenshot the image below!</p>
            <div class="hvac-share-profile-card-container" id="hvac-share-card-container">
                <!-- Profile card will be loaded here via AJAX -->
                <div class="hvac-share-card-loading">
                    <span class="dashicons dashicons-update spin"></span>
                    <p>Loading profile card...</p>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
// The CSS and JS are handled by the HVAC_Scripts_Styles class
// Additional localization for profile-specific data
if (class_exists('HVAC_Scripts_Styles')) {
    $scripts_styles = HVAC_Scripts_Styles::instance();
    $scripts_styles->localize_sharing_data([
        'profile_id' => $profile->ID
    ]);
}

get_footer();
