<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\LoanBorrowList;
use App\Jobs\Command\ProcessLoanBorrowListToWorkInProgress;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Everyday, update status of loan_borrow_lists table to Work in progress where date_to is equals today';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $checkIfLoanBorrowListHasDataToBeUpdated = LoanBorrowList::whereRaw('date_from <= CURDATE()')
            ->where('status', 'Pending')
            ->limit(1)
        ->get();
        
        if (!$checkIfLoanBorrowListHasDataToBeUpdated->isEmpty()) {
            echo "\n".'Queuing updateloanborrowliststatustoworkinprogress '.date('l jS \of F Y h:i:s A');
            ProcessLoanBorrowListToWorkInProgress::dispatch()->onQueue('updatequeuingloanborrowliststatustoworkinprogress')->onConnection('jobs_loan_borrow_lists');
        }
    }
}
