<?php
require_once 'config.php';
checkAccess(['programmer']);

try {
    // 1. Obtener estadísticas antes de borrar
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM system_logs");
    $total = $stmt->fetch()['total'];

    // 2. Vaciar tabla
    $pdo->exec("TRUNCATE TABLE system_logs");

    // 3. Notificar por correo
    $to = "andresmathison@gmail.com";
    $subject = "SAC 2025 - Mantenimiento de Logs Realizado";
    $message = "Se ha realizado un reset del Datalogger.\nRegistros eliminados: $total\nFecha: " . date('Y-m-d H:i:s');
    @mail($to, $subject, $message, "From: SAC System <no-reply@tecnomath.com>");

    response(["success" => true, "deleted" => $total]);
} catch (Exception $e) { response(["error" => $e->getMessage()], 500); }
?>