<?php

namespace App\Jobs;

use App\Tools\SmsTools;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Mail;

class SendNewRegistrationNotification implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $client;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(User $client)
    {
        $this->client = $client;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $user = $this->client;
        try {
            //prepare data to pass to email view
            $data = [
                'username' => $user->username,
                'password' => $user->pass,
                'full_name' => $user->full_name
            ];
            //send email notification to client
            Mail::send('emails.registration_msg', $data, function ($m) use ($user) {
                $m->from('npontualph@gmail.com', 'MESSENGER');
                $m->replyTo('support@npontu.com', 'Npontu Support');
                $m->to($user->email, $user->full_name);
                $m->subject('Welcome to MESSENGER!');
            });

            $data = [
                'full_name' => $user->full_name,
                'phone_number' => $user->phone_number,
                'username' => $user->username,
                'email' => $user->email,
                'organisation' => $user->organisation,
                'location' => $user->location
            ];

            Mail::send('emails.new_client_registered', $data, function ($m) {
                $m_email = 'support@npontu.com';
                $m->from($m_email, 'Npontu SMS');
                $m->to('support@npontu.com', 'Npontu Support Team');
                $m->cc('Josephine.asante@mtn.com', 'Josephine Asante');
                $m->cc('Maame.simpson@mtn.com', 'Maame Simpson');
                $m->subject('New registration on the MESSENGER SMS Platform');
            });

        } catch (\Throwable $ex) {
        }

        // send sms to client
        try {
            $msg = "Hello {$user->full_name}, 
Your account with MTN MTN MESSENGER has been successfully created, pending approval within the next 24hrs. 
Your username is: {$user->username} and your password is: {$user->pass}. Thank you";



            SmsTools::send_internal_sms($user->phone_number, $msg);
        } catch (\Throwable $ex) {
        }

        // send sms to support/debby
        try {
            $msg = "Hello Support, 
A new client has been registered on the SMS platform, kindly find below his details: 
Name: {$user->full_name}
Phone Number: {$user->phone_number}
Username: {$user->username}
Email: {$user->email}";

            SmsTools::send_internal_sms('233243375520', $msg);
            SmsTools::send_internal_sms('233246567599', $msg);
        } catch (\Throwable $ex) {
        }

    }
}