<?php

namespace App\Classes\Jobs\Commands\V2;


use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\SeasonalSegment;
use App\Classes\Modules\Companies\Services\RemovesCompanyFromSegment;
use Illuminate\Support\Facades\Log;

class RemoveSeasonalSegmentCompanyV2CommandJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function handle()
    {
        Log::info(Carbon::now() . ': Start job - Perform soft delete on Seasonal Segment Company.');
        $start   = new Carbon();

        $seasonalSegment = SeasonalSegment::where('ending_on', '<=', Carbon::today())->get();

        if (count($seasonalSegment)){
            foreach ($seasonalSegment as $seasonalSegmentCompany) {
                (new RemovesCompanyFromSegment())->execute($seasonalSegmentCompany->company, $seasonalSegmentCompany->segment);
                $seasonalSegmentCompany->delete();
                Log::info(Carbon::now() . ' : Deleted id: ' . $seasonalSegmentCompany->id);
            }
        }

        $end = new Carbon();
        $elapsedTime = $start->diff($end)->format('%H:%I:%S');
        Log::info(Carbon::now() . ': End job - Perform soft delete on Seasonal Segment Company. ElapsedTime: ' . $elapsedTime . '.');
    }
}
