<?php


namespace App\Core\Service;


use App\Core\Repository\Currency\CurrencyRepository;
use App\Core\Repository\CurrencyRates\CurrencyRatesRepository;
use App\Exceptions\ApiErrorException;
use Illuminate\Support\Facades\Log;

class CurrencyService
{
    private $currencyRepository;
    const STATUS = 1;

    public function __construct(
        CurrencyRepository $currencyRepository,
        CurrencyRatesRepository $currencyRatesRepository
    )
    {
        $this->currencyRepository = $currencyRepository;
        $this->currencyRatesRepository = $currencyRatesRepository;
    }


    public function getCurrencyList(array $params = [], $columns = ["*"])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $criteria = [
                'criteria' => [
                    ['field' => 'status', 'condition' => '=', 'value' => CurrencyService::STATUS],
                ],
                "orderBy" => [
                    ["field" => "name", "value" => "ASC"]
                ]
            ];

            if(isset($params['justBasicCurrencies']) && $params['justBasicCurrencies']) {
                $criteria['criteria'][] = ['field' => 'is_basic', 'condition' => '=', 'value' => 1];
            }

            $data = $this->currencyRepository->findByCriteria($criteria, $columns);
            $response = [
                'status' => true,
                'data' => $data,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function lastExchangeRate($currency, $exchangeCurrency)
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $criteria = [
                'criteria' => [
                    ['field' => 'currency_code', 'condition' => '=', 'value' => $currency],
                    ['field' => 'exc_currency_code', 'condition' => '=', 'value' => $exchangeCurrency],
                ],
                "orderBy" => [
                    ["field" => "date", "value" => "DESC"]
                ]
                ,
                'firstRow' => true,
            ];

            $data = $this->currencyRatesRepository->findByCriteria($criteria);

            $rate = fillOnUndefined($data, 'rate', 1);

            $response = [
                'status' => true,
                'data' => $rate,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function exchangeCurrencyRates($currency)
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $criteria = [
                'criteria' => [
                    ['field' => 'currency_code', 'condition' => '=', 'value' => $currency],
                ],
                "orderBy" => [
                    ["field" => "date", "value" => "DESC"]
                ],
                'firstRow' => true,
            ];

            $lastDate = $this->currencyRatesRepository->findByCriteria($criteria);


            $criteria = [
                'criteria' => [
                    ['field' => 'currency_code', 'condition' => '=', 'value' => $currency],
                    ['field' => 'date', 'condition' => '=', 'value' => $lastDate['date']],
                ],
                "orderBy" => [
                    ["field" => "currency_code", "value" => "ASC"],
                    ["field" => "exc_currency_code", "value" => "ASC"]
                ]
            ];

            $currencyRates = $this->currencyRatesRepository->findByCriteria($criteria);

            $response = [
                'status' => true,
                'data' => $currencyRates,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }
}
