<?php

namespace XcVm\Public\Controllers\Reseller;

use XcVm\Core\Util\AdminHelpers;
use XcVm\Domain\User\GroupService;
use XcVm\Domain\User\UserRepository;/**
 * ResellerUserController — Sub-reseller edit/create.
 *
 * @package XC_VM_Public_Controllers_Reseller
 * @author  Divarion_D <https://github.com/Divarion-D>
 * @copyright 2025-2026 Vateron Media
 * @link    https://github.com/Vateron-Media/XC_VM
 * @license AGPL-3.0 https://www.gnu.org/licenses/agpl-3.0.html
 */

class ResellerUserController extends BaseResellerController
{
    public function index()
    {
        $this->requirePermission();

        $rRequest     = $GLOBALS['rRequest'] ?? [];
        $rUserInfo    = $GLOBALS['rUserInfo'];
        $rPermissions = &$GLOBALS['rPermissions'];
        $rUser        = null;

        if (isset($rRequest['id'])) {
            $rUser = UserRepository::getRegisteredUserById($rRequest['id']);

            if (!$rUser || $rRequest['id'] == $rUserInfo['id']) {
                AdminHelpers::goHome();
            }

            // Remove edited user from reports arrays so they don't appear in owner dropdown
            if (($rKey = array_search($rUser['id'], $rPermissions['all_reports'])) !== false) {
                unset($rPermissions['all_reports'][$rKey]);
            }
            if (($rKey = array_search($rUser['id'], $rPermissions['direct_reports'])) !== false) {
                unset($rPermissions['direct_reports'][$rKey]);
            }
        }

        $rGroups = GroupService::getAll() ?: [];

        $this->setTitle('User');
        $this->render('user', [
            'rUser'   => $rUser,
            'rGroups' => $rGroups,
        ]);
    }
}
