<?php
// ============================================================
//  clear_log.php — Clear Activity Log (Super Admin Only)
//  Place in: /admin/clear_log.php
// ============================================================
require_once 'includes/auth_check.php';
require_super_admin();

$csrf = $_GET['csrf'] ?? '';

if ($csrf !== ($_SESSION['csrf_token'] ?? '')) {
    $_SESSION['flash_error'] = 'Invalid security token.';
    header('Location: settings.php'); exit;
}

// Count before deleting for the log entry
$count = db_fetch_one("SELECT COUNT(*) AS c FROM activity_log")['c'] ?? 0;

// Delete all log entries
db_execute("DELETE FROM activity_log");

// Write a new entry recording who cleared it
db_execute(
    "INSERT INTO activity_log (admin_id, action, target_type, details, ip_address)
     VALUES (?, 'cleared_activity_log', 'system', ?, ?)",
    [
        $current_admin['id'],
        "Cleared {$count} activity log entries",
        $_SERVER['REMOTE_ADDR']
    ]
);

$_SESSION['flash_success'] = "Activity log cleared. {$count} entries deleted.";
header('Location: activity_log.php');
exit;
