<?php
/**
 * Certificate Fix Admin Page
 */

// Security check
if (!current_user_can('manage_options')) {
    wp_die('Unauthorized access');
}

// Get header
get_header();
?>

<div class="hvac-container">
    <div class="hvac-content-wrapper">
        <h1>Certificate System Diagnostics</h1>
        
        <div class="hvac-admin-section">
            <h2>Rewrite Rules</h2>
            <p>If certificate download URLs are returning 404 errors, flush the rewrite rules.</p>
            
            <form method="post" action="">
                <?php wp_nonce_field('hvac_flush_rewrite_rules', 'hvac_flush_nonce'); ?>
                <button type="submit" name="flush_rewrite_rules" class="button button-primary">
                    Flush Rewrite Rules
                </button>
            </form>
            
            <?php
            if (isset($_POST['flush_rewrite_rules']) && wp_verify_nonce($_POST['hvac_flush_nonce'], 'hvac_flush_rewrite_rules')) {
                // Initialize certificate security to ensure rules are added
                if (class_exists('HVAC_Certificate_Security')) {
                    HVAC_Certificate_Security::instance();
                }
                
                flush_rewrite_rules();
                echo '<div class="notice notice-success"><p>Rewrite rules have been flushed!</p></div>';
            }
            ?>
            
            <p><a href="<?php echo admin_url('admin.php?test_certificate_rewrite=1'); ?>" class="button">
                Test Certificate Rewrite Rules
            </a></p>
        </div>
        
        <div class="hvac-admin-section">
            <h2>Certificate Database</h2>
            <?php
            global $wpdb;
            $cert_table = $wpdb->prefix . 'hvac_certificates';
            
            // Check if table exists
            $table_exists = $wpdb->get_var("SHOW TABLES LIKE '$cert_table'") === $cert_table;
            
            if ($table_exists) {
                $total = $wpdb->get_var("SELECT COUNT(*) FROM $cert_table");
                $active = $wpdb->get_var("SELECT COUNT(*) FROM $cert_table WHERE revoked = 0");
                $revoked = $wpdb->get_var("SELECT COUNT(*) FROM $cert_table WHERE revoked = 1");
                
                echo "<p>✅ Certificate table exists</p>";
                echo "<ul>";
                echo "<li>Total certificates: $total</li>";
                echo "<li>Active certificates: $active</li>";
                echo "<li>Revoked certificates: $revoked</li>";
                echo "</ul>";
            } else {
                echo "<p>❌ Certificate table does not exist!</p>";
            }
            ?>
        </div>
        
        <div class="hvac-admin-section">
            <h2>Certificate Files</h2>
            <?php
            $upload_dir = wp_upload_dir();
            $cert_dir = $upload_dir['basedir'] . '/hvac-certificates';
            
            if (is_dir($cert_dir)) {
                echo "<p>✅ Certificate directory exists: <code>$cert_dir</code></p>";
                
                // Count PDF files
                $pdf_count = count(glob($cert_dir . '/*.pdf'));
                echo "<p>Total PDF files: $pdf_count</p>";
                
                // Check .htaccess
                if (file_exists($cert_dir . '/.htaccess')) {
                    echo "<p>✅ .htaccess file exists for security</p>";
                } else {
                    echo "<p>⚠️ .htaccess file missing - certificates may not be protected</p>";
                }
            } else {
                echo "<p>❌ Certificate directory does not exist!</p>";
            }
            ?>
        </div>
        
        <div class="hvac-admin-section">
            <h2>Recent Certificate Activity</h2>
            <?php
            if ($table_exists) {
                $recent = $wpdb->get_results("
                    SELECT c.*, p.post_title as event_title 
                    FROM $cert_table c 
                    LEFT JOIN {$wpdb->posts} p ON c.event_id = p.ID 
                    ORDER BY c.generated_date DESC 
                    LIMIT 10
                ");
                
                if ($recent) {
                    echo '<table class="wp-list-table widefat fixed striped">';
                    echo '<thead><tr><th>ID</th><th>Event</th><th>Generated</th><th>Status</th></tr></thead>';
                    echo '<tbody>';
                    
                    foreach ($recent as $cert) {
                        $status = $cert->revoked ? 'Revoked' : 'Active';
                        echo '<tr>';
                        echo '<td>' . $cert->certificate_id . '</td>';
                        echo '<td>' . esc_html($cert->event_title) . '</td>';
                        echo '<td>' . date('Y-m-d H:i', strtotime($cert->generated_date)) . '</td>';
                        echo '<td>' . $status . '</td>';
                        echo '</tr>';
                    }
                    
                    echo '</tbody></table>';
                } else {
                    echo '<p>No certificates found.</p>';
                }
            }
            ?>
        </div>
    </div>
</div>

<?php get_footer(); ?>