<?php
require_once("/home/fundbridge/crons.ballng.com/db.php");

$ASSET_URL = "/home/fundbridge/assets.ballng.com/";
die();
$tables = array(
    array("table"=>"users", "key"=>"id", "status" => "2"),
    array("table"=>"admins", "key"=>"id", "status" => "2"),
    array("table"=>"roles", "key"=>"id", "status" => "2"),
    array("table"=>"sms", "key"=>"id", "status" => "5"),
    array("table"=>"app", "key"=>"id", "status" => "5"),
    array("table"=>"emails", "key"=>"id", "status" => "5"),
    array("table"=>"loan_categories", "key"=>"id", "status" => "2"),
);

$deleteDuration = 30;
function checkIfDelete($item){
    global $deleteDuration;
    if(time() >= strtotime("+$deleteDuration days", strtotime($item["date_updated"]))){
        return true;
    }
    return false;
}
function deleteItem(&$item, $keys, $table){
    global $conn;
    $key = array_keys($item)[0];
    $value = $item[$key];
    return $conn->prepare("delete from $table where $key = '$value'")->execute();
}
function processDelete($row){
    global $conn;
    $table = $row["table"];
    $key = $row["key"];
    $status = $row["status"];
    $data = $conn->query("select $key,date_updated from $table where status = $status")->fetch_all(MYSQLI_ASSOC);
    $filterDelete = array_filter($data, "checkIfDelete");
    array_walk($filterDelete, "deleteItem",$table);
}
foreach($tables as $table){
    processDelete($table);
}
$conn->close();
?>