<?php
/**
 * Simplified and Fixed Certificate Reports Template
 *
 * @package HVAC_Community_Events
 * @subpackage Templates/Certificates
 */

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

// Get current user ID
$current_user_id = get_current_user_id();

// Initialize variables with defaults
$certificates = array();
$certificate_stats = array('total' => 0, 'active' => 0, 'revoked' => 0, 'emailed' => 0);
$events = array();
$filter_event = isset($_GET['filter_event']) ? absint($_GET['filter_event']) : 0;
$filter_status = isset($_GET['filter_status']) ? sanitize_text_field($_GET['filter_status']) : 'active';

// Start output buffering to prevent issues
ob_start();

try {
    // Get user's events directly from database to bypass TEC issues
    global $wpdb;
    
    // Build author filter - only current user's events
    $events = $wpdb->get_results($wpdb->prepare(
        "SELECT ID, post_title, post_date 
         FROM {$wpdb->posts} 
         WHERE post_type = 'tribe_events' 
         AND post_author = %d 
         AND post_status = 'publish' 
         ORDER BY post_date DESC",
        $current_user_id
    ));

    // Check if certificate table exists
    $cert_table = $wpdb->prefix . 'hvac_certificates';
    $table_exists = $wpdb->get_var("SHOW TABLES LIKE '$cert_table'") === $cert_table;
    
    if ($table_exists && !empty($events)) {
        // Get event IDs for the user
        $event_ids = array_column($events, 'ID');
        $event_ids_placeholder = implode(',', array_fill(0, count($event_ids), '%d'));
        
        // Get certificate statistics
        $total_certs = $wpdb->get_var($wpdb->prepare(
            "SELECT COUNT(*) FROM $cert_table WHERE event_id IN ($event_ids_placeholder)",
            ...$event_ids
        ));
        
        $active_certs = $wpdb->get_var($wpdb->prepare(
            "SELECT COUNT(*) FROM $cert_table WHERE event_id IN ($event_ids_placeholder) AND revoked = 0",
            ...$event_ids
        ));
        
        $revoked_certs = $wpdb->get_var($wpdb->prepare(
            "SELECT COUNT(*) FROM $cert_table WHERE event_id IN ($event_ids_placeholder) AND revoked = 1",
            ...$event_ids
        ));
        
        $emailed_certs = $wpdb->get_var($wpdb->prepare(
            "SELECT COUNT(*) FROM $cert_table WHERE event_id IN ($event_ids_placeholder) AND email_sent = 1",
            ...$event_ids
        ));
        
        $certificate_stats = array(
            'total' => intval($total_certs),
            'active' => intval($active_certs),
            'revoked' => intval($revoked_certs),
            'emailed' => intval($emailed_certs)
        );
        
        // Get certificates based on filters
        $where_conditions = array("event_id IN ($event_ids_placeholder)");
        $query_params = $event_ids;
        
        // Add event filter if specified
        if ($filter_event > 0 && in_array($filter_event, $event_ids)) {
            $where_conditions = array("event_id = %d");
            $query_params = array($filter_event);
        }
        
        // Add status filter
        if ($filter_status === 'active') {
            $where_conditions[] = "revoked = 0";
        } elseif ($filter_status === 'revoked') {
            $where_conditions[] = "revoked = 1";
        }
        
        $where_clause = "WHERE " . implode(" AND ", $where_conditions);
        
        $certificates = $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM $cert_table $where_clause ORDER BY date_generated DESC LIMIT 50",
            ...$query_params
        ));
    }
    
} catch (Exception $e) {
    // Log error but continue with empty data
    error_log('Certificate Reports Error: ' . $e->getMessage());
}

// Clean output buffer
ob_end_clean();

// Get header
get_header();
?>

