<?php

/**
 * Guard Dog Debug Handlers
 * 
 * Handles admin actions for viewing and clearing debug logs.
 * 
 * @package GuardDog
 * @since 1.2.0
 */

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

class Guard_Dog_Debug_Handlers {
    
    /**
     * Initialize handlers by registering admin actions.
     *
     * @since 1.2.0
     */
    public function __construct() {
        add_action('admin_post_guard_dog_view_debug_log', [$this, 'handle_view_debug_log']);
        add_action('admin_post_guard_dog_clear_debug_log', [$this, 'handle_clear_debug_log']);
    }
    
    /**
     * Handle view debug log action.
     * 
     * Displays the debug log content in plain text format.
     * Requires manage_options capability and valid nonce.
     *
     * @since 1.2.0
     */
    public function handle_view_debug_log() {
        // Check user capabilities
        if (!current_user_can('manage_options')) {
            wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'guard-dog'));
        }
        
        // Verify nonce
        $nonce = sanitize_text_field(wp_unslash($_GET['_wpnonce'] ?? ''));
        if (!wp_verify_nonce($nonce, 'guard_dog_debug_log')) {
            wp_die(esc_html__('Security check failed. Please try again.', 'guard-dog'));
        }
        
        // Get debug instance for WordPress debug status
        $debug = Guard_Dog_Debug::instance();
        $wordpress_status = $debug->get_wordpress_debug_status();
        
        // Set headers for plain text display
        header('Content-Type: text/plain; charset=utf-8');
        
        echo "Guard Dog Debug Log Information\n";
        echo "==============================\n\n";
        
        echo "WordPress Debug Status:\n";
        echo "- WP_DEBUG: " . esc_html($wordpress_status['WP_DEBUG'] ? 'Enabled' : 'Disabled') . "\n";
        echo "- WP_DEBUG_LOG: " . esc_html($wordpress_status['WP_DEBUG_LOG'] ? 'Enabled' : 'Disabled') . "\n";
        echo "- Logging Active: " . esc_html($wordpress_status['is_logging_enabled'] ? 'Yes' : 'No') . "\n\n";
        
        if ($wordpress_status['is_logging_enabled']) {
            echo "Debug Log Location: " . esc_html($wordpress_status['debug_log_path']) . "\n";
            echo "Guard Dog logs to WordPress's standard debug.log file.\n";
            echo "Look for entries starting with '[Guard Dog]' in the debug.log file.\n\n";
        } else {
            echo "WordPress debugging is not fully enabled.\n";
            echo "To enable Guard Dog logging, add these lines to wp-config.php:\n";
            echo "define('WP_DEBUG', true);\n";
            echo "define('WP_DEBUG_LOG', true);\n\n";
        }
        
        echo "Time: " . esc_html((string) current_time('Y-m-d H:i:s')) . "\n";
        exit;
    }
    
    /**
     * Handle clear debug log action.
     * 
     * Clears the debug log file and redirects back to settings.
     * Requires manage_options capability and valid nonce.
     *
     * @since 1.2.0
     */
    public function handle_clear_debug_log() {
        // Check user capabilities
        if (!current_user_can('manage_options')) {
            wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'guard-dog'));
        }
        
        // Verify nonce
        $nonce = sanitize_text_field(wp_unslash($_GET['_wpnonce'] ?? ''));
        if (!wp_verify_nonce($nonce, 'guard_dog_debug_log')) {
            wp_die(esc_html__('Security check failed. Please try again.', 'guard-dog'));
        }
        
        // Output message that logs cannot be cleared programmatically
        wp_die(esc_html__('Guard Dog now logs to wp-content/debug.log. This file cannot be cleared from the admin. Please manage it manually.', 'guard-dog'));
    }
}

// Initialize handlers
new Guard_Dog_Debug_Handlers(); 