<?php

session_start();

// SRC is defined as the path to the src/ directory from public/

define('CLASS_MAP', [
	'User' => '/models/user.php',
	'Character' => '/models/character.php',
	'Wallet' => '/models/wallet.php'
]);

// Source libraries
require_once SRC . '/helpers.php';
require_once SRC . '/util/env.php';
require_once SRC . '/util/database.php';
require_once SRC . '/util/auth.php';
require_once SRC . '/util/router.php';
require_once SRC . '/util/components.php';
require_once SRC . '/util/render.php';
require_once SRC . '/util/enums.php';

// Database models
require_once SRC . '/models/session.php';
require_once SRC . '/models/token.php';

// Controllers
require_once SRC . '/controller/char.php';
require_once SRC . '/controller/auth.php';
require_once SRC . '/controller/world.php';
require_once SRC . '/controller/settings.php';
require_once SRC . '/controller/auctions.php';
require_once SRC . '/controller/profile.php';
require_once SRC . '/controller/ui.php';

spl_autoload_register(function (string $class) {
	if (array_key_exists($class, CLASS_MAP)) require_once SRC . CLASS_MAP[$class];
});

// Track the start time of the request
define('START_TIME', microtime(true));

/*
	Load env, set error reporting, etc.
*/
env_load(SRC . '/../.env');

if (env('debug') === 'true') {
	ini_set('display_errors', '1');
	ini_set('display_startup_errors', '1');
	error_reporting(E_ALL);
}

stopwatch_start('bootstrap'); // Start the bootstrap stopwatch

// Generate a new CSRF token. (if one doesn't exist, that is)
csrf();

// Have global counters for queries
$GLOBALS['queries'] = 0;
$GLOBALS['query_time'] = 0;

// Set the default page layout
page_layout('basic');

// Run auth_check to see if we're logged in, since it populates the user data in SESSION
auth_check();

stopwatch_stop('bootstrap'); // Stop the bootstrap stopwatch