<div class="hvac-container">
    <div class="hvac-content-wrapper">
        <!-- Navigation Header -->
        <div class="hvac-dashboard-header">
            <h1 class="entry-title">Certificate Reports</h1>
            <div class="hvac-dashboard-nav">
                <a href="<?php echo esc_url(home_url('/hvac-dashboard/')); ?>" class="ast-button ast-button-secondary">Dashboard</a>
                <a href="<?php echo esc_url(home_url('/generate-certificates/')); ?>" class="ast-button ast-button-secondary">Generate Certificates</a>
                <a href="<?php echo esc_url(home_url('/manage-event/')); ?>" class="ast-button ast-button-primary">Create Event</a>
            </div>
        </div>
        
        <div class="hvac-page-header">
            <p class="hvac-page-description">View and manage all certificates you've generated for event attendees.</p>
        </div>
        
        <!-- Certificate Statistics -->
        <div class="hvac-section hvac-stats-section">
            <h2>Certificate Statistics</h2>
            
            <div class="hvac-certificate-stats">
                <div class="hvac-stat-card">
                    <h3>Total Certificates</h3>
                    <div class="hvac-stat-value"><?php echo esc_html($certificate_stats['total']); ?></div>
                </div>
                
                <div class="hvac-stat-card">
                    <h3>Active Certificates</h3>
                    <div class="hvac-stat-value"><?php echo esc_html($certificate_stats['active']); ?></div>
                </div>
                
                <div class="hvac-stat-card">
                    <h3>Revoked Certificates</h3>
                    <div class="hvac-stat-value"><?php echo esc_html($certificate_stats['revoked']); ?></div>
                </div>
                
                <div class="hvac-stat-card">
                    <h3>Emailed Certificates</h3>
                    <div class="hvac-stat-value"><?php echo esc_html($certificate_stats['emailed']); ?></div>
                </div>
            </div>
        </div>
        
        <!-- Certificate Filters -->
        <div class="hvac-section hvac-filters-section">
            <h2>Certificate Filters</h2>
            
            <form method="get" class="hvac-certificate-filters">
                <div class="hvac-filter-group">
                    <label for="filter_event">Event:</label>
                    <select name="filter_event" id="filter_event">
                        <option value="0">All Events</option>
                        <?php foreach ($events as $event) : ?>
                            <option value="<?php echo esc_attr($event->ID); ?>" <?php selected($filter_event, $event->ID); ?>>
                                <?php echo esc_html($event->post_title); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div class="hvac-filter-group">
                    <label for="filter_status">Status:</label>
                    <select name="filter_status" id="filter_status">
                        <option value="all" <?php selected($filter_status, 'all'); ?>>All Certificates</option>
                        <option value="active" <?php selected($filter_status, 'active'); ?>>Active Only</option>
                        <option value="revoked" <?php selected($filter_status, 'revoked'); ?>>Revoked Only</option>
                    </select>
                </div>
                
                <div class="hvac-filter-group hvac-filter-submit">
                    <button type="submit" class="hvac-button hvac-primary hvac-touch-target">Apply Filters</button>
                </div>
            </form>
        </div>
        
        <!-- Certificate Listing -->
        <div class="hvac-section hvac-certificates-section">
            <h2>Certificate Listing</h2>
            
            <?php if (empty($events)) : ?>
                <div class="hvac-no-certificates">
                    <p>You don't have any events yet. Create your first event to start generating certificates.</p>
                    <p><a href="<?php echo esc_url(home_url('/manage-event/')); ?>" class="hvac-button hvac-primary">Create Event</a></p>
                </div>
            <?php elseif (empty($certificates)) : ?>
                <div class="hvac-no-certificates">
                    <p>No certificates found matching your filters.</p>
                    
                    <?php if ($filter_event > 0 || $filter_status !== 'all') : ?>
                        <p><a href="<?php echo esc_url(remove_query_arg(array('filter_event', 'filter_status'))); ?>">Clear filters</a> to see all your certificates.</p>
                    <?php else : ?>
                        <p>Generate certificates for your event attendees on the <a href="<?php echo esc_url(home_url('/generate-certificates/')); ?>">Generate Certificates</a> page.</p>
                    <?php endif; ?>
                </div>
            <?php else : ?>
                <div class="hvac-certificate-table-wrapper">
                    <table class="hvac-certificate-table">
                        <thead>
                            <tr>
                                <th>Certificate #</th>
                                <th>Event</th>
                                <th>Attendee</th>
                                <th>Date Generated</th>
                                <th>Status</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($certificates as $certificate) : 
                                // Get certificate data safely
                                $certificate_number = esc_html($certificate->certificate_number ?? 'N/A');
                                $event_id = intval($certificate->event_id ?? 0);
                                $attendee_id = intval($certificate->attendee_id ?? 0);
                                $generated_date = $certificate->date_generated ? date_i18n(get_option('date_format'), strtotime($certificate->date_generated)) : 'Unknown';
                                $is_revoked = !empty($certificate->revoked);
                                $is_emailed = !empty($certificate->email_sent);
                                
                                // Get event and attendee information safely
                                $event_title = get_the_title($event_id) ?: 'Unknown Event';
                                $attendee_name = get_post_meta($attendee_id, '_tribe_tickets_full_name', true) ?: 'Attendee #' . $attendee_id;
                                
                                // Status text and class
                                $status_text = $is_revoked ? 'Revoked' : 'Active';
                                $status_class = $is_revoked ? 'hvac-status-revoked' : 'hvac-status-active';
                            ?>
                                <tr class="<?php echo $is_revoked ? 'hvac-certificate-revoked' : ''; ?>">
                                    <td><?php echo $certificate_number; ?></td>
                                    <td>
                                        <a href="<?php echo esc_url(get_permalink($event_id)); ?>" target="_blank">
                                            <?php echo esc_html($event_title); ?>
                                        </a>
                                    </td>
                                    <td><?php echo esc_html($attendee_name); ?></td>
                                    <td><?php echo esc_html($generated_date); ?></td>
                                    <td>
                                        <span class="<?php echo esc_attr($status_class); ?>">
                                            <?php echo esc_html($status_text); ?>
                                        </span>
                                    </td>
                                    <td class="hvac-certificate-actions">
                                        <?php if (!$is_revoked) : ?>
                                            <button class="hvac-view-certificate" data-certificate-id="<?php echo esc_attr($certificate->certificate_id ?? ''); ?>">View</button>
                                            <button class="hvac-email-certificate" data-certificate-id="<?php echo esc_attr($certificate->certificate_id ?? ''); ?>"><?php echo $is_emailed ? 'Re-email' : 'Email'; ?></button>
                                        <?php else : ?>
                                            <span class="hvac-certificate-revoked-message">Certificate revoked</span>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<style>
