<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Twilio\Rest\Client;


class ProcessSendSMS implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $phoneNumber;
    protected $messageContent;
    protected $twilioSecretID;
    protected $twilioToken;
    protected $twilioAccountNumber;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($phoneNumber, $messageContent)
    {
        $this->phoneNumber = $phoneNumber;
        $this->messageContent  = $messageContent;
        
        $this->twilioSecretID = env('TWILIO_SECRET_ID');
        $this->twilioToken = env('TWILIO_TOKEN');
        $this->twilioAccountNumber = env('TWILIO_PHONE_NUMBER');
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->sendMessage($this->phoneNumber, $this->messageContent);
    }

    public function sendMessage($phoneNumber, $messageContent)
    {
        $client = new Client($this->twilioSecretID, $this->twilioToken);

        $client->messages->create(
            $phoneNumber,
            array (
                'from' => $this->twilioAccountNumber, /* Twilio acct number */
                'body' => $messageContent
            )
        );
    }
}
