<?php

namespace App\Console\Commands;

use App\Models\SeasonalSegment;
use Illuminate\Console\Command;
use Carbon\Carbon;
use App\Classes\Modules\Companies\Services\RemovesCompanyFromSegment;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Perform soft delete on Seasonal Segment Company';

    /** @var RemovesCompanyFromSegment */
    private $removesCompanyFromSegment;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(RemovesCompanyFromSegment $removesCompanyFromSegment)
    {
        parent::__construct();
        $this->removesCompanyFromSegment = $removesCompanyFromSegment;
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $seasonalSegment = SeasonalSegment::where('ending_on', '<=', Carbon::today())->get();

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