<?php
// ============================================================
// MetLAB — Front Controller
// ============================================================
require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/helpers/functions.php';
// Load interfaces early so any controller can implement them
require_once __DIR__ . '/helpers/ILabForm.php';
require_once __DIR__ . '/helpers/ILabCertificate.php';

startSession();

$page   = $_GET['page']   ?? 'dashboard';
$action = $_GET['action'] ?? 'index';

// Public pages (no auth required)
$publicPages = ['login', 'logout', 'otp'];

if (!in_array($page, $publicPages)) {
    requireLogin();
}

// Route map: page => controller file
$routes = [
    // Auth
    'login'        => 'controllers/AuthController.php',
    'logout'       => 'controllers/AuthController.php',
    'otp'          => 'controllers/AuthController.php',
    // Dashboard
    'dashboard'    => 'controllers/DashboardController.php',
    // Admin
    'users'        => 'controllers/UserController.php',
    'labs'         => 'controllers/LabController.php',
    'logs'         => 'controllers/LogController.php',
    // Front Desk
    'companies'    => 'controllers/CompanyController.php',
    'contacts'     => 'controllers/ContactController.php',
    'equipment'    => 'controllers/EquipmentController.php',
    'requests'     => 'controllers/RequestController.php',
    // Lab
    'workform'     => 'controllers/WorkFormController.php',
    'certificates' => 'controllers/CertificateController.php',
    // Profile
    'profile'      => 'controllers/ProfileController.php',
];

if (!isset($routes[$page])) {
    http_response_code(404);
    include __DIR__ . '/views/shared/404.php';
    exit;
}

require_once __DIR__ . '/' . $routes[$page];
