<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
  use AuthorizesRequests, DispatchesJobs, ValidatesRequests;


  public function log_query() {
    // , $binding, $timing 'bindings' => $binding)
    \DB::listen(function ($sql) {
        \Log::info('Showing query', array('sql' => $sql));
        //$encoded_sql = json_encode($sql);
        //$this->sendNtfy("Showing Query : " . $encoded_sql);
        }
      );
  }

  public function validatePhoneNumber($phone){
    //265998109217
    //265881161942
    //265881161942
    //0881161942
    $phone = str_replace(' ', '', $phone);
    $phone = str_replace('+', '', $phone);
    $pattern = "/^\+?(265|0)?[89]\d{8}$/i";
    $retval = preg_match($pattern, $phone, $matches, PREG_OFFSET_CAPTURE);
    if (count($matches) < 1) {
      return false;
    }
    elseif (strlen($phone) == 9) {
      $msisdn = "265" . $phone;
    }
    elseif (strlen($phone) == 10) {
      $phone = ltrim($phone, 0);
      $msisdn = "265" . $phone;
    }
    elseif (strlen($phone) == 12) {
      $msisdn = $phone;
    }
    if (!isset($msisdn)) {
      return FALSE;
    }

    $prefix = substr($msisdn, 0, 4);
    
    if ($prefix == '2658') {
      $network = 'tnm';
    }
    elseif ($prefix == '2659' ) {
      $network = 'airtel';
    }
    else{
      return FALSE;
    }
    return $msisdn;
  }
  public function sendNtfy($data){
    $url = 'https://ntfy.sh/airtimeSalesPortal';
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json'
      ),
      CURLOPT_URL => $url,
      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 => $data
    ));
    $response = curl_exec($curl);
    return $response;

  }
  public function ClickADPHttp($params){
      //$api_email, $api_token
      $token = "54efd476-8d6e-483b-8b2a-c7904a8b4a6b";
      $email = "srwb.airtime@clickairtime.com";
      $X_Click_Airtime_Token =  "54efd476-8d6e-483b-8b2a-c7904a8b4a6b";
      $X_Click_Airtime_Email = "reseller@clickairtime.com";
      $curl = curl_init();


      curl_setopt_array($curl, array(
      CURLOPT_URL => "https://api.clickairtime.com/adp",// "http://localhost:4646/api/request",
      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 => json_encode($params),
      CURLOPT_HTTPHEADER => array(
        "X-Click-Airtime-Email: " . $X_Click_Airtime_Email,
        "X-Click-Airtime-Token: " . $X_Click_Airtime_Token,
        "Content-Type: application/json"
      ),
    ));

    $response = curl_exec($curl);
    \Log::info(curl_getinfo($curl));

    curl_close($curl);
    return $response;
  }
}
