<?php
require_once 'vendor/autoloader.php';
require_once 'app/controllers/Controller.php';
require_once 'app/config.php';
require_once 'app/routes.php';

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

$route = '/';
if (!empty($uri[0])) {
    $route = '/'. $uri[0];
}

if (isset($routes[$route])) {
    $access = explode('@', $routes[$route]);
    $controllerClass = $access[0];
    $method = $access[1];

    $controllerFile = CONTROLLER_DIR. '/'. $controllerClass. '.php';

    if (file_exists($controllerFile)) {
        require_once $controllerFile;
        $controller = new $controllerClass(); // Create an instance of the class
        $controller->$method(); // Call the method
    } else {
        http_response_code(404);
        echo 'Controller not found';
    }
} else {
    http_response_code(404);
    echo 'Route not found';
}