<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Load Composer autoloader if available
$autoload = __DIR__ . '/../vendor/autoload.php';
if (file_exists($autoload)) {
    require $autoload;
}

// Register fallback autoloader for App namespace (compatible with PHP < 8.0)
spl_autoload_register(function ($class) {
    $prefix = 'App\\';
    $baseDir = __DIR__ . '/../app/';
    // Use strpos for PHP < 8.0 compatibility instead of str_starts_with
    $prefixLen = strlen($prefix);
    if (strlen($class) >= $prefixLen && substr($class, 0, $prefixLen) === $prefix) {
        $relative = substr($class, $prefixLen);
        $path = $baseDir . str_replace('\\', '/', $relative) . '.php';
        if (file_exists($path)) {
            require $path;
            return true;
        }
    }
    return false;
});

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

use App\Controllers\AdminController;
use App\Controllers\AdvanceController;
use App\Controllers\ApproverController;
use App\Controllers\AuthController;
use App\Controllers\ClaimController;
use App\Controllers\DashboardController;
use App\Controllers\LeaveController;
use App\Controllers\ProfileController;
use App\Helpers\SessionHelper;

SessionHelper::start();

$route = $_GET['route'] ?? 'login';
$method = $_SERVER['REQUEST_METHOD'];

$auth = new AuthController($pdo);
$dashboard = new DashboardController($pdo);
$advance = new AdvanceController($pdo);
$leave = new LeaveController($pdo);
$claim = new ClaimController($pdo);
$approver = new ApproverController($pdo);
$profile = new ProfileController($pdo);
$admin = new AdminController($pdo);

switch ($route) {
    case 'login':
        if ($method === 'POST') {
            $auth->login();
        } else {
            $auth->showLogin();
        }
        break;
    case 'logout':
        $auth->logout();
        break;
    case 'dashboard':
        $dashboard->index();
        break;
    case 'profile':
        $profile->show();
        break;
    case 'profile/update':
        if ($method === 'POST') {
            $profile->update();
        }
        break;
    case 'advance/create':
        $advance->create();
        break;
    case 'advance/store':
        if ($method === 'POST') {
            $advance->store();
        }
        break;
    case 'leave/create':
        $leave->create();
        break;
    case 'leave/store':
        if ($method === 'POST') {
            $leave->store();
        }
        break;
    case 'claim/create':
        $claim->create();
        break;
    case 'claim/store':
        if ($method === 'POST') {
            $claim->store();
        }
        break;
    case 'approver/pending':
        if ($method === 'POST') {
            $approver->update();
        } else {
            $approver->pending();
        }
        break;
    case 'admin/requests':
        $admin->requests();
        break;
    case 'admin/export':
        $admin->export();
        break;
    case 'admin/users':
        $admin->users();
        break;
    case 'admin/users/store':
        if ($method === 'POST') {
            $admin->storeUser();
        }
        break;
    case 'admin/users/reset':
        if ($method === 'POST') {
            $admin->resetPassword();
        }
        break;
    case 'admin/users/update':
        if ($method === 'POST') {
            $admin->updateUser();
        }
        break;
    case 'admin/employees/store':
        if ($method === 'POST') {
            $admin->storeEmployee();
        }
        break;
    case 'admin/employees/update':
        if ($method === 'POST') {
            $admin->updateEmployee();
        }
        break;
    case 'admin/employees/delete':
        if ($method === 'POST') {
            $admin->deleteEmployee();
        }
        break;
    case 'admin/employees':
        $admin->employees();
        break;
    case 'admin/employees/create':
        $admin->employeeCreate();
        break;
    case 'admin/users/delete':
        if ($method === 'POST') {
            $admin->deleteUser();
        }
        break;
    case 'admin/settings':
        $admin->settings();
        break;
    case 'admin/settings/update':
        if ($method === 'POST') {
            $admin->updateSettings();
        }
        break;
    default:
        http_response_code(404);
        echo 'Route not found';
}

