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


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($vnin) && isset($nin) && !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))) {


                try {

                    $response = $client->request('POST', 'https://api.verified.africa/sfx-verify/v3/id-service/', [
                        'body' => '{"searchParameter":"' . $vnin . '","verificationType":"V-NIN","countryCode":"NG"}',
                        'headers' => [
                            'accept' => 'application/json',
                            'apiKey' => 'CvOCgUf68YOHTwT9jHdK',
                            'content-type' => 'application/json',
                            'userid' => '1662732332645',
                        ],
                    ]);

                    $responses = $response->getBody();
                    // echo ($responses);
                    // return false;
                } catch (\Throwable $th) {

                    print_r($th->getMessage());
                    echo error_res(false, "03", "Network Error!");
                    exit;
                }





                // {
                //     "responseCode": "00",
                //     "description": "Successful",
                //     "verificationType": "V-NIN",
                //     "verificationStatus": "VERIFIED",
                //     "transactionStatus": "SUCCESSFUL",
                //     "transactionReference": "1628022119761-LFS5XUQF",
                //     "transactionDate": "1680002214947",
                //     "searchParameter": "SF895332826955L0",
                //     "callBackUrl": null,
                //     "livenessScore": 0,
                //     "paymentRef": null,
                //     "response": {
                //         "firstName": "PROUD",
                //         "surname": "CITIZEN",
                //         "middleName": "NIGERIAN",
                //         "dateOfBirth": "01 OCT 1960",
                //         "userid": "ABCDEF-8910",
                //         "gender": "M",
                //         "trustedNumber": "2341234567890",
                //         "txid": "bd633743-cdb8-452f-b46b-55c63c81a605",
                //         "ts": "2023-03-28T11:16:54.947",
                //         "agentID": "ABCDEF-8910",
                //         "photo": "/9j/4AAQSkZJRgABAQAASABIAAD",
                //         "vnin": "SF895332826955L0",
                //         "vNIN": "SF895332826955L0"
                //     },
                //     "faceMatch": null
                // }


                $response_std = json_decode($responses);
                // print_r($response_std);



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



                $new_response_std = new stdClass;

                if ($response_std->responseCode == 00 && $response_std->verificationStatus == 'VERIFIED') {
                    $status = 1;

                    $new_response_std->response_code = "00";
                    $new_response_std->detail = "Verification Successful";
                    $new_response_std->nin_data = new stdClass;

                    $new_response_std->nin_data->birthdate = $response_std->response->dateOfBirth;

                    $new_response_std->nin_data->email = '';
                    $new_response_std->nin_data->emplymentstatus = "";
                    $new_response_std->nin_data->firstname = $response_std->response->firstName;
                    $new_response_std->nin_data->middlename = $response_std->response->middleName;
                    $new_response_std->nin_data->gender = $response_std->response->gender;
                    $new_response_std->nin_data->txid = $response_std->response->txid;
                    $new_response_std->nin_data->agentID = $response_std->response->agentID;
                    $new_response_std->nin_data->ts = $response_std->response->ts;
                    $new_response_std->nin_data->heigth = "";
                    $new_response_std->nin_data->maritalstatus = 'Single';
                    $new_response_std->nin_data->nin = $nin;
                    $new_response_std->nin_data->trackingId = '';
                    $new_response_std->nin_data->nok_address1 = "";
                    $new_response_std->nin_data->nok_address2 = "";
                    $new_response_std->nin_data->nok_firstname = "";
                    $new_response_std->nin_data->nok_lga = "";
                    $new_response_std->nin_data->nok_middlename = "";
                    $new_response_std->nin_data->nok_postalcode = "";
                    $new_response_std->nin_data->nok_state = "";
                    $new_response_std->nin_data->nok_surname = "";
                    $new_response_std->nin_data->nok_town = "";
                    $new_response_std->nin_data->nspokenlang = "";
                    $new_response_std->nin_data->ospokenlang = "";
                    $new_response_std->nin_data->pfirstname = "";
                    $new_response_std->nin_data->photo = $response_std->response->photo;
                    $new_response_std->nin_data->pmiddlename = "";
                    $new_response_std->nin_data->profession = "";
                    $new_response_std->nin_data->psurname = "";
                    $new_response_std->nin_data->religion = '';
                    $new_response_std->nin_data->residence_address = '';
                    $new_response_std->nin_data->residence_town = '';
                    $new_response_std->nin_data->residence_lga = '';
                    $new_response_std->nin_data->residence_state = '';
                    $new_response_std->nin_data->residencestatus = '';
                    $new_response_std->nin_data->self_origin_lga = "";
                    $new_response_std->nin_data->self_origin_place = "";
                    $new_response_std->nin_data->self_origin_state = "";
                    $new_response_std->nin_data->signature = '';
                    $new_response_std->nin_data->surname = $response_std->response->surname;
                    $new_response_std->nin_data->telephoneno = '';
                    $new_response_std->nin_data->title = "";
                    $new_response_std->nin_data->vnin = "";
                    $new_response_std->nin_data->userid = "";

                    $new_response_std->verification = new stdClass;
                    $new_response_std->verification->status = '';
                    $new_response_std->verification->reference = "";

                    // print_r($new_response_std);
                    // return false;

                    $new_wallet_balance = $my_wallet_balance - $service_fee;
                    $my_wallet->balance = $new_wallet_balance;
                    $my_wallet->date_updated = $date;
                    $my_wallet->save();
                } else if ($response_std->responseCode == 00 && $response_std->description == 'The ID provided is invalid') {

                    $in_transaction->user_id = $user->id;
                    $in_transaction->wallet_id = $my_wallet->id;
                    $in_transaction->search_query = $vnin;
                    $in_transaction->amount = $service_fee;
                    $in_transaction->remaining_balance = $new_wallet_balance;
                    // $in_transaction->service = $service;
                    $in_transaction->service = $service_type->service_name;
                    $in_transaction->date = $date;
                    $in_transaction->status = $status;

                    $in_transaction->save();


                    $new_response_std->response_code = "03";
                    $new_response_std->message = "The ID provided is invalid";
                    $service_fee = 50;
                    $status = 0;
                    // $new_wallet_balance = $my_wallet_balance - $service_fee;
                    $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 {
                    echo error_res(false, "01", $response_std->description);
                    exit;

                    //     $new_wallet_balance = $my_wallet->balance;
                    //     $service_fee = 0;
                }




                $in_transaction->user_id = $user->id;
                $in_transaction->wallet_id = $my_wallet->id;
                $in_transaction->search_query = $vnin;
                $in_transaction->amount = $service_fee;
                $in_transaction->remaining_balance = $new_wallet_balance;
                // $in_transaction->service = $service;
                $in_transaction->service = $service_type->service_name;
                $in_transaction->date = $date;
                $in_transaction->status = $status;

                if ($in_transaction->save()) {

                    $service_record->user_id = $user->id;
                    // $service_record->service = $service;
                    $service_record->service = $service_type->service_name;
                    $service_record->search_query = $vnin;
                    $service_record->response = json_encode($new_response_std);
                    $service_record->in_transaction_id = $in_transaction->id;
                    if ($service_record->save()) {
                        $new_response_std->record_id = $service_record->id;

                        $new_response_std->search_query = $nin;
                        $new_response_std->record_id = $service_record->id;

                        echo (json_encode($new_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;
}