<?php

declare(strict_types=1);

if (PHP_SAPI === 'cli-server') {
    $requestPath = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/';
    $staticFile = __DIR__ . $requestPath;
    if ($requestPath !== '/' && is_file($staticFile)) {
        return false;
    }
}

require dirname(__DIR__) . '/config/config.php';

session_name(SESSION_NAME);
ini_set('session.use_strict_mode', '1');
ini_set('session.cookie_httponly', '1');
ini_set('session.cookie_samesite', 'Lax');
if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || (($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https')) {
    ini_set('session.cookie_secure', '1');
}
session_start();

require dirname(__DIR__) . '/app/Core/helpers.php';
require app_path('Core/Database.php');
require app_path('Core/Controller.php');
require app_path('Core/Auth.php');
require app_path('Core/Csrf.php');
require app_path('Core/App.php');
require app_path('Core/SimplePdf.php');
require app_path('Services/GeminiService.php');
require app_path('Services/AIAutomationService.php');

require app_path('Models/BaseModel.php');
require app_path('Models/Carrier.php');
require app_path('Models/Driver.php');
require app_path('Models/Truck.php');
require app_path('Models/Trailer.php');
require app_path('Models/Broker.php');
require app_path('Models/Load.php');
require app_path('Models/Document.php');
require app_path('Models/Invoice.php');
require app_path('Models/Settlement.php');
require app_path('Models/Maintenance.php');
require app_path('Models/User.php');
require app_path('Models/Accounting.php');
require app_path('Models/BorderCrossing.php');
require app_path('Models/TeamTask.php');
require app_path('Models/Notification.php');
require app_path('Models/TemperatureLog.php');
require app_path('Models/Fuel.php');
require app_path('Models/HoursOfService.php');
require app_path('Models/AIAutomation.php');

require app_path('Controllers/PublicController.php');
require app_path('Controllers/AuthController.php');
require app_path('Controllers/DashboardController.php');
require app_path('Controllers/CarrierController.php');
require app_path('Controllers/CarrierPortalController.php');
require app_path('Controllers/DriverController.php');
require app_path('Controllers/TruckController.php');
require app_path('Controllers/TrailerController.php');
require app_path('Controllers/BrokerController.php');
require app_path('Controllers/LoadController.php');
require app_path('Controllers/DocumentController.php');
require app_path('Controllers/InvoiceController.php');
require app_path('Controllers/SettlementController.php');
require app_path('Controllers/MaintenanceController.php');
require app_path('Controllers/UserController.php');
require app_path('Controllers/DriverPortalController.php');
require app_path('Controllers/ReportController.php');
require app_path('Controllers/AccountingController.php');
require app_path('Controllers/BorderCrossingController.php');
require app_path('Controllers/TrackingController.php');
require app_path('Controllers/SettingsController.php');
require app_path('Controllers/TaskController.php');
require app_path('Controllers/NotificationController.php');
require app_path('Controllers/SearchController.php');
require app_path('Controllers/FuelController.php');
require app_path('Controllers/HoursOfServiceController.php');
require app_path('Controllers/AIController.php');
require app_path('Controllers/AIAutomationController.php');

$app = new App();

$app->get('/', [PublicController::class, 'home']);
$app->get('/dashboard', [DashboardController::class, 'index']);

$app->get('/login', [AuthController::class, 'showLogin']);
$app->post('/login', [AuthController::class, 'login']);
$app->post('/logout', [AuthController::class, 'logout']);

$app->get('/carriers', [CarrierController::class, 'index']);
$app->get('/carriers/create', [CarrierController::class, 'create']);
$app->post('/carriers/store', [CarrierController::class, 'store']);
$app->get('/carriers/show', [CarrierController::class, 'show']);
$app->get('/carriers/edit', [CarrierController::class, 'edit']);
$app->post('/carriers/update', [CarrierController::class, 'update']);
$app->post('/carriers/delete', [CarrierController::class, 'delete']);

$app->get('/carrier', [CarrierPortalController::class, 'index']);
$app->get('/carrier/loads/show', [CarrierPortalController::class, 'showLoad']);

$app->get('/drivers', [DriverController::class, 'index']);
$app->get('/drivers/create', [DriverController::class, 'create']);
$app->post('/drivers/store', [DriverController::class, 'store']);
$app->get('/drivers/edit', [DriverController::class, 'edit']);
$app->post('/drivers/update', [DriverController::class, 'update']);
$app->post('/drivers/delete', [DriverController::class, 'delete']);

$app->get('/trucks', [TruckController::class, 'index']);
$app->get('/trucks/create', [TruckController::class, 'create']);
$app->post('/trucks/store', [TruckController::class, 'store']);
$app->get('/trucks/edit', [TruckController::class, 'edit']);
$app->post('/trucks/update', [TruckController::class, 'update']);
$app->post('/trucks/delete', [TruckController::class, 'delete']);

$app->get('/trailers', [TrailerController::class, 'index']);
$app->get('/trailers/create', [TrailerController::class, 'create']);
$app->post('/trailers/store', [TrailerController::class, 'store']);
$app->get('/trailers/edit', [TrailerController::class, 'edit']);
$app->post('/trailers/update', [TrailerController::class, 'update']);
$app->post('/trailers/delete', [TrailerController::class, 'delete']);

$app->get('/brokers', [BrokerController::class, 'index']);
$app->get('/brokers/create', [BrokerController::class, 'create']);
$app->post('/brokers/store', [BrokerController::class, 'store']);
$app->get('/brokers/edit', [BrokerController::class, 'edit']);
$app->post('/brokers/update', [BrokerController::class, 'update']);
$app->post('/brokers/delete', [BrokerController::class, 'delete']);

$app->get('/loads', [LoadController::class, 'index']);
$app->get('/loads/board', [LoadController::class, 'board']);
$app->get('/loads/calendar', [LoadController::class, 'calendar']);
$app->get('/loads/create', [LoadController::class, 'create']);
$app->post('/loads/store', [LoadController::class, 'store']);
$app->get('/loads/edit', [LoadController::class, 'edit']);
$app->post('/loads/update', [LoadController::class, 'update']);
$app->post('/loads/status', [LoadController::class, 'updateStatus']);
$app->post('/loads/temperature', [LoadController::class, 'storeTemperature']);
$app->post('/loads/delete', [LoadController::class, 'delete']);

$app->get('/documents', [DocumentController::class, 'index']);
$app->get('/documents/create', [DocumentController::class, 'create']);
$app->post('/documents/store', [DocumentController::class, 'store']);
$app->get('/documents/download', [DocumentController::class, 'download']);
$app->post('/documents/delete', [DocumentController::class, 'delete']);

$app->get('/invoices', [InvoiceController::class, 'index']);
$app->get('/invoices/create', [InvoiceController::class, 'create']);
$app->post('/invoices/store', [InvoiceController::class, 'store']);
$app->get('/invoices/edit', [InvoiceController::class, 'edit']);
$app->post('/invoices/update', [InvoiceController::class, 'update']);
$app->get('/invoices/view', [InvoiceController::class, 'viewInvoice']);
$app->get('/invoices/pdf', [InvoiceController::class, 'pdf']);
$app->get('/invoices/payment-receipt', [InvoiceController::class, 'paymentReceipt']);
$app->post('/invoices/payment', [InvoiceController::class, 'addPayment']);
$app->post('/invoices/status', [InvoiceController::class, 'status']);
$app->post('/invoices/delete', [InvoiceController::class, 'delete']);

$app->get('/settlements', [SettlementController::class, 'index']);
$app->get('/settlements/create', [SettlementController::class, 'create']);
$app->post('/settlements/store', [SettlementController::class, 'store']);
$app->post('/settlements/paid', [SettlementController::class, 'markPaid']);
$app->post('/settlements/delete', [SettlementController::class, 'delete']);
$app->get('/settlements/pdf', [SettlementController::class, 'pdf']);

$app->get('/maintenance', [MaintenanceController::class, 'index']);
$app->get('/maintenance/create', [MaintenanceController::class, 'create']);
$app->post('/maintenance/store', [MaintenanceController::class, 'store']);
$app->post('/maintenance/delete', [MaintenanceController::class, 'delete']);

$app->get('/users', [UserController::class, 'index']);
$app->get('/users/create', [UserController::class, 'create']);
$app->post('/users/store', [UserController::class, 'store']);
$app->get('/users/edit', [UserController::class, 'edit']);
$app->post('/users/update', [UserController::class, 'update']);
$app->post('/users/delete', [UserController::class, 'delete']);

$app->get('/driver/loads', [DriverPortalController::class, 'index']);
$app->get('/driver/loads/show', [DriverPortalController::class, 'show']);
$app->post('/driver/loads/status', [DriverPortalController::class, 'status']);
$app->post('/driver/loads/location', [DriverPortalController::class, 'location']);
$app->post('/driver/loads/stop-complete', [DriverPortalController::class, 'completeStop']);
$app->post('/driver/loads/upload', [DriverPortalController::class, 'upload']);
$app->post('/driver/loads/temperature', [DriverPortalController::class, 'recordTemperature']);

$app->get('/reports', [ReportController::class, 'index']);

$app->get('/hos', [HoursOfServiceController::class, 'index']);
$app->get('/hos/driver', [HoursOfServiceController::class, 'driver']);
$app->get('/hos/profile', [HoursOfServiceController::class, 'profile']);
$app->post('/hos/profile/save', [HoursOfServiceController::class, 'saveProfile']);
$app->post('/hos/events/store', [HoursOfServiceController::class, 'storeEvent']);
$app->post('/hos/log/update', [HoursOfServiceController::class, 'updateLog']);
$app->post('/hos/certify', [HoursOfServiceController::class, 'certify']);
$app->post('/hos/events/edit-request', [HoursOfServiceController::class, 'requestEdit']);
$app->post('/hos/edits/review', [HoursOfServiceController::class, 'reviewEdit']);
$app->get('/hos/inspection', [HoursOfServiceController::class, 'inspection']);
$app->get('/hos/pdf', [HoursOfServiceController::class, 'pdf']);


$app->get('/fuel', [FuelController::class, 'index']);
$app->post('/fuel/allocations/store', [FuelController::class, 'storeAllocation']);
$app->post('/fuel/allocations/status', [FuelController::class, 'allocationStatus']);
$app->post('/fuel/transactions/store', [FuelController::class, 'storeTransaction']);
$app->post('/fuel/transactions/status', [FuelController::class, 'transactionStatus']);


$app->get('/ai', [AIController::class, 'index']);
$app->post('/ai/settings', [AIController::class, 'saveSettings']);
$app->post('/ai/test', [AIController::class, 'test']);
$app->post('/ai/assistant', [AIController::class, 'assistant']);
$app->post('/ai/document', [AIController::class, 'analyzeDocument']);
$app->post('/ai/fuel-recommendation', [AIController::class, 'fuelRecommendation']);
$app->get('/ai/automation', [AIAutomationController::class, 'index']);
$app->post('/ai/automation/rules/save', [AIAutomationController::class, 'saveRule']);
$app->post('/ai/automation/rules/toggle', [AIAutomationController::class, 'toggleRule']);
$app->post('/ai/automation/run', [AIAutomationController::class, 'run']);
$app->post('/ai/automation/actions/approve', [AIAutomationController::class, 'approveAction']);
$app->post('/ai/automation/actions/reject', [AIAutomationController::class, 'rejectAction']);

$app->get('/accounting', [AccountingController::class, 'index']);
$app->get('/accounting/expenses', [AccountingController::class, 'expenses']);
$app->get('/accounting/expenses/create', [AccountingController::class, 'createExpense']);
$app->post('/accounting/expenses/store', [AccountingController::class, 'storeExpense']);
$app->post('/accounting/expenses/status', [AccountingController::class, 'expenseStatus']);
$app->get('/accounting/vendors', [AccountingController::class, 'vendors']);
$app->post('/accounting/vendors/store', [AccountingController::class, 'storeVendor']);
$app->get('/accounting/bills', [AccountingController::class, 'bills']);
$app->post('/accounting/bills/store', [AccountingController::class, 'storeBill']);
$app->post('/accounting/bills/pay', [AccountingController::class, 'payBill']);
$app->get('/accounting/banking', [AccountingController::class, 'banking']);
$app->post('/accounting/banking/account', [AccountingController::class, 'storeBankAccount']);
$app->post('/accounting/banking/transaction', [AccountingController::class, 'storeTransaction']);
$app->post('/accounting/banking/reconcile', [AccountingController::class, 'reconcileTransaction']);
$app->get('/accounting/journals', [AccountingController::class, 'journals']);
$app->post('/accounting/journals/store', [AccountingController::class, 'storeJournal']);
$app->get('/accounting/factoring', [AccountingController::class, 'factoring']);
$app->post('/accounting/factoring/store', [AccountingController::class, 'storeFactoring']);
$app->get('/accounting/reports', [AccountingController::class, 'reports']);
$app->get('/accounting/customer-statement', [AccountingController::class, 'customerStatement']);

$app->get('/borders', [BorderCrossingController::class, 'index']);
$app->get('/borders/create', [BorderCrossingController::class, 'create']);
$app->post('/borders/store', [BorderCrossingController::class, 'store']);
$app->get('/borders/edit', [BorderCrossingController::class, 'edit']);
$app->post('/borders/update', [BorderCrossingController::class, 'update']);
$app->get('/borders/show', [BorderCrossingController::class, 'show']);
$app->post('/borders/status', [BorderCrossingController::class, 'status']);
$app->post('/borders/checklist', [BorderCrossingController::class, 'checklist']);
$app->post('/borders/issue', [BorderCrossingController::class, 'issue']);
$app->post('/borders/upload', [BorderCrossingController::class, 'upload']);
$app->post('/borders/brokers/store', [BorderCrossingController::class, 'brokerStore']);
$app->get('/borders/packet', [BorderCrossingController::class, 'packet']);

$app->get('/tracking', [TrackingController::class, 'index']);
$app->get('/tracking/data', [TrackingController::class, 'data']);
$app->post('/tracking/ping', [TrackingController::class, 'ping']);
$app->post('/tracking/stop', [TrackingController::class, 'stop']);

$app->get('/branding/logo', [SettingsController::class, 'logo']);
$app->get('/settings', [SettingsController::class, 'index']);
$app->post('/settings/update', [SettingsController::class, 'update']);
$app->get('/tasks', [TaskController::class, 'index']);
$app->post('/tasks/store', [TaskController::class, 'store']);
$app->post('/tasks/status', [TaskController::class, 'status']);

$app->get('/driver/expenses', [DriverPortalController::class, 'expenses']);
$app->post('/driver/expenses/store', [DriverPortalController::class, 'storeExpense']);
$app->get('/driver/finances', [DriverPortalController::class, 'finances']);

$app->get('/notifications', [NotificationController::class, 'index']);
$app->post('/notifications/read', [NotificationController::class, 'read']);
$app->post('/notifications/read-all', [NotificationController::class, 'readAll']);
$app->get('/search', [SearchController::class, 'index']);

try {
    $app->dispatch();
} catch (Throwable $exception) {
    http_response_code(500);
    $errorMessage = (defined('APP_DEBUG') && APP_DEBUG) ? $exception->getMessage() : 'FleetLume could not complete this request. Please try again or contact your administrator.';
    require app_path('Views/errors/500.php');
}
