<?php
session_start();

$requestUri = $_SERVER['REQUEST_URI'];
if (strpos($requestUri, 'index.php') !== false) {
    $newUrl = str_replace('index.php', '', $requestUri);
    header('Location: ' . $newUrl);
    exit;
}

// Cek apakah ada session
$isLoggedIn = isset($_SESSION['user']) || isset($_SESSION['master']);

// Ambil URL path
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

// Cek apakah path cocok dengan pola /tracking/MP-xxxxxxxx-xxxxx
if (preg_match('#^/tracking/MP-\d{10}-\d{5}$#', $path)) {
    $_GET['id'] = basename($path); // agar bisa diakses di tracking.php
    include 'tracking/tracking.php';
    exit;
}

// Tentukan tampilan berdasarkan session dan path
switch ($path) {
    case '/login':
        if ($isLoggedIn) {
            include 'dashboard.php';
            exit;
        }
        include 'autentikasi/aut_akses.php';
        break;

    case '/lupa_password':
        include 'autentikasi/forget_pass.php';
        break;

    case '/register':
        if ($isLoggedIn) {
            include 'dashboard.php';
            exit;
        }
        include 'autentikasi/register.php';
        break;

    default:
        if ($isLoggedIn) {
            include 'dashboard.php';
        } else {
            include 'web/index.php';
        }
        break;
}
?>
