<?php

namespace App\Classes\Modules\Currencies\ControllersLogic\Rates;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Currencies\Services\Rates\ListsRates;
use App\Classes\Modules\Currencies\Standards\Rules\Rates\CanListRates;
use App\Http\Resources\CurrencyRateResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ListRatesLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Retrieved Currency Rate',
            'message' => 'You have successfully retrieved a list of Currency Rate'
        ];
    }

    /** @var CanListRates */
    private $canListRates;

    /** @var ListsRates */
    private $listsRates;

    /**
     * ListRatesLogic constructor.
     * @param CanListRates $canListRates
     * @param ListsRates $listsRates
     */
    public function __construct(CanListRates $canListRates, ListsRates $listsRates)
    {
        $this->canListRates = $canListRates;
        $this->listsRates = $listsRates;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        try {
            $this->canListRates->passes();

            $query = $this->listsRates->execute(
                [
                    'currency_id' => $request->input('currency_id'),
                    'payment_method_type' => $request->input('payment_method_type')
                ]
            );

            return $this->collectionResponse(CurrencyRateResource::collection($query));

        } catch (\Exception $exception){
            throw new ErrorException($exception->getMessage(), $exception->getCode());
        }

    }

}