<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\CompanySubscriptions;
use App\Jobs\Command\ProcessCompanySubscriptionChecking;
use Carbon\Carbon;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Check company subscription expiration/duration';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $subscriptions = CompanySubscriptions::select(
            'id',
            'current_month_paid',
            'duration',
            'is_expired',
        )
        ->where('duration', '<=', Carbon::now()->subDays(1)->format('Y-m-d'))
        ->where('is_expired', 'false')
        ->get();

        if(!$subscriptions->isEmpty()) {
            echo "\n".'Queuing checksubscriptionexpiration '.date('l jS \of F Y h:i:s A');
            ProcessCompanySubscriptionChecking::dispatch()->onConnection('jobs_subscriptions')->onQueue('updateexpiredsubscription');
        }else{
            echo 'No expired company subscription today '. Carbon::now();
        }
    }
}
