<?php
/**
 * Front Controller - Routes all requests
 *
 * This handles URL routing when .htaccess rewrites aren't available.
 * Supports clean URLs like /dashboard/, /login/, etc.
 */

declare(strict_types=1);

// Migration redirect: the authoritative Notifier app now lives on RunCloud.
$newBaseUrl = 'https://notifications.tonkios.com/notifier';
$requestUri = $_SERVER['REQUEST_URI'] ?? '/notifier/';
$path = parse_url($requestUri, PHP_URL_PATH) ?? '/notifier/';
$query = parse_url($requestUri, PHP_URL_QUERY);
$suffix = str_starts_with($path, '/notifier') ? substr($path, strlen('/notifier')) : $path;
$suffix = $suffix === '' ? '/' : $suffix;
$target = rtrim($newBaseUrl, '/') . $suffix;
if (is_string($query) && $query !== '') {
    $target .= '?' . $query;
}
header('Location: ' . $target, true, 302);
exit;

// Get the request path relative to this directory
$requestUri = $_SERVER['REQUEST_URI'] ?? '/';
$scriptName = dirname($_SERVER['SCRIPT_NAME']);

// Remove base path from request URI
$path = str_replace($scriptName, '', $requestUri);
$path = trim(parse_url($path, PHP_URL_PATH) ?? '', '/');

// Remove trailing slash for matching
$path = rtrim($path, '/');

// Default to dashboard if empty
if ($path === '' || $path === 'index.php') {
    $path = 'dashboard';
}

// Route mapping
$routes = [
    'login' => 'login.php',
    'logout' => 'logout.php',
    'dashboard' => 'dashboard.php',
    'notifications' => 'notifications.php',
    'notification-edit' => 'notification-edit.php',
    'categories' => 'categories.php',
    'history' => 'history.php',
    'console' => 'console.php',
];

// Handle notifications/new and notifications/edit/:id
if (preg_match('#^notifications/new$#', $path)) {
    require __DIR__ . '/notification-edit.php';
    exit;
}

if (preg_match('#^notifications/edit/(\d+)$#', $path, $matches)) {
    $_GET['id'] = $matches[1];
    require __DIR__ . '/notification-edit.php';
    exit;
}

if (preg_match('#^confirm/([A-Fa-f0-9]+)$#', $path, $matches)) {
    $_GET['token'] = $matches[1];
    require __DIR__ . '/confirm.php';
    exit;
}

// Check if route exists
if (isset($routes[$path])) {
    $file = __DIR__ . '/' . $routes[$path];
    if (file_exists($file)) {
        require $file;
        exit;
    }
}

// Fallback: check if it's a direct PHP file request
if (file_exists(__DIR__ . '/' . $path . '.php')) {
    require __DIR__ . '/' . $path . '.php';
    exit;
}

// 404 Not Found
http_response_code(404);
echo '<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>404 - Not Found</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-slate-50 min-h-screen flex items-center justify-center">
    <div class="text-center">
        <h1 class="text-6xl font-bold text-slate-300">404</h1>
        <p class="text-slate-500 mt-2">Page not found</p>
        <a href="dashboard/" class="inline-block mt-4 text-indigo-500 hover:text-indigo-600">Go to Dashboard</a>
    </div>
</body>
</html>';
