<?php

namespace App\Classes\Modules\Companies\ControllersLogic;

use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Processors\AssignSegmentProcessor;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Segments\DataTransferObjects\SeasonalSegmentObject;
use App\Classes\Modules\Milestones\Processors\CheckMilestonesForRewardProcessor;
use App\Classes\Modules\Segments\Services\CreatesSeasonalSegment;
use App\Http\Resources\CompanyResource;
use App\Classes\ValueObjects\Constants\Milestones;
use App\Models\SeasonalSegment;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AssignCompanyToSegmentLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Assign Company To Segment',
            'message' => 'You have successfully assigned Company to segment'
        ];
    }

    /** @var FetchesCompany */
    private $fetchesCompany;

    /** @var AssignSegmentProcessor */
    private $assignCompanyToSegmentProcessor;

    /** @var CreatesSeasonalSegment */
    private $createsSeasonalSegment;

    /** @var CheckMilestonesForRewardProcessor */
    private $checkMilestonesForRewardProcessor;

    /**
     * AssignCompanyToSegmentLogic constructor.
     * @param FetchesCompany $fetchesCompany
     * @param AssignSegmentProcessor $assignCompanyToSegmentProcessor
     * @param CreatesSeasonalSegment $createsSeasonalSegment
     * @param CheckMilestonesForRewardProcessor $checkMilestonesForRewardProcessor
     */
    public function __construct(FetchesCompany $fetchesCompany, AssignSegmentProcessor $assignCompanyToSegmentProcessor, CreatesSeasonalSegment $createsSeasonalSegment, CheckMilestonesForRewardProcessor $checkMilestonesForRewardProcessor)
    {
        $this->fetchesCompany = $fetchesCompany;
        $this->assignCompanyToSegmentProcessor = $assignCompanyToSegmentProcessor;
        $this->createsSeasonalSegment = $createsSeasonalSegment;
        $this->checkMilestonesForRewardProcessor = $checkMilestonesForRewardProcessor;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request) : JsonResponse
    {

        $company = $this->fetchesCompany->execute(['id' => $request->route('id')]);

        $this->assignCompanyToSegmentProcessor->execute($company, $request->input('segment_id'));

        if ($request->input('ending_on')) {

            if ($request->input('ending_on') && Carbon::parse($request->input('ending_on')) <= Carbon::now()) {
                throw new MalformedRequestException('The Ending Date must be equal to or later than today\'s date.');
            }

            // todo-new
            // if (count($company->seasonalSegments)) {
            //     throw new MalformedRequestException('System error. Please contact admin.');
            // }

            $start_date = $request->input('starting_on') ? $request->input('starting_on') : Carbon::now();

            $seasonalSegmentObject = new SeasonalSegmentObject($company->id, $request->input('segment_id'), $start_date, $request->input('ending_on') ?? null);

            $this->createsSeasonalSegment->execute($seasonalSegmentObject);
        }

        //cief todo: case study 3 voucherify
        // $user = $company->employees()->first();
        // $this->checkMilestonesForRewardProcessor->execute($user, [Milestones::MILESTONE_3]);

        return $this->resourceResponse(new CompanyResource($company));
    }
}
