<?php

namespace backendApi\modules\v1\controllers;

use backendApi\modules\v1\models\AdminCountry;
use backendApi\modules\v1\models\AdminRole;
use common\helpers\Cache;
use common\helpers\Form;
use common\models\Currency;
use common\models\CurrencyConversions;
use common\models\forms\CurrenciesConversionsForm;
use Yii;
use yii\db\Exception;
use yii\web\HttpException;

class CurrencyController extends BaseController
{
    public $modelClass = Currency::class;

    public $currencyConversionsModelClass = CurrencyConversions::class;

    public function actionCurrencies()
    {
        $data = $this->modelClass::getFromCache();
        return static::notice(['data' => $data]);
    }

    public function actionCurrenciesConversions()
    {
        // 国家
        $countries = Cache::getCountries();
        // 货币
        $currencies = $this->modelClass::getFromCache();
        // 汇率配置
        $currencyConversion = $this->currencyConversionsModelClass::getFromCache();

        $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
        if (!$isSuper) {
            $adminId = Yii::$app->getUser()->getUserInfo()['id'];
            $adminCountry = AdminCountry::getCountry($adminId);

            $countries = array_filter($countries, fn($country) => in_array($country['ID'], $adminCountry));
            $countries = array_values($countries);
            $countriesId = array_column($countries, 'ID');

            $currencies = array_filter($currencies, fn($currency) => in_array($currency['ID'], $countriesId));
            $currencies = array_values($currencies);
            $currenciesId = array_column($currencies, 'ID');

            $currencyConversion = array_filter($currencyConversion, fn($conversion) => in_array($conversion['TO_CURRENCY_ID'], $currenciesId));
            $currencyConversion = array_values($currencyConversion);
        }

        $currencyConversion = array_column($currencyConversion, NULL, 'TO_CURRENCY_ID');

        foreach ($currencies as &$currency) {
            $currency['PRODUCT_RATE'] = $currencyConversion[$currency['ID']]['PRODUCT_RATE'] ?? 0;
            $currency['BONUSES_RATE'] = $currencyConversion[$currency['ID']]['BONUSES_RATE'] ?? 0;
        }

        return static::notice(['data' => $currencies]);
    }

    /**
     * @throws Exception
     * @throws HttpException
     */
    public function actionSetCurrenciesConversions()
    {
        if (\Yii::$app->request->isPost) {
            $formModel = new CurrenciesConversionsForm();
            $formModel->scenario = 'setCurrenciesConversions';
            if ($formModel->load(\Yii::$app->request->post(), '') && $formModel->setCurrenciesConversions()) {
                // 更新缓存
                $this->currencyConversionsModelClass::updateToCache();

                return static::notice(\Yii::t('ctx', 'successfully'));
            } else {
                return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);

            }
        }

        return static::notice(\Yii::t('ctx', 'illegalRequest'));
    }
}
