<?php
/*
** Copyright (C) 2021-2026 initMAX s.r.o.
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/


namespace Modules\uxMAX;

use APP;
use CController as Action;
use Modules\uxMAX\Includes\Compat;
use Modules\uxMAX\Includes\MenuRegistrar;
use Modules\uxMAX\Includes\Presentation;
use Modules\uxMAX\Services\ModuleTranslator;
use Modules\uxMAX\Services\Preferences;
use Zabbix\Core\CModule;

/**
 * uxMAX entry point for Zabbix 6.4 - 7.4 (manifest_version 2).
 *
 * The 6.0/6.2 twin is legacy/Module.php: those frontends accept manifest 1
 * ONLY and have no `assets` block, so the same decisions have to be delivered
 * differently. WHICH assets and WHICH body attributes a request needs is
 * decided in Includes\Presentation, shared by both, so neither line can quietly
 * gain or lose a feature.
 *
 * @property Preferences $preferences
 */
class Module extends CModule {

    public Preferences $preferences;

    public function getAssets(): array {
        $assets = parent::getAssets();
        $action = APP::Component()->router->getAction();
        $preferences = $this->preferences->get();

        foreach (Presentation::scripts($preferences, $action) as $script) {
            $assets['js'][] = $script;
        }

        foreach (Presentation::bodyAttributes($preferences, $action) as $name => $value) {
            zbx_add_post_js('document.documentElement.setAttribute('
                .json_encode($name).', '.json_encode($value).')');
        }

        if (Presentation::needsGeneratedCss($preferences)) {
            // Zabbix resolves a manifest CSS entry against
            // modules/<id>/assets/css/, so four levels up is the web root.
            $assets['css'][] = '../../../../zabbix.php?action=mod.uxmax.css';
        }

        $assets['css'][] = 'uxmax.css';

        return $assets;
    }

    public function init(): void {
        ModuleTranslator::setRelativePath(Compat::relativePath($this));

        $this->preferences = new Preferences($this);

        // init() runs on EVERY authenticated request, so an unguarded menu
        // write here blanks the whole frontend - see Includes\MenuRegistrar.
        if ($this->preferences->isUserOverrideAllowed()) {
            MenuRegistrar::register();
        }
        else {
            MenuRegistrar::registerAdminOnly();
        }
    }

    public function onBeforeAction(Action $action): void {
        if (strpos(get_class($action), __NAMESPACE__) === 0) {
            $action->module = $this;
        }
    }

    public function onTerminate(Action $action): void {
    }
}
