<?php

namespace App\Classes\Modules\ServiceTypes\ControllersLogic;


use App\Classes\Exceptions\ResourceNotFoundException;
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\FetchesConstant;
use App\Classes\Modules\Segments\Services\FetchesSegment;
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\FetchesServiceType;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Http\Resources\CustomServiceTypeResource;
use App\Models\Segment;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateCustomServiceConstantLogic extends AbstractControllerLogic
{


    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Update Segment Service',
            'message' => 'You have successfully update a segments service configuration'
        ];
    }

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

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

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

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

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

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

    /**
     * UpdateCustomServiceConstantLogic constructor.
     * @param FetchesServiceType $fetchesServiceType
     * @param CreatesServiceTypeConstantDetails $createsServiceTypeConstantDetails
     * @param FetchesSegment $fetchesSegment
     * @param FetchesConstant $fetchesConstant
     * @param UpdatesConstant $updatesConstant
     * @param CreatesConstant $createsConstant
     */
    public function __construct(FetchesServiceType $fetchesServiceType, CreatesServiceTypeConstantDetails $createsServiceTypeConstantDetails, FetchesSegment $fetchesSegment, FetchesConstant $fetchesConstant, UpdatesConstant $updatesConstant, CreatesConstant $createsConstant)
    {
        $this->fetchesServiceType = $fetchesServiceType;
        $this->createsServiceTypeConstantDetails = $createsServiceTypeConstantDetails;
        $this->fetchesSegment = $fetchesSegment;
        $this->fetchesConstant = $fetchesConstant;
        $this->updatesConstant = $updatesConstant;
        $this->createsConstant = $createsConstant;
    }

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

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

        $customFields = $request->input('custom_fields');

        $object = new ServiceDetailObject($service->id, $customFields['bank_id'], $customFields['service_charge'],
            $customFields['minimum_charge'], $customFields['tax'], $customFields['po_limit'], $request->input('configurations.currencies'),
            $request->input('configurations.active'), true);

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

        /** @var Segment $segment */
        $segment = $this->fetchesSegment->execute(['id' => $request->route('id')]);

        try {

            $constant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'custom_service_type' => $object->getId()]);

            $this->updatesConstant->execute($constant, $constantObject);

        } catch (ResourceNotFoundException $exception){

            $constant = $this->createsConstant->execute($segment, $constantObject);
        }


        return $this->resourceResponse(new CustomServiceTypeResource($constant));

    }

}