<?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\Actions;

use CController, CControllerResponseData, CWebUser;
use Modules\uxMAX\Includes\RequestGuard;
use Modules\uxMAX\Module;

/**
 * GET controller for per-user uxMAX preferences (currently: color tags
 * override). Accessible to any logged-in non-guest user.
 */
class UserPrefs extends CController {

    use RequestGuard;

    public Module $module;

    protected function init() {
        $this->disableUxmaxRequestGuard();
    }

    protected function checkInput() {
        return true;
    }

    protected function checkPermissions() {
        return !CWebUser::isGuest() && $this->module->preferences->isUserOverrideAllowed();
    }

    protected function doAction() {
        $admin_prefs = $this->module->preferences->get();
        $user_data = $this->module->preferences->getUserOverride();

        $this->setResponse(new CControllerResponseData([
            'override' => (int) $user_data['override'],
            'colortags' => $user_data['colortags'],
            'admin_colortags' => $admin_prefs['colortags'] ?? []
        ]));
    }
}
