<?php
/**
 * RoutexShipping - Main Router (Clean URL Version)
 */

require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/app/Utils.php';
require_once __DIR__ . '/app/Models/Admin.php';
require_once __DIR__ . '/app/Models/Parcel.php';
require_once __DIR__ . '/app/Models/Review.php';
require_once __DIR__ . '/app/Services/EmailService.php';

// Ensure upload directory exists
if (!is_dir(UPLOAD_DIR)) {
    mkdir(UPLOAD_DIR, 0755, true);
}

/*
|--------------------------------------------------------------------------
| Resolve Requested Page from URL
|--------------------------------------------------------------------------
| Supports clean URLs like:
| /routex/
| /routex/track
| /routex/admin
| /routex/admin/dashboard
*/

$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = trim($uri, '/');

// Base folder name (VERY IMPORTANT)
$baseFolder = 'routex';

// Remove base folder from URI
if (strpos($uri, $baseFolder) === 0) {
    $uri = trim(substr($uri, strlen($baseFolder)), '/');
}

// Default route
$page = $uri ?: 'home';

// Normalize admin root
if ($page === 'admin') {
    $page = 'admin/login';
}

// Sanitize page input
$page = preg_replace('/[^a-zA-Z0-9_\/-]/', '', $page);

/*
|--------------------------------------------------------------------------
| Route Map
|--------------------------------------------------------------------------
*/
$routes = [
    'home' => 'pages/home.php',
    'track' => 'pages/track.php',
    'print-invoice' => 'pages/print-invoice.php',
    'about' => 'pages/about.php',
    'services' => 'pages/services.php',
    'contact' => 'pages/contact.php',
    'docs' => 'pages/docs.php',

    // Admin
    'admin/login' => 'pages/admin/login.php',
    'admin/dashboard' => 'pages/admin/dashboard.php',
    'admin/parcels' => 'pages/admin/parcels.php',
    'admin/parcel-create' => 'pages/admin/parcel-create.php',
    'admin/parcel-edit' => 'pages/admin/parcel-edit.php',
    'admin/reviews' => 'pages/admin/reviews.php',
    'admin/review-create' => 'pages/admin/review-create.php',
    'admin/review-edit' => 'pages/admin/review-edit.php',
    'admin/settings' => 'pages/admin/settings.php',
    'admin/email-logs' => 'pages/admin/email-logs.php',
    'admin/profile' => 'pages/admin/profile.php',
    'admin/logout' => 'pages/admin/logout.php',

    // API
    'api/track' => 'api/track.php',
    'api/parcel-create' => 'api/parcel-create.php',
    'api/parcel-update' => 'api/parcel-update.php',
    'api/parcel-delete' => 'api/parcel-delete.php',
    'api/review-create' => 'api/review-create.php',
    'api/review-update' => 'api/review-update.php',
    'api/review-delete' => 'api/review-delete.php',
    'api/email-invoice' => 'api/email-invoice.php',
];

/*
|--------------------------------------------------------------------------
| Admin Protection (Middleware)
|--------------------------------------------------------------------------
*/
$protectedAdminPages = [
    'admin/dashboard',
    'admin/parcels',
    'admin/parcel-create',
    'admin/parcel-edit',
    'admin/reviews',
    'admin/review-create',
    'admin/review-edit',
    'admin/settings',
    'admin/email-logs',
    'admin/profile'
];

if (in_array($page, $protectedAdminPages, true) && !Admin::isLoggedIn()) {
    header('Location: /routex/admin/login');
    exit;
}

/*
|--------------------------------------------------------------------------
| Load Page
|--------------------------------------------------------------------------
*/
$file = $routes[$page] ?? null;

if ($file && file_exists(__DIR__ . '/' . $file)) {
    require_once __DIR__ . '/' . $file;
} else {
    http_response_code(404);
    require_once __DIR__ . '/pages/404.php';
}
