<?php

namespace App\Classes\Modules\ServiceTypes\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Segments\DataTransferObjects\ConstantObject;
use App\Classes\Modules\Segments\Services\CreatesConstant;
use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Classes\Modules\ServiceTypes\DataTransferObjects\ServiceDetailObject;
use App\Classes\Modules\ServiceTypes\Services\CreatesServiceType;
use App\Classes\Modules\ServiceTypes\Services\CreatesServiceTypeConstantDetails;
use App\Classes\Modules\ServiceTypes\Services\UpdatesServiceCurrencyRates;
use App\Classes\Modules\ServiceTypes\Standards\Rules\CanCreateServiceType;
use App\Classes\Modules\ServiceTypes\DataTransferObjects\ServiceTypeObject;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Http\Resources\ServiceTypeResource;
use App\Models\ServiceType;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateServiceTypeLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Created Service Type',
            'message' => 'You have successfully created a new Service Type'
        ];
    }

    /** @var CanCreateServiceType */
    private $canCreateServiceType;

    /** @var CreatesServiceType */
    private $createsServiceType;

    /** @var CreatesServiceTypeConstantDetails */
    private $createsServiceTypeConstantDetails;

    /** @var UpdatesServiceCurrencyRates */
    private $updatesServiceCurrencyRates;

    /** @var FetchesSegment */
    private $fetchesSegment;

    /** @var CreatesConstant */
    private $createsConstant;


    /**
     * CreateServiceTypeLogic constructor.
     * @param CanCreateServiceType $canCreateServiceType
     * @param CreatesServiceType $createsServiceType
     * @param CreatesServiceTypeConstantDetails $createsServiceTypeConstantDetails
     * @param UpdatesServiceCurrencyRates $updatesServiceCurrencyRates
     * @param FetchesSegment $fetchesSegment
     * @param CreatesConstant $createsConstant
     */
    public function __construct(CanCreateServiceType $canCreateServiceType, CreatesServiceType $createsServiceType, CreatesServiceTypeConstantDetails $createsServiceTypeConstantDetails, UpdatesServiceCurrencyRates $updatesServiceCurrencyRates, FetchesSegment $fetchesSegment, CreatesConstant $createsConstant)
    {
        $this->canCreateServiceType = $canCreateServiceType;
        $this->createsServiceType = $createsServiceType;
        $this->createsServiceTypeConstantDetails = $createsServiceTypeConstantDetails;
        $this->updatesServiceCurrencyRates = $updatesServiceCurrencyRates;
        $this->fetchesSegment = $fetchesSegment;
        $this->createsConstant = $createsConstant;
    }

    /**
     * @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
    {

        $object = new ServiceTypeObject($request->input('name'));

        $this->canCreateServiceType->passes($object);

        /** @var ServiceType $service */
        $service = $this->createsServiceType->execute($object);

        $object = new ServiceDetailObject($service->id, (int) $request->input('configurations.bank_id'),
            $request->input('configurations.service_charge'), $request->input('configurations.minimum_charge'), $request->input('configurations.tax'),
            $request->input('configurations.po_limit'), $request->input('configurations.currencies'), false, $request->input('configurations.billable'));

        $this->updatesServiceCurrencyRates->execute($service, $object->getCurrencies());

        $constantObject = new ConstantObject('Service Type', SegmentConstants::SERVICE_TYPE, $this->createsServiceTypeConstantDetails->execute($object));

        $this->createsConstant->execute($this->fetchesSegment->execute(['type' => SegmentConstants::STANDARD_SEGMENT]), $constantObject);


        return $this->resourceResponse(new ServiceTypeResource($service));

    }

}