<?php
defined('ABSPATH') || exit;

// -------------------------
// Traitement : vider les logs
// -------------------------
if (
    isset($_POST['hpixl_securicheck_vider_antispam_commentaires_logs_nonce']) &&
    wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['hpixl_securicheck_vider_antispam_commentaires_logs_nonce'])), 'hpixl_securicheck_vider_antispam_commentaires_logs') &&
    current_user_can('manage_options')
) {
    global $wpdb;
    $wpdb->query('TRUNCATE TABLE ' . $wpdb->prefix . HPIXL_SECURICHECK_TABLE_ANTISPAM_COMMENTAIRES_LOGS);
    echo '<div class="notice notice-success"><p>' . esc_html__('Logs vidés avec succès.', 'securicheck') . '</p></div>';
}

// -------------------------
// Récupération des logs
// -------------------------
global $wpdb;
$table = $wpdb->prefix . HPIXL_SECURICHECK_TABLE_ANTISPAM_COMMENTAIRES_LOGS;
$logs  = $wpdb->get_results(
    $wpdb->prepare('SELECT * FROM %i ORDER BY created_at DESC LIMIT 200', $table)
);

$antispam_enabled = get_option('hpixl_securicheck_toggle_page_anti_spam_commentaires') == 1;
?>

<div class="hpixl-securicheck-section" style="padding:20px;margin-top:20px;border-radius:0px!important;">
    <strong style="font-size:14px; display:block; margin-bottom:6px;"><?php esc_html_e('Logs Anti Spam commentaires', 'securicheck'); ?></strong>
    <p style="margin:0 0 6px;"><?php esc_html_e('Commentaires détectés comme spam ou suspects. Les 200 dernières entrées sont affichées.', 'securicheck'); ?></p>
</div>

<?php if (!$antispam_enabled): ?>
    <div class="notice notice-warning inline">
        <p><?php esc_html_e('La protection Anti Spam est désactivée. Aucun log ne sera enregistré tant qu\'elle est inactive.', 'securicheck'); ?></p>
    </div>
<?php endif; ?>

<table class="wp-list-table widefat striped">
    <thead>
        <tr>
            <th><?php esc_html_e('IP', 'securicheck'); ?></th>
            <th><?php esc_html_e('Email', 'securicheck'); ?></th>
            <th><?php esc_html_e('Score', 'securicheck'); ?></th>
            <th><?php esc_html_e('Raison', 'securicheck'); ?></th>
            <th><?php esc_html_e('Contenu', 'securicheck'); ?></th>
            <th><?php esc_html_e('Date', 'securicheck'); ?></th>
            <th><?php esc_html_e('Source', 'securicheck'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php if (!empty($logs)): ?>
            <?php foreach ($logs as $log): ?>
                <tr>
                    <td><?php echo esc_html($log->ip); ?></td>
                    <td><?php echo $log->email !== null ? esc_html($log->email) : '—'; ?></td>
                    <td>
                        <?php if ($log->score >= 60): ?>
                            <span style="color:#dc3232;font-weight:bold;"><?php echo esc_html($log->score); ?></span>
                        <?php else: ?>
                            <span style="color:#f0b849;font-weight:bold;"><?php echo esc_html($log->score); ?></span>
                        <?php endif; ?>
                    </td>
                    <td><?php echo $log->reason !== null ? esc_html($log->reason) : '—'; ?></td>
                    <td><?php echo $log->content !== null ? esc_html(wp_trim_words($log->content, 10)) : '—'; ?></td>
                    <td><?php echo esc_html(get_date_from_gmt($log->created_at, 'd/m/Y H:i:s')); ?></td>
                    <td>
                        <?php if ($log->is_audit): ?>
                            <span style="background:#2271b1;color:#fff;padding:2px 7px;border-radius:3px;font-size:11px;"><?php esc_html_e('Audit', 'securicheck'); ?></span>
                        <?php else: ?>
                            <span style="background:#ddd;color:#555;padding:2px 7px;border-radius:3px;font-size:11px;"><?php esc_html_e('Réel', 'securicheck'); ?></span>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
        <?php else: ?>
            <tr>
                <td colspan="7"><?php esc_html_e('Aucun spam détecté.', 'securicheck'); ?></td>
            </tr>
        <?php endif; ?>
    </tbody>
</table>

<form method="post" action="" style="margin-top:12px;">
    <?php wp_nonce_field('hpixl_securicheck_vider_antispam_commentaires_logs', 'hpixl_securicheck_vider_antispam_commentaires_logs_nonce'); ?>
    <button type="submit" class="button button-secondary" onclick="return confirm('<?php esc_attr_e('Vider tous les logs ? Cette action est irréversible.', 'securicheck'); ?>')">
        <?php esc_html_e('Vider les logs', 'securicheck'); ?>
    </button>
</form>
