<?php

class BpjsClient extends BackgroundBaseClient
{
    public function filterResponse($body, $result) {
        //NOTE: Logger harus diset dari controller, karena didapat dari DI.
        parent::filterResponse($body, $result);
        if($this->logger != null) {
            if (!empty($result)) {
                if (array_key_exists('metaData', $result) && !empty($result['metaData']))
                    $this->logger->addArray('api_result_', $result['metaData']);
                elseif (array_key_exists('metadata', $result) && !empty($result['metadata']))
                    $this->logger->addArray('api_result_', $result['metadata']);
            }
        }
    }

	function getSignature($timestamp)
	{			
		$secretKey = $this->prop["secret_key"];
		$consid = $this->prop["consid"];				
		$hash = hash_hmac("sha256", $consid . "&" . $timestamp, $secretKey, true);
		$hashInBase64 = base64_encode($hash);		
		
		return $hashInBase64;
	}

    public function getDetailByNIKAndTglLahir($nik, $tgl_lahir)
    {
		$timestamp = time();
		
        $method = "GET";
        $path = "/kpk-rest/nik/$nik/$tgl_lahir";
        $params = [
            "headers" => [
                "X-Cons-ID"   => $this->prop["consid"],
                "X-Timestamp" => $timestamp,
				"X-Signature" => $this->getSignature($timestamp)
            ]
        ];

        //region LOG Definition
        $this->setApiName("bpjs-get_detail_by_nik_and_tgl_lahir");
        $this->setLogParams([
            'nik' => $nik,
            'tgl_lahir' => $tgl_lahir,
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }
}
