<?php
/** @noinspection PhpIncludeInspection */
// Include essential configuration and function libraries.
require_once 'secrets/config.php'; // Load sensitive configuration such as database credentials.
require_once 'lib/config.php';      // Load general site configuration settings.
require_once 'lib/navigation.php';  // Include functions related to navigation generation.
require_once 'lib/router.php';      // Include routing functionality to manage URL routing.
require_once 'lib/page.php';        // Functions related to page content generation and management.
require_once 'lib/endpoint.php';    // Functions for handling API endpoints.
require_once 'lib/account.php';     // Include user account management functionality.

// Load configuration for the router from the configuration files.
$routerConfig = loadRouterConfig();

// Initialize the router to parse the request URI and determine the requested site/page.
$routerRequest = initRouter();

// Start or resume a session to manage user sessions across requests.
session_start();

// Set default session data if the user is not logged in.
if (!isLoggedIn()) {
setDefaultSessionData();
}

// Handle requests for the sitemap.
if ($routerRequest["site_name"] == "sitemap.xml") {
require "lib/sitemap.php";  // Include sitemap generation functions.
echo generateSitemap();     // Generate and output the sitemap XML.
exit();                     // Stop script execution after sitemap generation.
}

// Handle API type requests by fetching and outputting the endpoint response.
if ($routerRequest["type"] == "api") {
echo getEndpoint($routerRequest["site_name"]);
}
// Handle page type requests by fetching and rendering the page content.
elseif ($routerRequest["type"] == "page") {
echo getPage($routerRequest["site_name"], $routerRequest["page_name"]);
}
