<?php

namespace routes;

use classes\authentication;
use classes\fxratesapi;
use classes\response;
use classes\router;
use objects\currency_conversion_rates_o;
use objects\logs_o;
use traits\route_t;

class moduleFxRatesAPIRoute
{
    use route_t;

    public function run(): void
    {
        global /** @var response $response */
        /** @var router $router */
        $router, $response;


        /** Modules > FXRatesAPI > conversion rate > GET */
        $this->get('/modules/fxratesapi/rate', function () {
            global $response;
            self::requirePermission('modules_fxratesapi_rate');
            $user = (new authentication())->get_user();
            if ($user) {
                self::requireParameters(['base', 'target']);
                self::requireType('base', self::type_string());
                self::requireType('target', self::type_string());
                self::requireMinLength('base', 3);
                self::requireMaxLength('base', 3);
                self::requireMinLength('target', 3);
                self::requireMaxLength('target', 3);
                (new logs_o())->add('modules_fxratesapi', 'global', 1, $user->id, 'MODULES_FXRATESAPI', 'User accessed the conversion rate');
                $result = (new fxratesapi())->getConversionRate(
                    self::getParameter('base'),
                    self::getParameter('target'),
                    'latest',
                    []
                );
                // Check if the result is an error
                if (!isset($result->success) || !$result->success) {
                    (new logs_o())->add('modules_fxratesapi', 'global', 1, $user->id, 'MODULES_FXRATESAPI', 'User accessed the conversion rate and got an error');
                    $response->error($result->error, 400);
                }
                // Set the rates
                $currency_conversion_rates = (new currency_conversion_rates_o());
                $currency_conversion_rates->setMany($result->rates);
                // Response
                $response->success($result->rates, 200);
            } else {
                (new logs_o())->add('modules_fxratesapi', 'global', 1, 0, 'MODULES_FXRATESAPI', 'User accessed the conversion rate without being logged in');
                $response->error('Invalid session', 400);
            }
        },
            [
                'modules_fxratesapi_rate' => 'Update conversion rates using the FXRatesAPI',
            ]
        );

        /** Modules > FXRatesAPI > conversion rates > GET */
        $this->get('/modules/fxratesapi/rates', function () {
            global $response;
            self::requirePermission('modules_fxratesapi_rates');
            $user = (new authentication())->get_user();
            if ($user) {
                (new logs_o())->add('modules_fxratesapi', 'global', 1, $user->id, 'MODULES_FXRATESAPI', 'User accessed the conversion rate');
                $result = (new currency_conversion_rates_o())->listObjects(function ($result) {
                    return [
                        'id' => (int)$result['id'],
                        'currency' => $result['currency'],
                        'rate' => (float)$result['rate'],
                    ];
                });
                // Response
                $response->success($result, 200);
            } else {
                (new logs_o())->add('modules_fxratesapi', 'global', 1, 0, 'MODULES_FXRATESAPI', 'User accessed the conversion rate without being logged in');
                $response->error('Invalid session', 400);
            }
        },
            [
                'modules_fxratesapi_rate' => 'Update conversion rates using the FXRatesAPI',
            ]
        );

    }
}