<?php

namespace App\Jobs\Command;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\CompanySubscriptions;
use Carbon\Carbon;

class ProcessBlockedCompanySubscription implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $tries = 5;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $subscriptions = CompanySubscriptions::select(
            'id',
            'current_month_paid',
            'duration',
            'is_blocked',
            'is_monthly_expired',
        )
        ->where('is_monthly_expired', 'true')
        ->where('mode_of_payment', 'monthly')
        ->where('current_month_paid', '<=', Carbon::now()->subDays(30)->format('Y-m-d'))
        ->limit(100)
        ->get();

        foreach($subscriptions as $subscription){
           CompanySubscriptions::where('id', $subscription->id)->update(['is_blocked' => 'true']);
        }
    }
}
