<?php
function api_add_new_customer($dbc,$phoneNum,$pin,$fullName,$accountType){
    $countryCode = 'gh';
    $kyc = 2;
    $q = mysqli_query($dbc,"INSERT INTO customer_tbl (id,phoneNum,ussdPasscode,registeredPhoneType,countryCode,fullName,accountType,accountKYC)
      VALUES (UUID(),'$phoneNum','$pin','USSD','$countryCode','$fullName','$accountType','$kyc')
    ");

    if($q){
        $qq = mysqli_query($dbc,"SELECT id FROM customer_tbl WHERE phoneNum = '$phoneNum'");
        $data = mysqli_fetch_assoc($qq);

        if(create_new_account($dbc,$data['id'])){
            $message = "Thank's {$fullName} for joining PayGlobal, Your pin is {$pin}.";
            sendSMS($phoneNum,$message);
            return true;
        }
    }
}

function api_add_funds($dbc,$phoneNum,$balance){
    $customerData = get_customer_by_phoneNum($dbc,$phoneNum);
    if(add_funds($dbc,$customerData['id'],$balance)){
        return true;
    }
}

function api_genPIN($length = 4){
    $characters = '0123456789';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

function api_update_pin($dbc,$customerData,$pin){
    $q = mysqli_query($dbc,"UPDATE customer_tbl SET ussdPasscode = '$pin' WHERE id = '$customerData[id]'");
    if($q){
        return true;
    }
}


function api_get_single_transaction($dbc,$phoneNum,$transaction_uid,$mPhone){
    $customerData = get_customer_by_phoneNum($dbc,$phoneNum);
    $recipientData = get_customer_by_phoneNum($dbc,$mPhone);
    $q = mysqli_query($dbc,"SELECT * FROM maintransaction_tbl WHERE recipientID = '$recipientData[id]' AND senderID = '$customerData[id]' AND transaction_uid = '$transaction_uid' AND transactionStatus = 'COMPLETED'");
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function api_check_header_auth($dbc,$pKey,$mPhone){
    $q = mysqli_query($dbc,"SELECT id FROM web_api_connection_tbl WHERE phoneNum = '$mPhone' AND pKay = '$pKey' AND status = 'ACTIVE' ");
    if(mysqli_num_rows($q) > 0){
        return true;
    }
}