<?php

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


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Currencies\Services\Rates\ListsRateLogs;
use App\Classes\Modules\Currencies\Services\Rates\ListsRates;
use App\Classes\Modules\Currencies\Standards\Rules\Rates\CanListRates;
use App\Http\Resources\CurrencyRateLogResource;
use App\Models\CurrencyRateLog;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Carbon\Carbon;

class ListCurrencyRateHistoryLogic extends AbstractControllerLogic
{

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

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

    /** @var ListsRateLogs */
    private $listsRateLogs;

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

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


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

        $filterArray = json_decode($request->input('filters'));

        $rate_filter['currency_id'] = $filterArray->currency_id;
        $rate_filter['service_id'] = $filterArray->service_id;

        $currency_rate_ids = $this->listsRates->execute(
            [
                'currency_id' => $filterArray->currency_id,
                'service_id' => $filterArray->service_id,
            ]
        )->pluck('id')->toArray();

        $query = $this->listsRateLogs->execute(
            [
                'currency_rate_id_in' => $currency_rate_ids,
                'date_start' => new Carbon($filterArray->date_from),
                'date_end' => new Carbon($filterArray->date_to),
            ]
        );

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