<?php
// router.php - Built-in PHP Server Router
// Usage: php -S localhost:8000 router.php

$requested_file = __DIR__ . $_SERVER['REQUEST_URI'];

// Serve static files normally
if (is_file($requested_file) && is_readable($requested_file)) {
    return false;
}

// Route API requests
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

// API routing
if (strpos($path, '/api/auth') === 0) {
    $_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/api/auth/index.php';
    include(__DIR__ . '/api/auth/index.php');
}
elseif (strpos($path, '/api/dashboard') === 0) {
    $_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/api/dashboard/index.php';
    include(__DIR__ . '/api/dashboard/index.php');
}
elseif (strpos($path, '/api/sessions') === 0) {
    $_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/api/sessions/index.php';
    include(__DIR__ . '/api/sessions/index.php');
}
elseif (strpos($path, '/api/stats') === 0) {
    $_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/api/stats/index.php';
    include(__DIR__ . '/api/stats/index.php');
}
elseif (strpos($path, '/api/goals') === 0) {
    $_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/api/goals/index.php';
    include(__DIR__ . '/api/goals/index.php');
}
elseif (strpos($path, '/api/schedules') === 0) {
    $_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/api/schedules/index.php';
    include(__DIR__ . '/api/schedules/index.php');
}
elseif (strpos($path, '/api/settings') === 0) {
    $_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/api/settings/index.php';
    include(__DIR__ . '/api/settings/index.php');
}
else {
    // Serve HTML files or public directory
    if (is_file(__DIR__ . '/public' . $path)) {
        $_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/public' . $path;
        return false;
    }
    
    // Default to index.html
    $_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/public/index.html';
    return false;
}
?>
