<?php
// TSHEN - lightweight PHP app entry point & router for PHP -S
// Serve static assets when running with built-in server
if (php_sapi_name() === 'cli-server') {
    $path = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
    // If running in a subdirectory (e.g. /tshen) adjust path for built-in server static serving
    $base = rtrim(dirname($_SERVER['SCRIPT_NAME'] ?? ''), '/\\');
    if ($base === '/' || $base === '\\') $base = '';
    if ($base !== '' && strpos($path, $base) === 0) {
        $path = substr($path, strlen($base));
        if ($path === '') $path = '/';
    }
    $file = __DIR__ . $path;
    if ($path && strpos($path, '/public/') === 0 && is_file($file)) {
        return false; // let server serve static file
    }
}

require __DIR__ . '/src/bootstrap.php';

use Tshen\Router;

$router = new Router();

// Public routes
$router->get('/', 'HomeController@index');
$router->get('/about', 'HomeController@about');
$router->get('/login', 'AuthController@login');
$router->post('/login', 'AuthController@doLogin');
$router->get('/register', 'AuthController@register');
$router->post('/register', 'AuthController@doRegister');
$router->get('/logout', 'AuthController@logout');

// Class listings and booking
$router->get('/classes', 'ClassController@index');
$router->get('/classes/view', 'ClassController@show');
$router->post('/classes/book', 'ClassController@book');

// Dashboards
$router->get('/dashboard', 'DashboardController@index');
$router->get('/profile', 'DashboardController@profile');
$router->post('/profile/upload-picture', 'UserController@uploadProfilePicture');
$router->get('/profile/upload-picture', 'UserController@uploadProfilePicture');
$router->post('/dashboard/cancel', 'DashboardController@cancel');
$router->post('/dashboard/purchase', 'DashboardController@purchase');
$router->post('/dashboard/redeem', 'DashboardController@redeem');
$router->get('/dashboard/bookings', 'DashboardController@bookings');

// Studio dashboard
$router->get('/studio', 'StudioController@index');
$router->post('/studio/classes/create', 'StudioController@create');
$router->post('/studio/classes/update', 'StudioController@update');

// Admin
$router->get('/admin', 'AdminController@index');
$router->post('/admin/credits/code', 'AdminController@issueCode');

// Store
$router->get('/store', 'StoreController@index');
$router->post('/store/checkout', 'StoreController@checkout');

// Contact
$router->get('/contact', 'ContactController@index');
$router->post('/contact', 'ContactController@send');

// Dispatch
$router->dispatch();
?>



<?php
// ...existing code...

// Replace the previous safe_include + loops with this improved helper + usage

function safe_include_with_layout($candidate, $root) {
    if (!file_exists($candidate)) return false;

    // Prevent directory traversal
    $realRoot = realpath($root);
    $realFile = realpath($candidate);
    if (!$realFile || strpos($realFile, $realRoot) !== 0) return false;

    // Read a small prefix to detect if file is a full HTML document
    $fh = fopen($realFile, 'r');
    $prefix = $fh ? fread($fh, 2048) : '';
    if ($fh) fclose($fh);

    $isFullHtml = (stripos($prefix, '<!doctype') !== false) || (stripos($prefix, '<html') !== false);

    if ($isFullHtml) {
        include $realFile;
        return true;
    }

    // Try to include layout header/footer if present, otherwise include file directly
    $header = $root . '/views/layout/header.php';
    $footer = $root . '/views/layout/footer.php';

    if (file_exists($header)) include $header;
    include $realFile;
    if (file_exists($footer)) include $footer;

    return true;
}

?>