<?php

namespace App\Listeners\Backend;

use App\Events\Backend\ContactCreated;
use App\Models\UserManagement\Admin;
use App\Models\UserManagement\User;
use App\Notifications\Backend\ContactCreatedNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class NotifyAfterContactCreated{

    /**The time (seconds) before the job should be processed.
     * @var int
     */
    public $delay = 60;

    public $afterCommit = true;


    public function __construct(){
        //
    }
    /** Get the name of the listener's queue.
     * @return string
     */
    public function viaQueue(){
        return 'listener-contact-created';
    }

    /**Determine si l'ecouteur doit être mis en fil d'attente
     * @param ContactCreated $event
     * @return bool
     */
    public function shouldQueue(ContactCreated $event){
        return !blank($event->contact->email) ;
    }

    /**
     * Handle the event.
     *
     * @param ContactCreated $event
     * @return void
     */
    public function handle(ContactCreated $event){

        // Notification to admins.
        Admin::get()
             ->map
            ->notify(new ContactCreatedNotification($event->contact));

    }
}
