<?php
/**
 * Logs Page
 *
 * @package AIContentGenerator
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Get diagnostics and suggestions
$diagnostics = AICG\System_Diagnostics::get_all_diagnostics();
$suggestions = AICG\System_Diagnostics::get_suggestions();
$health_score = AICG\System_Diagnostics::get_health_score();

// Handle log actions
if (isset($_POST['roidaico_clear_logs']) && check_admin_referer('roidaico_clear_logs')) {
    AICG\Action_Logger::clear_all_logs();
    echo '<div class="notice notice-success"><p>' . esc_html__('All logs have been cleared.', 'roidnet-ai-content-generator') . '</p></div>';
}

if (isset($_POST['roidaico_clear_old_logs']) && check_admin_referer('roidaico_clear_old_logs')) {
    $days = isset($_POST['days']) ? absint($_POST['days']) : 30;
    $deleted = AICG\Action_Logger::clear_old_logs($days);
    echo '<div class="notice notice-success"><p>' . sprintf(esc_html__('Cleared %d old log entries.', 'roidnet-ai-content-generator'), $deleted) . '</p></div>';
}

// Pagination
$per_page = 50;
$current_page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
$offset = ($current_page - 1) * $per_page;

// Filter
$status_filter = isset($_GET['status']) ? sanitize_text_field($_GET['status']) : '';

// Get logs
if ($status_filter) {
    $logs = AICG\Action_Logger::get_logs_by_status($status_filter, $per_page, $offset);
    $total_logs = AICG\Action_Logger::get_logs_count($status_filter);
} else {
    $logs = AICG\Action_Logger::get_logs($per_page, $offset);
    $total_logs = AICG\Action_Logger::get_logs_count();
}

$total_pages = ceil($total_logs / $per_page);
?>

<div class="wrap roidaico-wrap">
    <h1>
        <?php esc_html_e('System Log', 'roidnet-ai-content-generator'); ?>
        <span class="roidaico-health-badge roidaico-health-<?php echo $health_score >= 80 ? 'good' : ($health_score >= 50 ? 'fair' : 'poor'); ?>">
            <?php printf(esc_html__('Health: %d%%', 'roidnet-ai-content-generator'), $health_score); ?>
        </span>
    </h1>

    <?php if (!empty($suggestions)): ?>
    <!-- Suggestions Section -->
    <div class="roidaico-suggestions-section">
        <h2>
            <span class="dashicons dashicons-lightbulb"></span>
            <?php esc_html_e('Suggestions & Recommendations', 'roidnet-ai-content-generator'); ?>
        </h2>
        <div class="roidaico-suggestions-grid">
            <?php foreach ($suggestions as $suggestion): ?>
            <div class="roidaico-suggestion-card roidaico-suggestion-<?php echo esc_attr($suggestion['type']); ?>">
                <div class="roidaico-suggestion-icon">
                    <span class="dashicons dashicons-<?php echo esc_attr($suggestion['icon']); ?>"></span>
                </div>
                <div class="roidaico-suggestion-content">
                    <h3><?php echo esc_html($suggestion['title']); ?></h3>
                    <p><?php echo esc_html($suggestion['description']); ?></p>
                    <?php if (!empty($suggestion['code'])): ?>
                    <pre class="roidaico-suggestion-code"><?php echo esc_html($suggestion['code']); ?></pre>
                    <?php endif; ?>
                    <?php if (!empty($suggestion['action'])): ?>
                    <a href="<?php echo esc_url($suggestion['action']['url']); ?>" class="button button-small">
                        <?php echo esc_html($suggestion['action']['text']); ?>
                    </a>
                    <?php endif; ?>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- System Diagnostics -->
    <div class="roidaico-section">
        <h2>
            <span class="dashicons dashicons-heart"></span>
            <?php esc_html_e('System Diagnostics', 'roidnet-ai-content-generator'); ?>
        </h2>
        <div class="roidaico-diagnostics-grid">
            <?php foreach ($diagnostics as $diagnostic): ?>
            <div class="roidaico-diagnostic-card roidaico-diagnostic-<?php echo esc_attr($diagnostic['status']); ?>">
                <div class="roidaico-diagnostic-header">
                    <span class="roidaico-diagnostic-status">
                        <?php
                        switch ($diagnostic['status']) {
                            case 'ok':
                                echo '<span class="dashicons dashicons-yes-alt"></span>';
                                break;
                            case 'warning':
                                echo '<span class="dashicons dashicons-warning"></span>';
                                break;
                            case 'error':
                                echo '<span class="dashicons dashicons-dismiss"></span>';
                                break;
                            default:
                                echo '<span class="dashicons dashicons-info"></span>';
                        }
                        ?>
                    </span>
                    <h3><?php echo esc_html($diagnostic['name']); ?></h3>
                </div>
                <p class="roidaico-diagnostic-message"><?php echo esc_html($diagnostic['message']); ?></p>
                <?php if (!empty($diagnostic['details'])): ?>
                <ul class="roidaico-diagnostic-details">
                    <?php foreach ($diagnostic['details'] as $detail): ?>
                    <li><?php echo esc_html($detail); ?></li>
                    <?php endforeach; ?>
                </ul>
                <?php endif; ?>
            </div>
            <?php endforeach; ?>
        </div>
    </div>

    <!-- Logs Section -->
    <div class="roidaico-section">
        <h2>
            <span class="dashicons dashicons-list-view"></span>
            <?php esc_html_e('Activity Logs', 'roidnet-ai-content-generator'); ?>
            <span class="roidaico-log-count"><?php printf(esc_html__('%d entries', 'roidnet-ai-content-generator'), $total_logs); ?></span>
        </h2>

        <!-- Filters and Actions -->
        <div class="roidaico-logs-toolbar">
            <div class="roidaico-logs-filters">
                <form method="get">
                    <input type="hidden" name="page" value="roidaico-logs">
                    <select name="status" onchange="this.form.submit()">
                        <option value=""><?php esc_html_e('All Statuses', 'roidnet-ai-content-generator'); ?></option>
                        <option value="info" <?php selected($status_filter, 'info'); ?>><?php esc_html_e('Info', 'roidnet-ai-content-generator'); ?></option>
                        <option value="success" <?php selected($status_filter, 'success'); ?>><?php esc_html_e('Success', 'roidnet-ai-content-generator'); ?></option>
                        <option value="warning" <?php selected($status_filter, 'warning'); ?>><?php esc_html_e('Warning', 'roidnet-ai-content-generator'); ?></option>
                        <option value="error" <?php selected($status_filter, 'error'); ?>><?php esc_html_e('Error', 'roidnet-ai-content-generator'); ?></option>
                    </select>
                </form>
            </div>
            <div class="roidaico-logs-actions">
                <form method="post" style="display: inline-block;">
                    <?php wp_nonce_field('roidaico_clear_old_logs'); ?>
                    <select name="days">
                        <option value="7"><?php esc_html_e('Older than 7 days', 'roidnet-ai-content-generator'); ?></option>
                        <option value="30" selected><?php esc_html_e('Older than 30 days', 'roidnet-ai-content-generator'); ?></option>
                        <option value="90"><?php esc_html_e('Older than 90 days', 'roidnet-ai-content-generator'); ?></option>
                    </select>
                    <button type="submit" name="roidaico_clear_old_logs" class="button">
                        <?php esc_html_e('Clear Old Logs', 'roidnet-ai-content-generator'); ?>
                    </button>
                </form>
                <form method="post" style="display: inline-block;" onsubmit="return confirm('<?php esc_attr_e('Are you sure you want to clear all logs?', 'roidnet-ai-content-generator'); ?>');">
                    <?php wp_nonce_field('roidaico_clear_logs'); ?>
                    <button type="submit" name="roidaico_clear_logs" class="button button-link-delete">
                        <?php esc_html_e('Clear All Logs', 'roidnet-ai-content-generator'); ?>
                    </button>
                </form>
            </div>
        </div>

        <!-- Logs Table -->
        <?php if (!empty($logs)): ?>
        <table class="wp-list-table widefat fixed striped">
            <thead>
                <tr>
                    <th style="width: 150px;"><?php esc_html_e('Date', 'roidnet-ai-content-generator'); ?></th>
                    <th style="width: 80px;"><?php esc_html_e('Status', 'roidnet-ai-content-generator'); ?></th>
                    <th style="width: 120px;"><?php esc_html_e('Action', 'roidnet-ai-content-generator'); ?></th>
                    <th><?php esc_html_e('Message', 'roidnet-ai-content-generator'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($logs as $log): ?>
                <tr>
                    <td>
                        <span title="<?php echo esc_attr($log->created_at); ?>">
                            <?php echo esc_html(human_time_diff(strtotime($log->created_at), current_time('timestamp'))); ?>
                            <?php esc_html_e('ago', 'roidnet-ai-content-generator'); ?>
                        </span>
                    </td>
                    <td>
                        <span class="roidaico-log-status roidaico-log-<?php echo esc_attr($log->status); ?>">
                            <?php echo esc_html(ucfirst($log->status)); ?>
                        </span>
                    </td>
                    <td>
                        <code><?php echo esc_html($log->action); ?></code>
                    </td>
                    <td><?php echo esc_html($log->message); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <!-- Pagination -->
        <?php if ($total_pages > 1): ?>
        <div class="tablenav bottom">
            <div class="tablenav-pages">
                <span class="displaying-num">
                    <?php printf(esc_html__('%d items', 'roidnet-ai-content-generator'), $total_logs); ?>
                </span>
                <span class="pagination-links">
                    <?php if ($current_page > 1): ?>
                    <a class="prev-page button" href="<?php echo esc_url(add_query_arg('paged', $current_page - 1)); ?>">
                        <span>‹</span>
                    </a>
                    <?php endif; ?>
                    
                    <span class="paging-input">
                        <?php printf(esc_html__('%1$d of %2$d', 'roidnet-ai-content-generator'), $current_page, $total_pages); ?>
                    </span>
                    
                    <?php if ($current_page < $total_pages): ?>
                    <a class="next-page button" href="<?php echo esc_url(add_query_arg('paged', $current_page + 1)); ?>">
                        <span>›</span>
                    </a>
                    <?php endif; ?>
                </span>
            </div>
        </div>
        <?php endif; ?>

        <?php else: ?>
        <div class="roidaico-empty-state">
            <span class="dashicons dashicons-format-aside"></span>
            <h3><?php esc_html_e('No Logs Yet', 'roidnet-ai-content-generator'); ?></h3>
            <p><?php esc_html_e('Activity logs will appear here as the plugin operates.', 'roidnet-ai-content-generator'); ?></p>
        </div>
        <?php endif; ?>
    </div>
</div>
