<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\CompanyCart;
use Carbon\Carbon;
use App\Jobs\Command\ProcessRemoveUnsubmittedCarts;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Delete (using softdelete) all records that are not submitted within the day from date of creation';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $existingCarts = CompanyCart::where('created_at', '<=', Carbon::now()->subDays(1)->toDateTimeString())
            ->limit(1)
            ->get();

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