<?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\FetchesConstant;
use App\Classes\Modules\Segments\Services\UpdatesConstant;
use App\Classes\Modules\ServiceTypes\DataTransferObjects\ServiceDetailObject;
use App\Classes\Modules\ServiceTypes\Services\CreatesServiceTypeConstantDetails;
use App\Classes\Modules\ServiceTypes\Services\UpdatesServiceCurrencyRates;
use App\Classes\Modules\ServiceTypes\Services\UpdatesServiceType;
use App\Classes\Modules\ServiceTypes\Services\FetchesServiceType;
use App\Classes\Modules\ServiceTypes\Standards\Rules\CanUpdateServiceType;
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 UpdateServiceTypeLogic extends AbstractControllerLogic
{


    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Updated Service Type',
            'message' => 'You have successfully updated the Service Type'
        ];
    }

    /** @var CanUpdateServiceType */
    private $canUpdateServiceType;

    /** @var UpdatesServiceType */
    private $updatesServiceType;

    /** @var FetchesServiceType */
    private $fetchesServiceType;

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

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

    /** @var UpdatesConstant */
    private $updatesConstant;

    /** @var FetchesConstant */
    private $fetchesConstant;


    /**
     * UpdateServiceTypeLogic constructor.
     * @param CanUpdateServiceType $canUpdateServiceType
     * @param UpdatesServiceType $updatesServiceType
     * @param FetchesServiceType $fetchesServiceType
     * @param CreatesServiceTypeConstantDetails $createsServiceTypeConstantDetails
     * @param UpdatesServiceCurrencyRates $updatesServiceCurrencyRates
     * @param UpdatesConstant $updatesConstant
     * @param FetchesConstant $fetchesConstant
     */
    public function __construct(CanUpdateServiceType $canUpdateServiceType, UpdatesServiceType $updatesServiceType, FetchesServiceType $fetchesServiceType, CreatesServiceTypeConstantDetails $createsServiceTypeConstantDetails, UpdatesServiceCurrencyRates $updatesServiceCurrencyRates, UpdatesConstant $updatesConstant, FetchesConstant $fetchesConstant)
    {
        $this->canUpdateServiceType = $canUpdateServiceType;
        $this->updatesServiceType = $updatesServiceType;
        $this->fetchesServiceType = $fetchesServiceType;
        $this->createsServiceTypeConstantDetails = $createsServiceTypeConstantDetails;
        $this->updatesServiceCurrencyRates = $updatesServiceCurrencyRates;
        $this->updatesConstant = $updatesConstant;
        $this->fetchesConstant = $fetchesConstant;
    }

    /**
     * @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->canUpdateServiceType->passes($object);

        $service = $this->fetchesServiceType->execute(['id' => $request->route('id')]);

        /** @var ServiceType $service */
        $service = $this->updatesServiceType->execute($service, $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'), $request->input('configurations.active'), $request->input('configurations.billable'));

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

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

        $this->updatesConstant->execute($this->fetchesConstant->execute(['service_type' => $service->id]), $constantObject);

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

    }

}