<?php
require_once('../../vendor/autoload.php');
require_once('../init.php');
$client = new \GuzzleHttp\Client();

define('API_BASE', 'https://api.myidentitypass.com');
// define('API_BASE', 'https://sandbox.myidentitypass.com');

function error_res($status, $response_code, $message)
{
    $response = new stdClass;
    $response->status = $status;
    $response->response_code = $response_code;
    $response->message = $message;
    return json_encode($response);
}

if (isset($_SESSION['user_id'])) {
    extract($_POST);
    if (isset($bvn) && !empty($bvn) && !empty($service_type)) {
 
        $user = User::find_by_id($_SESSION['user_id']);
        if ($user) {

            $service_type = Service::find_by_id($service_type);
            $sub_service = SubService::find_by_id($sub_service_id);

            if (!$service_type) {
                echo error_res(false, "03", "Service Not Avilable, Contact Administrator!");
                exit;
            }
            if (!$sub_service) {
                echo error_res(false, "03", "Service Not Avilable, Contact Administrator!");
            exit;
            }
        

            $service = $service_type->service_name;
            $service_fee = intval($service_type->service_fee);
            $status = 0;

            $my_wallet = Wallet::find_by_query("SELECT * FROM wallet WHERE user_id =" . $user->id . " LIMIT 1");
            $my_wallet = array_shift($my_wallet);
            $my_wallet_balance = intval($my_wallet->balance);

            if ($my_wallet_balance >= ($service_fee + intval($sub_service->service_fee))) {

                     $fields = [
             'bvn' => $bvn,
            
            ];
            
            $endpoint = "https://payyourbills.ng/api/verification/bvn";
            $fields_string = http_build_query($fields);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $endpoint);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
             'Authorization: Bearer sk_live_3b2ae7de76762603f1cd95a89ae43476ef2bb9285c249dba470f367c930f3133AV',
            
            )
            );
            $responses = curl_exec($ch);



                // $response_string = substr($responses, 98);
                
                // echo $string;

                $response_std = json_decode($responses);

                // print_r($response_std);
                // exit;
                
                // echo $response_std->status;


                $service_record = new ServiceRecord;
                $in_transaction = new InTransaction;

           
                   if ($response_std->response_code === "00") {
                    
                    $new_response_std = new stdClass;
                    $new_response_std->response_code = "00";
                    $new_response_std->detail = "Verification Successful";
                    $new_response_std->data = new stdClass;

                    $new_response_std->data->bvn = $response_std->data->bvn;
                    $new_response_std->data->firstName = $response_std->data->firstName;
                    $new_response_std->data->middleName = $response_std->data->middleName;
                    $new_response_std->data->lastName = $response_std->data->lastName;
                    $new_response_std->data->dateOfBirth = $response_std->data->dateOfBirth;
                    $new_response_std->data->registrationDate = $response_std->data->registrationDate;
                    $new_response_std->data->enrollmentBank = $response_std->data->enrollmentBank;
                    $new_response_std->data->enrollmentBranch = $response_std->data->enrollmentBranch;
                    $new_response_std->data->email = $response_std->data->email;
                    $new_response_std->data->gender = $response_std->data->gender;
                    $new_response_std->data->levelOfAccount = $response_std->data->levelOfAccount;
                    $new_response_std->data->lgaOfOrigin = $response_std->data->lgaOfOrigin;
                    $new_response_std->data->lgaOfResidence = $response_std->data->lgaOfResidence;
                    $new_response_std->data->maritalStatus = $response_std->data->maritalStatus;
                    $new_response_std->data->nin = $response_std->data->nin;
                    $new_response_std->data->nameOnCard = $response_std->data->nameOnCard;
                    $new_response_std->data->nationality = "Nigeria";
                    $new_response_std->data->phoneNumber1 = $response_std->data->phoneNumber1;
                    $new_response_std->data->phoneNumber2 = $response_std->data->phoneNumber2;
                    $new_response_std->data->residentialAddress = $response_std->data->residentialAddress;
                    $new_response_std->data->stateOfOrigin = $response_std->data->stateOfOrigin;
                    $new_response_std->data->stateOfResidence = $response_std->data->stateOfResidence;
                    $new_response_std->data->title = $response_std->data->title;
                    $new_response_std->data->watchListed = $response_std->data->watchListed;
                    $new_response_std->data->base64Image = $response_std->data->base64Image;
                    $new_response_std->data->number = $response_std->data->bvn;

                    // $response_std = $new_response_std;
                    
                    $status = 1;
                    $new_wallet_balance = $my_wallet_balance - ($service_fee + intval($sub_service->service_fee));

                    $my_wallet->balance = $new_wallet_balance;
                    $my_wallet->date_updated = $date;
                    $my_wallet->save();
                }else {
                    $service_fee = 40;
                    $new_wallet_balance = $my_wallet->balance - $service_fee;
                    
                    $my_wallet->balance = $new_wallet_balance;
                    $my_wallet->date_updated = $date;
                    $my_wallet->save();
                }
                



                $in_transaction->user_id = $user->id;
                $in_transaction->wallet_id = $my_wallet->id;
                $in_transaction->search_query = $bvn;
                $in_transaction->amount = $service_fee;
                // $in_transaction->service_id = intval($service_type);
                $in_transaction->remaining_balance = $new_wallet_balance;
                $in_transaction->service = $service;
                $in_transaction->date =  $date;
                $in_transaction->status = $status;

                if ($in_transaction->save()) {

                    $service_record->user_id = $user->id;
                    $service_record->service = $service;
                    $service_record->search_query = $bvn;
                    $service_record->response = json_encode($new_response_std);
                    $service_record->in_transaction_id = $in_transaction->id;
                    if ($service_record->save()) {
                        $response_std->record_id = $service_record->id;
                        $response_std->search_query = $bvn;

                        echo (json_encode($response_std));
                        exit;
                    } else {
                        echo error_res(false, "03", "Contact Administrator!");
                        exit;
                    }
                } else {
                    echo error_res(false, "03", "Contact Administrator!");
                    exit;
                }
            } else {
                echo error_res(false, "03", "Insufficient wallet balance");
                exit;
            }
        } else {

            echo error_res(false, "03", "Contact Administrator!");
            exit;
        }
    } else {

        echo error_res(false, "03", "Contact Administrator!");
        exit;
    }
} else {
    echo error_res(false, "03", "Contact Administrator!");
    exit;
}