<?php

if (!function_exists('sendSMS')){

    /**
     * Send SMS
     * @return mixed
     */
    function sendSms(string $to, string $message)
    {
        $to = validatePhone($to);
        if(!$to){
            return false;
        }
        // dump($to, $message);
        $apiKey= env('SMS_API_KEY', null);
        $sender= env('SMS_SENDER', null);
        // $message= 'test';
        // $message= str_replace(' ', '%20', $message);
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://api.oursms.com/msgs/sms',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS =>'{
              "src": "'.$sender.'",
              "dests": ["'.$to.'"],
              "body": "'.$message.'",
              "priority": 0,
              "delay": 0,
              "validity": 0,
              "maxParts": 0,
              "dlr": 0,
              "prevDups": 0,
              "msgClass": "promotional"
          }',
            CURLOPT_HTTPHEADER => array(
              'Authorization: Bearer '.$apiKey,
              'Content-Type: application/json'
            ),
          ));
          
          $response = curl_exec($curl);
        // echo $response;exit;
        // dump(curl_getinfo($curl));
        // echo curl_errno($curl) . '<br/>';
        // echo curl_error($curl) . '<br/>';
        // curl_close($curl);
        // dd($response);
        return $response;
    }
    
}
if (!function_exists('sendSmsMSGAPI')){

    /**
     * Send SMS
     * @return mixed
     */
    function sendSmsMSGAPI(string $to, string $message)
    {
        $to = validatePhone($to);
        if(!$to){
            return false;
        }
        // dd($to, $message);
        $username= env('SMS_USERNAME', null);
        $apiKey= env('SMS_API_KEY', null);
        $sender= env('SMS_SENDER', null);
        // $message= 'test';
        // $message= str_replace(' ', '%20', $message);
        $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://www.msegat.com/gw/sendsms.php',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS =>'{
            "userName": "'.$username.'",
            "numbers": "'.$to.'",
            "userSender": "'.$sender.'",
            "apiKey": "'.$apiKey.'",
            "msg": "'.$message.'"
        }',
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json',
            'Cookie: userLang=Ar; SERVERID=MBE1; userCurrency=SAR'
        ),
        ));

        $response = curl_exec($curl);
        // echo $response;exit;
        // dump(curl_getinfo($curl));
        // echo curl_errno($curl) . '<br/>';
        // echo curl_error($curl) . '<br/>';
        curl_close($curl);
        // dd($response);
        return $response;
    }
}
    
function validatePhone (string $phone= null): ?string
{
    $re = config('globals.phone_regex');
    // dd($re);
    preg_match($re, $phone, $matches);
    // dd($matches);
    if($matches && $matches[2]) {
        return config('globals.phone_code').$matches[2];
    }
    return null;
    
    
}
?>
