<?php
/**
 * TekDek Documentation Site - Main Router
 * 
 * All requests route through here via .htaccess.
 */
define('TEKDEK', true);

require_once __DIR__ . '/config.php';
require_once __DIR__ . '/includes/functions.php';

// --- Determine requested page ---
$request_uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$base = rtrim(SITE_BASE_URL, '/');
$path = ($base !== '') ? substr($request_uri, strlen($base)) : $request_uri;
$path = trim($path, '/');

// Default to home
$current_page = ($path === '') ? 'home' : $path;

// --- Validate page exists ---
if (!isset($MENU[$current_page])) {
    $current_page = '404';
    http_response_code(404);
}

// --- Render ---
include __DIR__ . '/includes/top.php';

$page_file = __DIR__ . '/pages/' . $current_page . '.php';
if (file_exists($page_file)) {
    include $page_file;
} elseif ($current_page === '404') {
    include __DIR__ . '/pages/404.php';
} else {
    // Fallback: try loading markdown content
    $content = load_content($current_page);
    if ($content) {
        echo '<article class="page-content">' . $content . '</article>';
    } else {
        include __DIR__ . '/pages/404.php';
    }
}

include __DIR__ . '/includes/bottom.php';
