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

$verifications = mysqli_query($conn, "select * from verifications where status = 1 and bvn_status = 1 and (select id from dedicated_accounts where verifications.uid =  dedicated_accounts.uid and status = 1 limit 1) is NULL order by id desc");
while($verify = mysqli_fetch_assoc($verifications)){
    $uid = $verify['uid'];
    $user = mysqli_fetch_assoc(mysqli_query($conn, "select * from users where uid = '$uid' limit 1"));
    
    $bvnDetails = $verify["bvn_data"] ? json_decode($verify["bvn_data"], true) : array("firstName"=>$user["fname"], "lastName"=>$user["lname"]);
    
    $accountStatus = mysqli_query($conn, "select status,cus_code from dedicated_accounts where uid =  '$uid' limit 1");
    if(mysqli_num_rows($accountStatus) == 0){
        $accountStatus = -1;
        $customer = createCustomer($user["email"],$bvnDetails["firstName"], $bvnDetails["lastName"], $user["phone"]);
    }else{
        $accountStatus = mysqli_fetch_assoc($accountStatus);
        $customer = $accountStatus['cus_code'];
        $accountStatus = intval($accountStatus['status']);
    }
    if($customer){
        $account = validateCustomer($customer,$bvnDetails["firstName"], $bvnDetails["lastName"], $verify["bvn"]);
        if($accountStatus == -1 && $account){
            $sql = $conn->prepare("INSERT INTO `dedicated_accounts`(`uid`, `bank`, `number`, `cus_code`, `status`) VALUES (?,'','',?,0)");
            $sql->bind_param("ss",$uid,$customer);
            $sql->execute();
        }
    }
}
mysqli_close($conn);
?>