<?php

namespace App\Listeners\Company;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Events\Company\LoanBillingNotificationEvent;
use App\Models\Notifications;
use App\Events\Company\PusherNotificationEvent as CompanyNotificationEvent;

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

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle(LoanBillingNotificationEvent $event)
    {
        $notificationData = $event->notificationData;
        $purpose = $notificationData->purpose;

        if($purpose == 'resolve-modification'){

            //notification intended for loaner
            $message = "A company has resolved the request for modification on the billing information of <b>" . $notificationData->full_name . "</b>.";
            
            $notificationData = array(
                'type' => 'Borrow Invoice',
                'company_id' => $notificationData->company_id,
                'message' => $message,
                'link' => '/company/borrow/billing-summary',
                "is_read" => false,
            );
        
            $createNotification = Notifications::create($notificationData);
            event(new CompanyNotificationEvent($createNotification)); //Event for realtime notif
        }
    }
}
