<?php

namespace App\Controllers;
use CodeIgniter\API\ResponseTrait;

class WrenchHooks extends BaseController
{

//    protected $db;
//    public $con_name = 'wrench_blog';
//    use ResponseTrait;
//    protected $request;
    public function __construct()
    {
       // $this->request = $request = \Config\Services::request();
    }

    public function index()
    {
        $envID = getenv('ENV_ID');
    }


    public function stripes(){

        log_message('critical', "WrenchHooks stripes");

    }
    public function flutter(){
        $enable_event_publish =  true;
        $microPort = "3037"; // wallet service port

        $raw_json = file_get_contents('php://input');
        $raw_array = json_decode($raw_json, true);
        $in = $raw_array;
        try {
            $this->saveCache("FLUTTER_HOOK-".strtoupper( date("FDYjGis") ),$in,30000); // save flutter hook response to redis
        } catch (Exception $e) {
            log_message('critical', "WrenchHooks flutter fail => ".$e->getMessage());
        }

        try {
            if ( $enable_event_publish ==  true  && !empty($in)  ){
                $eventMsg = [
                    "message" => serialize($in)
                ];

                $local_url = "http://".$this->micro_service_net1.":".$microPort."/flutterOkHook";
                $out = $this->APIcall('POST', $local_url, $eventMsg);  //kafKa Lane
            }
        } catch (Exception $e) {
            log_message('critical', "WrenchHooks flutter Kafka fail => ".$e->getMessage());
        }

        if ( isset( $in["event"]) & isset($in["data"]) ){
            $inx = $in["data"];
            $inx["txRef"] = $inx["tx_ref"];
            $inx["orderRef"] = $inx["flw_ref"];
            $inx["flwRef"] = $inx["flw_ref"];
            $inx["appfee"] = $inx["app_fee"];
            $inx["customer"]["AccountId"] =$inx["customer"]["id"];
            $inx["customer"]["fullName"] = $inx["customer"]["name"];
        }
        else{
            $inx =$in;
        }

        try {
            log_message('critical', "WrenchHooks flutter");
            log_message('critical', "WrenchHooks ***************************** flutter");
            log_message('critical', "WrenchHooks-CALL PREPARE DATA".serialize($in) );
            log_message('critical', "WrenchHooks-CALL ********** RETURN *************** DATA".$inx["event.type"] );
            log_message('critical', "WrenchHooks-CALL ********** RETURN *************** DATA".$inx["status"] );
            log_message('critical', "WrenchHooks-CALL ********** RETURN *************** DATA".$inx["txRef"] );
            log_message('critical', "WrenchHooks-CALL ********** RETURN *************** DATA".$inx["flwRef"] );
            log_message('critical', "WrenchHooks-CALL ********** RETURN *************** DATA".$inx["orderRef"] );
            log_message('critical', "WrenchHooks-CALL ********** RETURN *************** DATA".$inx["amount"] );
            log_message('critical', "WrenchHooks-CALL ********** RETURN *************** DATA".$inx["appfee"] );
            log_message('critical', "WrenchHooks-CALL ********** RETURN *************** DATA".$inx["customer"]["id"] );
            log_message('critical', "WrenchHooks-CALL ********** RETURN *************** DATA".$inx["customer"]["AccountId"] );
            log_message('critical', "WrenchHooks-CALL ********** RETURN *************** DATA".$inx["customer"]["fullName"] );
        } catch (Exception $e) {
            log_message('critical', "WrenchHooks flutter fail => ".$e->getMessage());
        }

        $data = [
            "event_type" =>  $inx["event.type"],
            "status" =>  $inx["status"],
            "txRef" =>  $inx["txRef"],
            "flwRef" =>  $inx["orderRef"],
            "orderRef" => $inx["orderRef"]  ,
            "amount" => $inx["amount"]*100 ,
            "appfee" => $inx["appfee"]*100,
            "customer_id" =>  $inx["customer"]["id"],
            "AccountId" =>  $inx["customer"]["AccountId"],
            "fullName" =>  $inx["customer"]["fullName"],
            "action" => WRECNH_CREDIT_HOOKS_FULUTTER,
            "raw_result" =>  serialize($in),
        ];

        if ( $data["action"] !='' ){
            $ret = $this->wrenchboard->wrenchboard_api($data, $out);
            $out['internal_return'] = $ret;
        }
    }
}

/*

CREATE TABLE flutterwave_hooks (
	id SERIAL,
	uid  uuid  DEFAULT uuid_generate_v4(),
	event_type VARCHAR(25),
	status VARCHAR(25),
	txRef VARCHAR(50),
	flwRef VARCHAR(50),
	orderRef VARCHAR(50),
	amount INT DEFAULT 0,
	appfee INT DEFAULT 0,
	customer_id VARCHAR(25),
	AccountId VARCHAR(25),
	fullName VARCHAR(50),
	raw_result TEXT,
	process_status INT DEFAULT 1,
    added timestamp without time zone DEFAULT now(),
    updated timestamp without time zone DEFAULT now()
	);
 ALTER TABLE ONLY flutterwave_hooks
    ADD CONSTRAINT flutterwave_hooks_id_key UNIQUE (id);


 *{
	s:2:"id";i:4490321;
	s:5:"txRef";s:13:"1690507667625";
	s:6:"flwRef";s:41:"FLW-MOCK-8705ee76a3efc2ae124c783f79f8e681";
	s:8:"orderRef";s:25:"URF_1690507780219_8700835";
	s:11:"paymentPlan";N;
	s:11:"paymentPage";N;
	s:9:"createdAt";s:24:"2023-07-28T01:29:40.000Z";
	s:6:"amount";i:555;
	s:14:"charged_amount";i:555;
	s:6:"status";s:10:"successful";
	s:2:"IP";s:12:"54.75.161.64";
	s:8:"currency";s:3:"NGN";
	s:6:"appfee";d:7.77;
	s:11:"merchantfee";i:0;
	s:16:"merchantbearsfee";i:1;
	s:8:"customer";a:9:
	{
		s:2:"id";i:2158727;
		s:5:"phone";N;
		s:8:"fullName";s:15:"Last11 Olusesan";
		s:13:"customertoken";N;
		s:5:"email";s:20:"ses66181+1@gmail.com";
		s:9:"createdAt";s:24:"2023-07-28T01:29:40.000Z";
		s:9:"updatedAt";s:24:"2023-07-28T01:29:40.000Z";
		s:9:"deletedAt";N;
		s:9:"AccountId";i:1017511;
	}
	s:6:"entity";a:4:
	{
		s:5:"card6";s:6:"553188";
		s:10:"card_last4";s:4:"2950";
		s:16:"card_country_iso";s:2:"NG";
		s:9:"createdAt";s:24:"2020-04-24T15:19:22.000Z";
	}
	s:10:"event.type"; s:16:"CARD_TRANSACTION";
}
 */
