<?php

namespace App\Classes\Modules\Currencies\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Currencies\Services\FetchesCurrency;
use App\Classes\Modules\Currencies\Standards\Rules\CanFetchCurrency;
use App\Classes\Modules\Segments\Services\FetchesConstant;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Http\Resources\CurrencyResource;
use App\Models\Currency;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class FetchSystemPrimaryCurrencyLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Fetch System\'s primary currency',
            'message' => 'You have successfully retrieved a Currency'
        ];
    }


    /** @var CanFetchCurrency */
    private $canFetchCurrency;

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

    /** @var FetchesCurrency */
    private $fetchesCurrency;

    /**
     * FetchSystemPrimaryCurrencyLogic constructor.
     * @param CanFetchCurrency $canFetchCurrency
     * @param FetchesConstant $fetchesConstant
     * @param FetchesCurrency $fetchesCurrency
     */
    public function __construct(CanFetchCurrency $canFetchCurrency, FetchesConstant $fetchesConstant, FetchesCurrency $fetchesCurrency)
    {
        $this->canFetchCurrency = $canFetchCurrency;
        $this->fetchesConstant = $fetchesConstant;
        $this->fetchesCurrency = $fetchesCurrency;
    }

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

        $this->canFetchCurrency->passes();

        $constant = $this->fetchesConstant->execute(['reference' => SegmentConstants::SYSTEM_PRIMARY_CURRENCY]);

        /** @var Currency $currency */
        $currency = $this->fetchesCurrency->execute(['id' => $constant->detail->id]);

        return $this->resourceResponse(new CurrencyResource($currency));

    }
}