<?php
/**
 * Router for PHP Built-in Server
 * This file handles routing for the admin panel and API endpoints
 */

$requestUri = $_SERVER['REQUEST_URI'];
$requestPath = parse_url($requestUri, PHP_URL_PATH);

// Remove leading slash
$requestPath = ltrim($requestPath, '/');

// Since the server runs from the backend directory, we need to handle paths that include 'backend/'
// When request is /backend/admin/, the path is 'backend/admin/'
// We need to strip 'backend/' to get 'admin/'
if (strpos($requestPath, 'backend/') === 0) {
    $requestPath = substr($requestPath, 8); // Remove 'backend/' (8 characters)
}

// Route admin requests
if (strpos($requestPath, 'admin/') === 0 || $requestPath === 'admin') {
    $adminPath = str_replace('admin/', '', $requestPath);
    $adminPath = ltrim($adminPath, '/');
    
    // If no specific file, show index
    if (empty($adminPath) || $adminPath === '') {
        if (file_exists(__DIR__ . '/admin/index.php')) {
            require __DIR__ . '/admin/index.php';
            return true;
        } else {
            // Determine base path for redirects
            $basePath = strpos($_SERVER['REQUEST_URI'], '/backend/admin') !== false ? '/backend/admin' : '/admin';
            header('Location: ' . $basePath . '/login.php');
            exit;
        }
    }
    
    // Handle admin assets (CSS, JS, etc.) - check this first
    if (strpos($adminPath, 'assets/') === 0) {
        $assetFile = __DIR__ . '/admin/' . $adminPath;
        if (file_exists($assetFile) && is_file($assetFile)) {
            // Set appropriate content type
            $ext = pathinfo($assetFile, PATHINFO_EXTENSION);
            $mimeTypes = [
                'css' => 'text/css',
                'js' => 'application/javascript',
                'png' => 'image/png',
                'jpg' => 'image/jpeg',
                'jpeg' => 'image/jpeg',
                'gif' => 'image/gif',
                'svg' => 'image/svg+xml'
            ];
            if (isset($mimeTypes[$ext])) {
                header('Content-Type: ' . $mimeTypes[$ext]);
            }
            readfile($assetFile);
            return true;
        }
    }
    
    // Check if requested admin file exists
    $adminFile = __DIR__ . '/admin/' . $adminPath;
    if (file_exists($adminFile) && is_file($adminFile)) {
        require $adminFile;
        return true;
    }
    
    // If file not found, show 404
    http_response_code(404);
    echo "404 - Admin file not found: " . htmlspecialchars($adminPath);
    return true;
}

// Route API requests
if (strpos($requestPath, 'api/') === 0) {
    $apiPath = substr($requestPath, 4); // Remove 'api/' prefix
    $apiFile = __DIR__ . '/api/' . $apiPath;
    
    if (file_exists($apiFile) && is_file($apiFile)) {
        require $apiFile;
        return true;
    }
}

// Route uploads (handle both 'uploads/' and 'backend/uploads/')
// After stripping 'backend/', 'backend/uploads/' becomes 'uploads/'
if (strpos($requestPath, 'uploads/') === 0) {
    // Get path after 'uploads/'
    $uploadRelativePath = substr($requestPath, 8); // Remove 'uploads/' prefix (8 characters)
    $uploadFile = __DIR__ . '/uploads/' . $uploadRelativePath;
    
    // Debug: Log the file path (remove in production)
    // error_log("Router: Looking for upload file: " . $uploadFile);
    
    if (file_exists($uploadFile) && is_file($uploadFile)) {
        // Set appropriate content type for images
        $ext = strtolower(pathinfo($uploadFile, PATHINFO_EXTENSION));
        $mimeTypes = [
            'jpg' => 'image/jpeg',
            'jpeg' => 'image/jpeg',
            'png' => 'image/png',
            'gif' => 'image/gif',
            'webp' => 'image/webp',
            'svg' => 'image/svg+xml',
            'mp4' => 'video/mp4',
            'webm' => 'video/webm'
        ];
        if (isset($mimeTypes[$ext])) {
            header('Content-Type: ' . $mimeTypes[$ext]);
        }
        readfile($uploadFile);
        return true;
    } else {
        // File not found - return 404
        http_response_code(404);
        echo "404 - Upload file not found: " . htmlspecialchars($uploadRelativePath);
        return true;
    }
}

// If no route matches, return false to let PHP server handle it
return false;

