<?php
include "../config/connect.php";

$checkQuery = "
    SELECT COUNT(*) AS count FROM tb_agent 
    WHERE agent_balance < 0 
    AND DATEDIFF(NOW(), agent_created) > 2
";

$result = $call->query($checkQuery);
$row = $result->fetch_assoc();
$agentCount = $row['count'];

if ($agentCount > 0) {
    $call->begin_transaction();
    
    $deleteQuery = "
        DELETE FROM tb_agent 
        WHERE agent_balance < 0 
        AND DATEDIFF(NOW(), agent_created) > 2
    ";

    if ($call->query($deleteQuery)) {
        echo $call->affected_rows . " agen telah dihapus.\n";
        $call->commit();
    } else {
        $call->rollback();
        echo "Terjadi kesalahan dalam penghapusan agen.\n";
    }
} else {
    echo "Tidak ada agen yang memenuhi kriteria untuk dihapus.\n";
}

$call->close();
?>
