<?php

namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;

class Promo extends BaseController
{
    use ResponseTrait;
    public function promoStart(){

        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
        $out =[];
        //$in["action"] = -1;

        $in['action'] = WRENCHBOARD_MARKET_PLAN01_NEXT1;
        $ret = $this->wrenchboard->wrenchboard_api($in, $out);
        $out['internal_return'] = $ret;
//        $in["promo"] = $promo;
//        $in["promo_owner"] = $promoOwner;

        log_message('critical', "Promo::promoStart ********* ALL   ".serialize($in) );
       // $in["loc"] = $_SERVER["REMOTE_ADDR"];
       // $in['limit']     = 10;
//        $out["firstname"] = "Firstname";
//        $out["promo_title"] = "This is the promo title";
//        $out["promo_description"] = "This is the promo description , This is the promo description ,This is the promo description ,This is the promo description ,This is the promo description ,This is the promo description ,This is the promo description ,";
        $out["site_name"]=  "WrenchBoard";
        $out["server_name"]= "https://www.wrenchboard.com";
        //$out['yyyy']     = '2024';
        //$out['user_server_name'] =  $this->getSiteConfigurations("system.server_name"); // 'https://dev-users.wrenchboard.com';

        //log_message('critical', "Promo::user_server_name **** user_server_name  ". $out["user_server_name"] );
        log_message('critical', "Promo::promoStart ********* OUT  ".serialize($out) );
        $in['action'] = -1; // we dont need reformating on this data
        return $this->respond( $this->summaryReturnData($in,$out), 200);
    }

    public function promoVerify(){

        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
        $out =[];
        //$in["action"] = -1;

        $in['action'] = WRENCHBOARD_MARKET_PLAN01_NEXT1;
        $ret = $this->wrenchboard->wrenchboard_api($in, $out);
        $out['internal_return'] = $ret;
//        $in["promo"] = $promo;
//        $in["promo_owner"] = $promoOwner;

        log_message('critical', "Promo::promoStart ********* ALL   ".serialize($in) );
        $in["loc"] = $_SERVER["REMOTE_ADDR"];
        $in['limit']     = 10;
//        $out["firstname"] = "Firstname";
//        $out["promo_title"] = "This is the promo title";
//        $out["promo_description"] = "This is the promo description , This is the promo description ,This is the promo description ,This is the promo description ,This is the promo description ,This is the promo description ,This is the promo description ,";
        $out["site_name"]=  "WrenchBoard";
       // $out["server_name"]= "https://www.wrenchboard.com";
        $out['yyyy']     = '2024';
      //  $out['user_server_name'] =  'https://dev-users.wrenchboard.com';

        log_message('critical', "***** ***** Promo::promoVerify Ret ");
        return $this->respond( $this->summaryReturnData($in,$out), 200);
    }
    public function promoTask(){

        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
        $in["action"] = -1; // bad number - we dont want formating WRENCHBOARD_ACCOUNT_JOBLIST; dont send this line , the formater will be confused
        $out=[];
        $endpoint = "PROMO_JOBS-". $in["promo"];

        $out = $this->getCache($endpoint);
        $countItem  = 0;
        if( is_array($out)){
            $countItem = count($out);
        }

        if (  $countItem == 0 ){
            $local_url = "http://".$this->micro_service_net1.":3033/promojobs";
            $out = $this->APIcall('GET', $local_url, $in);
            // dont cache junk
            if ( is_array($out) && is_array($out["result_list"]) && count($out["result_list"]) > 0){
                $this->saveCache($endpoint,$out,3000);
            }
            $out['internal_return'] = 0; // just backwad comaptobility
            log_message('critical', "***** ***** Promo::getJobsData Cache Done:::Ret ");
        }

        $endpoint = "WRENCH_JOB_INTEREST_COUNT";
        $int_list  =  $this->getCache($endpoint)["result_list"];
        $out["interest_list"] = ( isset($int_list) && is_array($int_list) ) ? $int_list : [];

        log_message('critical', "***** ***** Promo::getJobsData Ret ");
        return $this->respond( $this->summaryReturnData($in,$out), 200);
    }

    public function promoAuth(){
        log_message('critical', "***** ***** promoAuth::userLogin ****" );
        //  $in = $this->request->getPostGet();
        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);

        $in["action"] = WRENCHBOARD_PROMOADMIN_LOGIN;
        if (!isset($in["login_mode"])){
            $in["login_mode"] = MOBILE_LOGIN;
        }


        // make safe for redis
        $outF["username"] = str_replace("@", "_", $in["username"]);
        $outF["username"] = str_replace(".", "_", $outF["username"]);
        $outF["original_username"] =  $in["username"];
        $outF["loc"] = $in["loc"];
        $fail_endpoint = "PROMO-LOGIN_FAILED-".$outF["username"]."-";
        //===================REDIS DATA IN CASE OF FAIL
        $out_redis = $this->getCache($fail_endpoint); // try find in cache
        if (isset($out_redis["fail_count"]) && $out_redis["fail_count"] > 2){
            // we need stop here
            log_message('critical', "Danger ***** ***** promoAuth::userLogin FAIL = ".$out_redis["fail_count"] );
        }else{
            $outF["fail_count"] = isset(  $out_redis["fail_count"] ) ? $out_redis["fail_count"]+1:1;
        }
        $out=[];

        $ret = $this->wrenchboard->wrenchboard_api($in, $out);
        $out['internal_return'] = $ret;
        log_message('critical', "***** ***** promoAuth::userLogin Ret = ".$ret );
        if ( $out['internal_return'] ==  100 ){
            $endpoint = "PROMO-USER_SESSION-". str_pad($out["member_id"], 12, "0", STR_PAD_LEFT) ."-";
            log_message('critical', "***** ***** promoAuth::promoAuth USER_SESSION = ".$endpoint );
            $this->saveCache($endpoint,$out,15000);
        }else
        {
            log_message('critical', "***** ***** WrenchAuth::userLogin USER_SESSION = ".$fail_endpoint );
            $this->saveCache($fail_endpoint,$outF,15000);
        }

        return $this->respond( $this->summaryReturnData($in,$out), 200);
    }
    public function promoList(){
        log_message('critical', "***** ***** Promo::promoList ****" );
        //  $in = $this->request->getPostGet();
        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
        $out = [];

        return $this->respond( $this->summaryReturnData($in,$out), 200);
    }
    public function promoAddRef(){
        log_message('critical', "***** ***** Promo::promoAddRef ****" );
        //  $in = $this->request->getPostGet();
        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
        $out = [];

        return $this->respond( $this->summaryReturnData($in,$out), 200);
    }

}