<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\LoanPendingWorkers;
use App\Jobs\Command\ProcessExpiredLoanPendingWorkers;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Everyday, update status of loan_pending_workers and borrow_pending_workers table to Declined where date_from is equals to yesterday or the other days (In short, expired loan and borrow pending workers)';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $checkIfLoanPendingWorkersListHasDataToBeUpdated = LoanPendingWorkers::whereRaw('date_from < CURDATE()')
            ->where('status', 'Pending')
            ->limit(1)
            ->get();

        if (!$checkIfLoanPendingWorkersListHasDataToBeUpdated->isEmpty()) {
            echo "\n".'Queuing declineexpiredloanrequest '.date('l jS \of F Y h:i:s A');
            ProcessExpiredLoanPendingWorkers::dispatch()->onQueue('declinequeuingexpiredloanrequest')->onConnection('jobs_expired_requests');
        }
    }
}