.hvac-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

.hvac-dashboard-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    flex-wrap: wrap;
    gap: 15px;
}

.hvac-dashboard-nav {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.hvac-certificate-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
}

.hvac-stat-card {
    background: #f8f9fa;
    padding: 20px;
    border-radius: 8px;
    text-align: center;
    border: 2px solid #e9ecef;
}

.hvac-stat-card h3 {
    margin: 0 0 10px 0;
    font-size: 14px;
    color: #6c757d;
    text-transform: uppercase;
}

.hvac-stat-value {
    font-size: 32px;
    font-weight: bold;
    color: #007cba;
}

.hvac-certificate-filters {
    display: flex;
    gap: 20px;
    align-items: end;
    flex-wrap: wrap;
    margin-bottom: 30px;
}

.hvac-filter-group label {
    display: block;
    margin-bottom: 5px;
    font-weight: bold;
}

.hvac-filter-group select {
    padding: 8px 12px;
    border: 1px solid #ddd;
    border-radius: 4px;
    min-width: 150px;
}

.hvac-button {
    padding: 10px 20px;
    background: #007cba;
    color: white;
    text-decoration: none;
    border-radius: 4px;
    border: none;
    cursor: pointer;
    display: inline-block;
}

.hvac-button:hover {
    background: #005a87;
    color: white;
}

.hvac-touch-target {
    min-height: 44px;
    min-width: 44px;
}

.hvac-certificate-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
}

.hvac-certificate-table th,
.hvac-certificate-table td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid #ddd;
}

.hvac-certificate-table th {
    background: #f8f9fa;
    font-weight: bold;
}

.hvac-no-certificates {
    text-align: center;
    padding: 40px;
    background: #f8f9fa;
    border-radius: 8px;
}

.hvac-status-active {
    color: #28a745;
    font-weight: bold;
}

.hvac-status-revoked {
    color: #dc3545;
    font-weight: bold;
}

.hvac-certificate-revoked {
    opacity: 0.6;
}

@media (max-width: 768px) {
    .hvac-dashboard-header {
        flex-direction: column;
        align-items: stretch;
    }
    
    .hvac-certificate-filters {
        flex-direction: column;
    }
    
    .hvac-certificate-table {
        font-size: 14px;
    }
    
    .hvac-certificate-table th,
    .hvac-certificate-table td {
        padding: 8px;
    }
}
</style>

<?php
get_footer();
?>