<?php

// Force HTTPS
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
    // Check if the request is already HTTPS
    if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
        $_SERVER['HTTPS'] = 'on';
    } else {
        // Redirect to HTTPS
        $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: ' . $redirect);
        exit();
    }
}

$uri = parse_url($_SERVER['REQUEST_URI'])['path'];

 $routes = [
  // Home Page
  '/home' => '../controllers/dashboard/dashboard.view.php',
  '/lastestalerts' => '../controllers/dashboard/lastestalerts.php',
  '/hours_charts' => '../controllers/dashboard/hours_charts.php',
  '/checklists_api' => '../controllers/dashboard/checklists_api.php',
  '/dashboard_api' => '../controllers/dashboard/dashboard_api.php',
  '/inquire' => '../controllers/dashboard/inquire.php',
  '/profile' => '../controllers/dashboard/profile.php',
  '/statusupdate' => '../controllers/dashboard/statusupdate.php',
  '/onelinedevices' => '../controllers/dashboard/onelinedevices.php',
  '/alertnotificationsummary' => '../controllers/dashboard/alertnotificationsummary.php',
  

  // Login Page
  '/' => '../controllers/loginLogout/login.php',
  '/submit-login' => '../controllers/loginLogout/authenticate.php',
  '/logout' => '../controllers/loginLogout/logout.php',
  '/forgotpassword' => '../controllers/loginLogout/forgotpassword.php',
  '/twofactor' => '../controllers/loginLogout/twofactor.php',
  '/resetpassword' => '../controllers/loginLogout/resetpassword.php',
  '/welcomeemail' => '../controllers/loginLogout/welcomeemail.php',
  '/microsoftoauth' => '../controllers/loginLogout/microsoftoauth.php',

  // Alert Notifications Page / Notifications
  '/alertsnotificationpage' => '../controllers/alertNotifications/alertsnotificationpage.php',
  '/alertnotifications' => '../controllers/alertNotifications/alertnotifications.php',
  '/notifications_table_realtime' => '../controllers/alertNotifications/notifications_table_realtime.php',
  '/alertnotificationspop' => '../controllers/alertNotifications/alertnotificationspop.php',
  '/notificationsrealtimeheader' => '../controllers/alertNotifications/notificationsrealtimeheader.php',

  // Live View Page
  '/liveview' => '../controllers/liveView/liveview.php',
  '/liveviewvideo' => '../controllers/liveView/liveviewvideo.php',

  // View Device Page
  '/viewdevice' => '../controllers/viewDevices/viewdevice.php',
  '/oneviewvideoget' => '../controllers/viewDevices/oneviewvideoget.php',
  '/ch1videoget' => '../controllers/viewDevices/ch1videoget.php',
  '/ch2videoget' => '../controllers/viewDevices/ch2videoget.php',
  '/ch3videoget' => '../controllers/viewDevices/ch3videoget.php',
  '/ch4videoget' => '../controllers/viewDevices/ch4videoget.php',
  '/viewDevicesAlert' => '../controllers/viewDevices/viewdevicealerttab.php',
  '/viewDevicesSessions' => '../controllers/viewDevices/viewdevicesessionstab.php',
  '/viewDevicesChecklistStart' => '../controllers/viewDevices/viewdevicechecklisttab.php',
  '/viewDevicesRecordedVideos' => '../controllers/viewDevices/viewdevicerecordedvideostab.php',
  '/devicevideolookup' => '../controllers/viewDevices/devicevideolookup.php',
  '/viewDevicesDeviceConfig' => '../controllers/viewDevices/viewdeviceconfigtab.php',
  '/updatedvrsettings' => '../controllers/viewDevices/updatedvrsettings.php',

  // Reports Page
  '/reports' => '../controllers/reports/reports.php',
  '/reports/alertsnotifications' => '../controllers/reports/alertsnotifications.php',
  '/reports/devicessessions' => '../controllers/reports/devicessessions.php',
  '/reports/checkliststart' => '../controllers/reports/checkliststart.php',
  '/reports/securitylog' => '../controllers/reports/securitylog.php',
  '/reports/log' => '../controllers/reports/log.php',

  // Admin Pages
  '/admin/overview' => '../controllers/admin/index.php',
  '/admin/clients' => '../controllers/admin/clients/clients.php',
  '/admin/client' => '../controllers/admin/clients/client.php',
  '/admin/accounts' => '../controllers/admin/accounts/accounts.php',
  '/admin/account' => '../controllers/admin/accounts/account.php',
  '/admin/groups' => '../controllers/admin/groups/groups.php',
  '/admin/group' => '../controllers/admin/groups/group.php',
  '/admin/subgroups' => '../controllers/admin/groups/subgroups.php',
  '/admin/subgrouplocations' => '../controllers/admin/groups/subgrouplocations.php',
  '/admin/groupoperators' => '../controllers/admin/groups/groupoperators.php',
  '/admin/savechecklist' => '../controllers/admin/groups/savechecklist.php',
  '/admin/devices' => '../controllers/admin/devices/devices.php',
  '/admin/device' => '../controllers/admin/devices/device.php',
  '/admin/device/ajaxData' => '../controllers/admin/devices/ajaxData.php',
  '/admin/devices/exportdevices' => '../controllers/admin/devices/exportdevices.php',
  '/admin/operators' => '../controllers/admin/operators/operators.php',
  '/admin/operator' => '../controllers/admin/operators/operator.php',
  '/admin/operator/ajaxData' => '../controllers/admin/operators/ajaxData.php',
  '/admin/operator/exportoperators' => '../controllers/admin/operators/exportoperators.php',

  // Server Pages
  '/admin/systememail' => '../controllers/admin/settings/systememail.php',
  '/admin/whatsappsettings' => '../controllers/admin/settings/whatsappsettings.php',
  '/admin/generalsettings' => '../controllers/admin/settings/generalsettings.php',
  '/admin/branding' => '../controllers/admin/settings/branding.php',
  '/admin/update' => '../controllers/admin/settings/update.php',
  '/admin/upload' => '../controllers/admin/settings/upload.php',
  '/admin/download' => '../controllers/admin/settings/download.php',
  '/admin/checkforupdate' => '../controllers/admin/settings/checkforupdate.php',
  '/admin/activation' => '../controllers/admin/settings/activation.php',
  '/admin/agreement' => '../controllers/admin/settings/agreement.php',

  // Email Preview Video Page
  '/playemailvideo' => '../controllers/emails/playemailvideo.php',

  // Services
  '/downloadpdf' => '../controllers/services/downloadpdf.php',
  '/downloadrecordfile' => '../controllers/services/downloadrecordfile.php',
  '/playbackvideo' => '../controllers/services/playbackvideo.php',
  '/playbackvideohome' => '../controllers/services/playbackvideohome.php',
  '/preparevideo' => '../controllers/services/preparevideo.php',
  '/playbackvideoemail' => '../controllers/services/playbackvideoemail.php',


  // Check Online Update
  '/ver' => '../controllers/dashboard/ver.php',

];


if (array_key_exists($uri, $routes)) {

  require $routes[$uri];

} else {

  abort(404);

} 

function abort($code) {

  http_response_code(404);

  require "../views/{$code}.php";

  die();

}