<?php

namespace routes;

use classes\authentication;
use classes\edge_gateway_manager;
use classes\edgegateway;
use classes\response;
use objects\logs_o;
use traits\route_t;

class edgeGatewayConfigRoute
{
    use route_t;

    public function run(): void
    {
        $this->get('/edgegateway/config', fn() => $this->handleGetConfig(), [
            'modules_shelly_config' => 'Get edge gateway config',
        ]);
        $this->post('/edgegateway/config', fn() => $this->handlePostConfig(), [
            'modules_shelly_config' => 'Update edge gateway config',
        ]);
        $this->post('/edgegateway/config/broker-diagnostics', fn() => $this->handleBrokerDiagnostics(), [
            'modules_shelly_config' => 'Test edge gateway broker configuration',
        ]);
    }

    private function handleGetConfig(): void
    {
        global /** @var response $response */ $response;
        $this->requirePermission('modules_shelly_config');
        $user = (new authentication())->get_user();

        if (!$user) {
            (new logs_o())->add('edgegateway_config', 'global', 1, 0, 'EDGEGATEWAY_CONFIG', 'No user found, or invalid session');
            $response->error('Invalid session', 400);
        }

        (new logs_o())->add('edgegateway_config', 'global', 1, $user->id, 'EDGEGATEWAY_CONFIG', 'Successfully fetched edge gateway config');
        $config = (new edgegateway())->config->getConfigRequest();
        foreach ($config as &$entry) {
            if (($entry['variable'] ?? null) === 'broker_shared_secret') {
                $entry['value'] = '';
            }
        }
        unset($entry);

        $response->success($config);
    }

    private function handlePostConfig(): void
    {
        global /** @var response $response */ $response;
        $this->requirePermission('modules_shelly_config');
        $user = (new authentication())->get_user();

        if (!$user) {
            (new logs_o())->add('edgegateway_config', 'global', 1, 0, 'EDGEGATEWAY_CONFIG', 'No user found, or invalid session');
            $response->error('Invalid session', 400);
        }

        (new logs_o())->add('edgegateway_config', 'global', 1, $user->id, 'EDGEGATEWAY_CONFIG', 'Successfully updated edge gateway config');
        $response->success((new edgegateway())->config->postConfigRequest());
    }

    private function handleBrokerDiagnostics(): void
    {
        global /** @var response $response */ $response;
        $this->requirePermission('modules_shelly_config');
        $user = (new authentication())->get_user();

        if (!$user) {
            (new logs_o())->add('edgegateway_config', 'global', 1, 0, 'EDGEGATEWAY_BROKER_DIAGNOSTICS', 'No user found, or invalid session');
            $response->error('Invalid session', 400);
        }

        $payload = self::getParametersAsArray();
        $diagnosticOptions = array_intersect_key($payload, array_flip([
            'target',
            'broker_auth_mode',
            'broker_shared_secret',
        ]));
        (new logs_o())->add('edgegateway_config', 'global', 1, $user->id, 'EDGEGATEWAY_BROKER_DIAGNOSTICS', 'Tested edge gateway broker config');
        $response->success((new edge_gateway_manager())->diagnoseBrokerConfiguration($diagnosticOptions));
    }
}
