<?php

/*
	Setup
*/
define('SRC', __DIR__ . '/../src');
require_once SRC . '/bootstrap.php';

$r = new Router;

/*
	Home
*/
$r->get('/', function () {
	if (user()) must_have_character();
	$GLOBALS['active_nav_tab'] = 'home';
	echo render('layouts/basic', ['view' => 'pages/home']);
});

/*
	Auth
*/
$r->get('/auth/register', 'auth_controller_register_get');
$r->post('/auth/register', 'auth_controller_register_post');
$r->get('/auth/login', 'auth_controller_login_get');
$r->post('/auth/login', 'auth_controller_login_post');
$r->post('/auth/logout', 'auth_controller_logout_post');

/*
	Characters
*/
$r->get('/characters', 'char_controller_list_get');
$r->post('/characters', 'char_controller_list_post');
$r->get('/character/create-first', 'char_controller_create_first_get');
$r->post('/character/create', 'char_controller_create_post');
$r->post('/character/delete', 'char_controller_delete_post');

/*
	World
*/
$r->get('/world', 'world_controller_get');
$r->post('/move', 'world_controller_move_post');

/*
	Profile
*/
$r->get('/profile', 'profile_controller_get');
$r->get('/profile/:id', 'profile_controller_show_get');

/*
	Settings
*/
$r->get('/settings', 'settings_controller_get');

/*
	Auctions
*/
$r->get('/auctions', 'auctions_controller_get');

/*
	UI
*/
router_post($r, '/ui/stats', 'ui_contoller_stats_post');

/*
	Testing
*/
if (env('debug')) {
	$r->get('/give_silver/:x', function (int $amt) {
		auth_only_and_must_have_character();
		wallet()->give(Currency::Silver, $amt);
		redirect('/');
	});

	$r->get('/take_silver/:x', function (int $amt) {
		auth_only_and_must_have_character();
		wallet()->take(Currency::Silver, $amt);
		redirect('/');
	});
}

/*
	Router
*/
// [code, handler, params]
stopwatch_start('router');
$l = $r->lookup($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
stopwatch_stop('router');

stopwatch_start('handler');
if ($l['code'] !== 200) error_response($l['code']);
$l['handler'](...$l['params'] ?? []);
stopwatch_stop('handler');

/*
	Cleanup
*/
clear_flashes();
