<?php

use XcVm\Core\Auth\AuthRepository;
use XcVm\Core\Auth\SessionManager;
use XcVm\Core\Http\RequestManager;
use XcVm\Core\Util\AdminHelpers;
use XcVm\Core\Util\NetworkUtils;
use XcVm\Domain\User\UserRepository;/**
 * Reseller functions bootstrap.
 *
 * Extracted from reseller/functions.php for Front Controller use.
 * Loads includes/admin.php, then sets up $rUserInfo, $rPermissions
 * and validates reseller session integrity.
 *
 * @package XC_VM_Infrastructure_Bootstrap
 * @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
 */

if (!defined('MAIN_HOME')) {
	define('MAIN_HOME', '/home/xc_vm/');
}

require_once MAIN_HOME . 'bootstrap.php';
XC_Bootstrap::boot(XC_Bootstrap::CONTEXT_ADMIN);

if ($rMobile) {
	$rSettings['js_navigate'] = 0;
}

if (isset($_SESSION['reseller'])) {
	$rUserInfo = UserRepository::getRegisteredUserById($_SESSION['reseller']);

	if (strlen($rUserInfo['timezone'] ?? '') > 0) {
		date_default_timezone_set($rUserInfo['timezone']);
	}

	setcookie('hue', $rUserInfo['hue'] ?? '', time() + 604800);
	setcookie('theme', $rUserInfo['theme'] ?? '', time() + 604800);
	$language::setLanguage($rUserInfo['lang']);

	$rPermissions = array_merge(AuthRepository::getPermissions($rUserInfo['member_group_id']), AuthRepository::getGroupPermissions($rUserInfo['id']));
	$rPermissions['direct_reports'] = $rPermissions['direct_reports'] ?? [];
	$rPermissions['all_reports'] = $rPermissions['all_reports'] ?? [];
	$rPermissions['stream_ids'] = $rPermissions['stream_ids'] ?? [];
	$rPermissions['category_ids'] = $rPermissions['category_ids'] ?? [];
	$rPermissions['series_ids'] = $rPermissions['series_ids'] ?? [];
	$rPermissions['subresellers'] = $rPermissions['subresellers'] ?? [];
	$rUserInfo['reports'] = array_map('intval', array_merge(array($rUserInfo['id']), $rPermissions['all_reports']));
	$rIP = NetworkUtils::getUserIP();
	$rIPMatch = ($rSettings['ip_subnet_match'] ? implode('.', array_slice(explode('.', $_SESSION['rip']), 0, -1)) == implode('.', array_slice(explode('.', $rIP), 0, -1)) : $_SESSION['rip'] == $rIP);

	if (!$rUserInfo || !$rPermissions || !$rPermissions['is_reseller'] || !$rIPMatch && $rSettings['ip_logout'] || $_SESSION['rverify'] != md5($rUserInfo['username'] . '||' . $rUserInfo['password'])) {
		unset($rUserInfo, $rPermissions);

		SessionManager::clearContext('reseller');
		header('Location: ./index');

		exit();
	}
	if ($_SESSION['rip'] != $rIP && !$rSettings['ip_logout']) {
		$_SESSION['rip'] = $rIP;
	}
}

if (isset(RequestManager::getAll()['status'])) {
	$_STATUS = intval(RequestManager::getAll()['status']);
	$rArgs = RequestManager::getAll();
	unset($rArgs['status']);
	$customScript = AdminHelpers::setArgs($rArgs);
}
