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

$mails = mysqli_query($conn, "select * from emails where status = 1");

$rcp_types = ["","Users","Others","","","","","",""];
$rcp_tables = ["","users","","","","","","",""];
$rcp_userID = ["","uid","","","","","","",""];
$rcp_status = ["1","1","","","","","","",""];
while($sm = mysqli_fetch_assoc($mails)){
    $rcp = intval($sm['recipients']);
    $emails = array();
    
    
    $table = $rcp_tables[$rcp];
    $uid = $rcp_userID[$rcp];
    $status = $rcp_status[$rcp];
    
    $rcps = $rcp == 0 ? [] : json_decode($sm["emails"], true);
    if($table != ""){
        if(empty($rcps)){
            //all recipients
            $emails = $conn->query("select email from $table where email != '' and status = '$status'")->fetch_all(MYSQLI_ASSOC);
            if($emails){ $emails = array_map(function($email){ return $email['email']; }, $emails); }
        }else{
            foreach($rcps as $rc){
                if($rc != ""){
                    $check_rc = mysqli_query($conn, "select email from $table where email != '' and $uid = '$rc' and status = '$status'");
                    if(mysqli_num_rows($check_rc)){
                        $emails[] = mysqli_fetch_assoc($check_rc)["email"] ;
                    }
                }
            }
        }
    }else{
        if($rcp == 0){
            // send to all
            $emails = $conn->query("select email from users where email != '' and status = '1'")->fetch_all(MYSQLI_ASSOC);
            if($emails){ $emails = array_map(function($email){ return $email['email']; }, $emails); }
        }else if($rcp == 3){
            // send to users without loans
            $emails = $conn->query("select email from users where email != '' and status = '1' and coalesce((select count(id) from loans where uid = users.uid limit 1),0) = 0")->fetch_all(MYSQLI_ASSOC);
            if($emails){ $emails = array_map(function($email){ return $email['email']; }, $emails); }
        }else if($rcp == 4){
            // send to users with loans
            $emails = $conn->query("select email from users where email != '' and status = '1' and coalesce((select count(id) from loans where uid = users.uid limit 1),0) > 0")->fetch_all(MYSQLI_ASSOC);
            if($emails){ $emails = array_map(function($email){ return $email['email']; }, $emails); }
        }else if($rcp == 5){
            // send to verified users
            $emails = $conn->query("select email from users where email != '' and status = '1' and ( coalesce((select count(id) from verifications where uid = users.uid and bvn_status = 1 and status = 1 limit 1),0) = 1 )")->fetch_all(MYSQLI_ASSOC);
            if($emails){ $emails = array_map(function($email){ return $email['email']; }, $emails); }
        }else if($rcp == 6){
            // send to failed verified users
            $emails = $conn->query("select email from users where email != '' and status = '1' and ( coalesce((select count(id) from verifications where uid = users.uid and (bvn_status = 2 or status = 2) limit 1),0) = 1 )")->fetch_all(MYSQLI_ASSOC);
            if($emails){ $emails = array_map(function($email){ return $email['email']; }, $emails); }
        }else if($rcp == 7){
            // send to pending verified users
            $emails = $conn->query("select email from users where email != '' and status = '1' and ( coalesce((select count(id) from verifications where uid = users.uid limit 1),0) = 0 )")->fetch_all(MYSQLI_ASSOC);
            if($emails){ $emails = array_map(function($email){ return $email['email']; }, $emails); }
        }else if($rcp == 8){
            // send to web users only
            $emails = $conn->query("select email from users where email != '' and status = '1' and ( coalesce((select count(id) from devices where uid = users.uid and type = 2 limit 1),0) != 0 ) and ( coalesce((select count(id) from devices where uid = users.uid and type != 2 limit 1),0) = 0 )")->fetch_all(MYSQLI_ASSOC);
            if($emails){ $emails = array_map(function($email){ return $email['email']; }, $emails); }
        }else{
            //others
            $emails = $rcps;
        }
    }
    
    print_r($sm);
    echo "<br />";
    print_r($emails);
    foreach($emails as $email){
        $ret = send("", $email, "admin_email",array("subject"=>$sm["subject"],"body"=>$sm["message"]));
    }
    if($ret){
        $status = 2;
    }else{
        $status = 4;
    }
    
    mysqli_query($conn, "update emails set status = '$status' where id = '".$sm['id']."' limit 1");
}
$conn->close();
?>