<?php

namespace App\Console\Commands;

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

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Everyday, update status of loan_borrow_lists table to Work done where date_to is equals to yesterday or the other days';

    /**
     * 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_to < CURDATE()')
            ->where('status', 'Work in progress')
            ->limit(1)
            ->get();

        if (!$checkIfLoanBorrowListHasDataToBeUpdated->isEmpty()) {
            echo "\n".'Queuing updateloanborrowliststatustoworkdone '.date('l jS \of F Y h:i:s A');
            ProcessLoanBorrowListToWorkDone::dispatch()->onQueue('updatequeuingloanborrowliststatustoworkdone')->onConnection('jobs_loan_borrow_lists');
        }
    }
}
