<?php

namespace App\Classes\Modules\Currencies\ControllersLogic;

use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\Exceptions\ResourceConflictException;
use App\Classes\Exceptions\ResourceNotFoundException;
use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Countries\DataTransferObjects\CountryObject;
use App\Classes\Modules\Countries\Services\CreatesCountry;
use App\Classes\Modules\Countries\Services\FetchesCountry;
use App\Classes\Modules\Currencies\Services\FetchesCurrency;
use App\Classes\Modules\Currencies\Standards\Rules\CanCreateCurrency;
use App\Classes\Modules\Currencies\Services\CreatesCurrency;
use App\Classes\Modules\Currencies\Services\CreatesCurrencyLog;
use App\Classes\Modules\Currencies\DataTransferObjects\CurrencyObject;
use App\Http\Resources\CurrencyResource;

use App\Models\Country;
use App\Models\Currency;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateCurrencyLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Create Currency',
            'message' => 'You have successfully created a new Currency'
        ];
    }

    /** @var FetchesCountry */
    private $fetchesCountry;

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

    /** @var CreatesCountry */
    private $createsCountry;

    /** @var CanCreateCurrency */
    private $canCreateCurrency;

    /** @var CreatesCurrency */
    private $createsCurrency;

    /** @var CreatesCurrencyLog */
    private $createsCurrencyLog;

    /**
     * CreateCurrencyLogic constructor.
     * @param FetchesCountry $fetchesCountry
     * @param FetchesCurrency $fetchesCurrency
     * @param CreatesCountry $createsCountry
     * @param CanCreateCurrency $canCreateCurrency
     * @param CreatesCurrency $createsCurrency
     * @param CreatesCurrencyLog $createsCurrencyLog
     */
    public function __construct(FetchesCountry $fetchesCountry, FetchesCurrency $fetchesCurrency, CreatesCountry $createsCountry, CanCreateCurrency $canCreateCurrency, CreatesCurrency $createsCurrency, CreatesCurrencyLog $createsCurrencyLog)
    {
        $this->fetchesCountry = $fetchesCountry;
        $this->fetchesCurrency = $fetchesCurrency;
        $this->createsCountry = $createsCountry;
        $this->canCreateCurrency = $canCreateCurrency;
        $this->createsCurrency = $createsCurrency;
        $this->createsCurrencyLog = $createsCurrencyLog;
    }

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

        $countryObject = Country($request->input('country_short_code'));

        /** @var Country $country */
        $country = $this->createsCountry->execute(new CountryObject($countryObject));

        $currencyInfo = $countryObject->getCurrency();
        $currencyObject = new CurrencyObject($currencyInfo['iso_4217_name'], $currencyInfo['iso_4217_code']);

        $this->canCreateCurrency->passes($currencyObject);

        try {
            $this->fetchesCurrency->execute(['country_id' => $country->id, 'short_code' => $currencyObject->getShortCode()]);
            throw new ResourceConflictException('Duplicated Currency. This currency already exists in the system');
        } catch (ResourceNotFoundException $exception){
            /** @var Currency $currency */
            $currency = $this->createsCurrency->execute($country, $currencyObject);
            $this->createsCurrencyLog->execute($currency);
        }

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

    }
}