<?php

namespace App\Classes\Modules\Companies\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\Segments\Standards\Rules\CanCreateConstant;
use App\Classes\Modules\Segments\Standards\Rules\CanUpdateConstant;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Http\Resources\ConstantResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateSupplierCurrenciesLogic extends AbstractControllerLogic
{

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

    /** @var CanCreateConstant */
    private $canCreateConstant;

    /** @var CanUpdateConstant */
    private $canUpdateConstant;

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

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

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

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


    /**
     * UpdateSupplierCurrenciesLogic constructor.
     * @param CanCreateConstant $canCreateConstant
     * @param CanUpdateConstant $canUpdateConstant
     * @param FetchesSegment $fetchesSegment
     * @param FetchesConstant $fetchesConstant
     * @param UpdatesConstant $updatesConstant
     * @param CreatesConstant $createsConstant
     */
    public function __construct(CanCreateConstant $canCreateConstant, CanUpdateConstant $canUpdateConstant, FetchesSegment $fetchesSegment, FetchesConstant $fetchesConstant, UpdatesConstant $updatesConstant, CreatesConstant $createsConstant)
    {
        $this->canCreateConstant = $canCreateConstant;
        $this->canUpdateConstant = $canUpdateConstant;
        $this->fetchesSegment = $fetchesSegment;
        $this->fetchesConstant = $fetchesConstant;
        $this->updatesConstant = $updatesConstant;
        $this->createsConstant = $createsConstant;
    }

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

        $supplierId = $request->route('id');

        $object = new ConstantObject('Supplier\'s Currencies',SegmentConstants::SUPPLIER_CURRENCIES,
            [
                'id' => $supplierId, 'currencies' => $request->input('currencies')
            ]
        );

        try {

            $constant = $this->fetchesConstant->execute(['supplier_currencies' => $supplierId]);
            $this->canUpdateConstant->passes($object);
            $this->updatesConstant->execute($constant, $object);

        } catch (ResourceNotFoundException $exception){
            $this->canCreateConstant->passes($object);
            $segment = $this->fetchesSegment->execute(['type' => SegmentConstants::STANDARD_SEGMENT]);
            $this->createsConstant->execute($segment, $object);
        }

        return $this->response([]);

    }

}