<?php
if (!defined('ABSPATH')) {
    exit;
}
?>
<div class="wrap aisbw-admin">
    <h1><?php esc_html_e('Logs', 'guco-ai-blog-writer'); ?></h1>

    <table class="wp-list-table widefat fixed striped aisbw-table">
        <thead>
            <tr>
                <th><?php esc_html_e('ID', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Time', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Topic ID', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Level', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Action', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Message', 'guco-ai-blog-writer'); ?></th>
                <th><?php esc_html_e('Context', 'guco-ai-blog-writer'); ?></th>
            </tr>
        </thead>
        <tbody>
        <?php if (empty($logs)) : ?>
            <tr>
                <td colspan="7"><?php esc_html_e('No logs found.', 'guco-ai-blog-writer'); ?></td>
            </tr>
        <?php endif; ?>

        <?php foreach ($logs as $log) : ?>
            <?php
            $context = json_decode((string) ($log['context'] ?? ''), true);
            $context_pretty = is_array($context) ? wp_json_encode($context, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : '';
            ?>
            <tr>
                <td><?php echo esc_html((string) $log['id']); ?></td>
                <td><?php echo esc_html((string) $log['created_at']); ?></td>
                <td><?php echo esc_html((string) ($log['topic_id'] ?: '-')); ?></td>
                <td><?php echo esc_html((string) $log['level']); ?></td>
                <td><?php echo esc_html((string) $log['action']); ?></td>
                <td><?php echo esc_html((string) $log['message']); ?></td>
                <td>
                    <?php if ($context_pretty) : ?>
                        <details>
                            <summary><?php esc_html_e('Show', 'guco-ai-blog-writer'); ?></summary>
                            <pre class="aisbw-code"><?php echo esc_html($context_pretty); ?></pre>
                        </details>
                    <?php else : ?>
                        -
                    <?php endif; ?>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</div>
