<?php

namespace App\Controllers;

class WrenchUserContacts extends BaseController
{

    /*
     CREATE TABLE members_contacts_import (
		id SERIAL,
		uid uuid DEFAULT  uuid_generate_v4(),
		member_id INT REFERENCES members(id) NOT NULL,
		member_uid  VARCHAR(150) NOT NULL,
		raw_contacts  TEXT,
		status INT DEFAULT 0,
		added timestamp without time zone DEFAULT now(),
		updated timestamp without time zone DEFAULT now()
	);
 ALTER TABLE ONLY members_contacts_import
    ADD CONSTRAINT members_contacts_import_id_key UNIQUE (id);

     *   this.usrData = {action:22010,
          member_id: this.sessionDataProviderService.member_id,
          uid: this.sessionDataProviderService.member_uid,
          sessionid: this.sessionDataProviderService.session ,
          raw_contacts: contactResult }
     */
    public function usersContacts(){
   //     log_message('critical', "*********** WrenchUserContacts::usersContacts()   0000   ");
        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
        $out =[];
      //  log_message('critical', "*********** WrenchUserContacts::usersContacts()   0001   ".serialize($in));
        $in["action"] =  WRENCHBOARD_ACCOUNT_CONTACTIMPORT;

        if (isset($in["uid"]) && $in["uid"] !='' ){
            $in["member_uid"]=$in["uid"];
        }
        $member_uid  = str_replace("-", "_", $in["uid"]);
        $time_to_save = false;
        $endpoint = "CONTACTS_IMPORT_ACTION-". $member_uid;
        $outC = $this->getCache($endpoint); // try find in cache

        if (isset($outC) && is_array($outC) && isset($outC['raw_contacts'])){
            log_message('critical', "************* WrenchUserContacts REFRESH   0002   ".serialize($in));
            $outC["raw_contacts_". $in["mmode"]] = $in["raw_contacts"];  // extra store raw
            $this->deleteCache($endpoint);
            $this->saveCache($endpoint,$outC,5300);
        }
        else
        {
          //  $this->deleteCache($endpoint);
            log_message('critical', "************* WrenchUserContacts CREATE   0001   ".serialize($in));
            $in["raw_contacts_". $in["mmode"]] = $in["raw_contacts"];  // extra store raw
            $this->saveCache($endpoint,$in,5300);
        }



        if ($in["csize"] + $in["mmode"] >= $in["pcount"]){
            $time_to_save = true;
        }
        ///=== cache game gere

        if($time_to_save){
            $in["raw_contacts"] =  serialize($in['raw_contacts']);
//            $ret = $this->wrenchboard->wrenchboard_api($in, $out);
//            $out['internal_return'] = $ret;
        }
        else{
         $out["status_msg"] = "Received and cached";
        }
       // log_message('critical', "*********** WrenchUserContacts::usersContacts() 0001   ".serialize($out) );
        return  $this->summaryReturnData($in,$out); //json_encode( $final_out );
    }

    private function processContacts( $in ){
        log_message('critical', "************************ cachecontacts   0001   ".serialize($in));

        $inx =[];
        $inx["member_id"] = $in["member_id"];
        $inx["uid"] = $in["uid"];
        $inx["sessionid"] = $in["sessionid"];
        $raw_contacts = $in["raw_contacts"];

        foreach ( $raw_contacts["contacts"] as $rows){
            $firstname =  $rows["name"]["given"];
            $lastname =  $rows["name"]["family"];
            $contactid =  $rows["contactId"];
            log_message('critical', "************************ $firstname   $lastname   $contactid");
        }
    }



}