<?php

namespace App\Listeners;

use GuzzleHttp\Client;
use App\Events\SmsCreatedEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class SmsSentListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle(SmsCreatedEvent $event)
    {
        //
        $phone_no = EF::fillterPhoneNo($event->phone_no);

        if(strlen(trim($phone_no)) == "14") // 00923031234567
        {

            // SMS .ENV VERIFICATION AND PHONE LIST VERIFICATION IN FLAGS
            if(config('app.app_sms') && strlen(trim(config('app.app_sms_key'))) > 0)
            {
                $client = new Client();

                $query = '{"from":"SKYLITE","to":"'.$phone_no.'","text":"'.$event->message.'"}';

                $response = $client->post('http://107.20.199.106/restapi/sms/1/text/single', [
                  'body' => $query,
                  'headers' => [
                    'accept' => 'application/json',
                    'content-type' => 'application/json',
                     'authorization' => config('app.app_sms_key')
                  ]
                ]);
            }

        }
    }
}
