<?php

namespace App\Listeners\Company;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

use App\Events\Company\CreateNotificationEvent;
use App\Events\Company\PusherNotificationEvent as CompanyNotificationEvent;

use App\Models\LoanPendingWorkers;
use App\Models\Notifications;
use App\Models\Tasks;


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

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle(CreateNotificationEvent $event)
    {
        if ($event->transaction == "request") {
            $request_details = LoanPendingWorkers::select(
                'loaner.name as loaner_company',
                'borrower.name as borrower_company',
                'loan_requests.loaner_company_id as loaner_id',
                'loan_requests.borrower_company_id as borrower_id',
                'loan_pending_workers.status',
                'workers.worker_id'
            )
                ->join('loan_requests', 'loan_requests.id', 'loan_pending_workers.loan_request_id')
                ->join('companies as loaner', 'loaner.id', 'loan_requests.loaner_company_id')
                ->join('companies as borrower', 'borrower.id', 'loan_requests.borrower_company_id')
                ->join('workers', 'workers.id', 'loan_pending_workers.worker_id')
                ->where('loan_pending_workers.id', $event->loan_pending_worker_id)
                ->first();
            /* Log::channel('background')->info($request_details); */

            switch ($event->action) {
                case 'create':
                    $borrower = [
                        "type" => "Borrow Request",
                        "company_id" => $request_details->borrower_id,
                        "link" => '/company/borrow/pending-requests?id=', $event->request_id,
                        "message" => null,
                        "is_read" => 0,

                    ];
                    $loaner = [
                        "type" => "Loan Request",
                        "company_id" => $request_details->loaner_id,
                        "link" => '/company/loan/pending-requests',
                        "reference_id" => $event->loan_request_id,
                        "message" => null,
                        "is_read" => 0,
                        "status" => 'pending',

                    ];

                    $request_count = count(LoanPendingWorkers::where('loan_request_id', $event->loan_request_id)->get());
                    $loaner["message"] = 'A company has requested <b>' . $request_count . '</b> of your employee(s).';
                    $borrower["message"] = "You have requested <b>" . $request_count . "</b> employee(s) from a company.";

                    Tasks::create($loaner);
                    Notifications::create($borrower);
                    event(new CompanyNotificationEvent($borrower)); //Event for realtime notif
                    event(new CompanyNotificationEvent($loaner)); //Event for realtime notif

                    break;
                case 'update':
                    $borrower_url = "/company/borrow/" . strtolower($request_details->status) . "-requests";
                    $borrower_message = "A company had " . strtolower($request_details->status) . " your request for Employee <b>" . $request_details->worker_id . "</b>.";

                    $notitification = [
                        "type" => "Borrow Request",
                        "company_id" => $request_details->borrower_id,
                        "message" => $borrower_message,
                        "link" => $borrower_url,
                        "is_read" => 0,
                    ];

                    Notifications::create($notitification);
                    event(new CompanyNotificationEvent($notitification)); //Event for realtime notif

                    break;
                case 'cancelled':
                    $loaner_url = "/company/loan/pending-requests";
                    $loaner_message = "A company had " . $event->action . " their request for Employee <b>" . $request_details->worker_id . "</b>.";

                    $notitification = [
                        "type" => "Borrow Request",
                        "company_id" => $request_details->loaner_id,
                        "message" => $loaner_message,
                        "link" => $loaner_url,
                        "is_read" => 0,
                    ];

                    Notifications::create($notitification);
                    event(new CompanyNotificationEvent($notitification)); //Event for realtime notif

                    break;
            }

        }
    }
}
