<?php

namespace App\Console\Commands\Company\Search;

use Illuminate\Console\Command;
use App\Models\Search;
use Carbon\Carbon;
use App\Jobs\Command\ProcessSearchRemovePastDate;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Remove search data where the date_to is already on the ast';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        $dateToCheck = Carbon::now()->format('Y-m-d'); //get the worker who's contract is finished yesterday
        $garbageData = Search::where('date_to', '<' ,$dateToCheck)->limit(1)->get();

        if($garbageData){
            echo 'Queuing search-removepastdates : ' . Carbon::now()->format('jS \\of F Y');
            ProcessSearchRemovePastDate::dispatch()->onQueue('searchremovepastdates')->onConnection('jobs_carts');
        }else{
            echo 'No Last day of worker detected on the date '. Carbon::now()->format('jS \\of F Y');
        }
    }
}
