<?php

namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;

class WrenchUser extends BaseController
{
    use ResponseTrait;

    public function UsersFiles(){
        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
        $in["action"] =  WRENCHBOARD_MYFILES_LIST;
        $out=[];
        $endpoint = "UPLOADS-USER-". str_replace("-", "_", $in["uid"]);;

        $out = $this->getCache($endpoint);
        if ( count($out)==0 ){
            $ret = $this->wrenchboard->wrenchboard_api($in, $out);
            $this->saveCache($endpoint,$out,15000);
            $out['internal_return'] = $ret;
            log_message('critical', "***** ***** WrenchUser::UsersFiles Cache Done:::Ret ". $ret);
        }
        log_message('critical', "***** ***** WrenchUser::UsersFiles Ret ");
        return $this->respond( $this->summaryReturnData($in,$out), 200);
    }

    public function UsersRefLink(){
        //const WRENCHBOARD_MYREFLINK = 11310;
        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
        $in["action"] =  WRENCHBOARD_MYREFLINK;
        $out=[];

        $in["uid_part"] =  substr(trim($in["refer_link"]), 0, 8);
        $in["member_id"] = (int) substr(trim($in["refer_link"]), 9);
        log_message('critical', "***** ***** WrenchUser::UsersRefLink uid_part  = ".$in["uid_part"]);
        log_message('critical', "***** ***** WrenchUser::UsersRefLink member_id = ". $in["member_id"]);

        if ( $in["uid_part"] !='' && $in["member_id"] > 0 ){
            log_message('critical', "***** ***** WrenchUser::UsersRefLink IN Call Backend ");
            $out["refer_link"] = $this->user_server_name."/myrefer/".$out["refer_link"];
            $ret = $this->wrenchboard->wrenchboard_api($in, $out);
        }

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

