<?php

namespace App\Console\Commands\Company\Borrow;

use Illuminate\Console\Command;
use App\Models\BorrowApproved;
use Carbon\Carbon;
use App\Events\Company\Borrow\NotifyCompanyForWorkerRatingEvent;

class NotifyCompanyForWorkerRating extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'companynotification-wokerrating:cron';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This is to trigger a notification on making the company rate the workers, under borrow approved pagepage ';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $dateToCheck = Carbon::now()->subDays(1)->format('Y-m-d'); //get the worker who's contract is finished yesterday
        $lastDayOfApprovedWorker = BorrowApproved::where('date_to', $dateToCheck)->limit(1)->get();
        if(!$lastDayOfApprovedWorker->isEmpty()){
            event(new NotifyCompanyForWorkerRatingEvent());
            echo 'Last day of worker detected.';
        }else{
            echo 'No Last day of worker detected on the date '. Carbon::parse($dateToCheck)->format('jS \\of F Y');
        }
    }
}
