<?php
session_set_cookie_params([
    'lifetime' => 7200,
    'path' => '/',
    'domain' => '',
    'secure' => false,
    'httponly' => true,
    'samesite' => 'Lax'
]);
session_name('cix_academy_session');
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

define('CIX_ROOT', dirname(__DIR__));

if (file_exists(__DIR__ . '/../config.php')) {
    require_once __DIR__ . '/../config.php';
} else {
    die('Configuration not found. Please run the installer.');
}

require_once CIX_ROOT . '/app/bootstrap.php';

$config = require __DIR__ . '/../config/app.php';
$pdo = require __DIR__ . '/../config/database.php';

$request_uri = $_SERVER['REQUEST_URI'] ?? '/';
$request_uri = str_replace(dirname($_SERVER['SCRIPT_NAME']), '', $request_uri);
$request_uri = parse_url($request_uri, PHP_URL_PATH);
$request_uri = rtrim($request_uri, '/');

if ($request_uri === '') $request_uri = '/';

$segments = explode('/', trim($request_uri, '/'));
$controller = $segments[0] ?? 'home';
$action = $segments[1] ?? 'index';
$params = array_slice($segments, 2);

function cix_kebab_to_camel(string $value): string {
    $value = strtolower($value);
    return lcfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', $value))));
}

$controllerMap = [
    '' => 'HomeController',
    'home' => 'HomeController',
    'login' => 'AuthController',
    'logout' => 'AuthController',
    'apply' => 'ApplicationController',
    'courses' => 'CourseController',
    'course' => 'CourseController',
    'pricing' => 'PackageController',
    'verify' => 'CertificateController',
    'contact' => 'ContactController',
    'faq' => 'PageController',
    'terms' => 'PageController',
    'privacy' => 'PageController',
    'student' => 'StudentController',
    'teacher' => 'TeacherController',
    'admin' => 'AdminController',
    'api' => 'ApiController'
];

$singleSegmentRoutes = [
    'login' => ['AuthController', 'login', []],
    'logout' => ['AuthController', 'logout', []],
    'apply' => ['ApplicationController', 'index', []],
    'verify' => ['CertificateController', 'verify', []],
    'faq' => ['PageController', 'faq', []],
    'terms' => ['PageController', 'terms', []],
    'privacy' => ['PageController', 'privacy', []],
];

if (count($segments) === 1 && isset($singleSegmentRoutes[$controller])) {
    [$controllerName, $action, $params] = $singleSegmentRoutes[$controller];
} else {
    $controllerName = $controllerMap[$controller] ?? 'HomeController';
    $action = cix_kebab_to_camel($action ?: 'index');
}

$controllerFile = __DIR__ . '/../app/Controllers/' . $controllerName . '.php';

if (!file_exists($controllerFile)) {
    http_response_code(404);
    require_once __DIR__ . '/../app/Views/errors/404.php';
    exit;
}

require_once $controllerFile;
$controllerClass = 'App\\Controllers\\' . $controllerName;
$controllerInstance = new $controllerClass($pdo, $config);

if (!method_exists($controllerInstance, $action)) {
    http_response_code(404);
    require_once __DIR__ . '/../app/Views/errors/404.php';
    exit;
}

call_user_func_array([$controllerInstance, $action], $params);
