<?php
require __DIR__ . '/../app/bootstrap.php';

auth_require();
$user = auth_user();
$page = $_GET['page'] ?? 'dashboard';

$routes = [
  'dashboard'   => __DIR__ . '/../app/pages/dashboard.php',
  'oases'       => __DIR__ . '/../app/pages/oases.php',
  'aflaj'       => __DIR__ . '/../app/pages/aflaj.php',
  'farms'       => __DIR__ . '/../app/pages/farms.php',
  'assets'      => __DIR__ . '/../app/pages/assets.php',
  'maintenance' => __DIR__ . '/../app/pages/maintenance.php',
  'contractors' => __DIR__ . '/../app/pages/contractors.php',
  'contracts'   => __DIR__ . '/../app/pages/contracts.php',
  'reports'     => __DIR__ . '/../app/pages/reports.php',
  'audit'       => __DIR__ . '/../app/pages/audit.php',
  'users'       => __DIR__ . '/../app/pages/users.php',
  'roles'       => __DIR__ . '/../app/pages/permissions.php',
  'permissions' => __DIR__ . '/../app/pages/permissions.php',
  'map'         => __DIR__ . '/../app/pages/map.php',
  'import'      => __DIR__ . '/../app/pages/import.php',
  'export_xlsx' => __DIR__ . '/../app/pages/export_xlsx.php',
  'inspectors'  => __DIR__ . '/../app/pages/inspectors.php',
  'inspector_tasks' => __DIR__ . '/../app/pages/inspector_tasks.php',
  'db_tools'    => __DIR__ . '/../app/pages/db_tools.php',
  'logout'      => __DIR__ . '/../app/logout.php',
];

if (!isset($routes[$page])) {
  $page = 'dashboard';
}

$title_map = [
  'dashboard'   => 'لوحة التحكم',
  'oases'       => 'الواحات',
  'aflaj'       => 'الأفلاج',
  'farms'       => 'المزارع',
  'assets'      => 'الأصول',
  'maintenance' => 'الصيانة',
  'contractors' => 'المقاولون',
  'contracts'   => 'العقود',
  'reports'     => 'التقارير',
  'audit'       => 'سجل التدقيق',
  'users'       => 'المستخدمون',
  'roles'       => 'الصلاحيات',
  'permissions' => 'الصلاحيات',
  'map'         => 'الخريطة',
  'import'      => 'الاستيراد',
  'export_xlsx' => 'تصدير Excel',
  'inspectors'  => 'المراقبون',
  'inspector_tasks' => 'مهام المراقبين',
  'db_tools'    => 'نسخ/استعادة قاعدة البيانات',
];

$title = $title_map[$page] ?? 'لوحة التحكم';

$perm_map = [
  'dashboard'   => 'view_dashboard',
  'oases'       => 'manage_oases',
  'aflaj'       => 'manage_aflaj',
  'farms'       => 'manage_farms',
  'assets'      => 'manage_assets',
  'maintenance' => 'manage_maintenance',
  'contractors' => 'manage_contractors',
  'contracts'   => 'manage_contracts',
  'reports'     => 'view_reports',
  'audit'       => 'view_audit',
  'users'       => 'manage_users',
  'roles'       => 'manage_roles',
  'permissions' => 'manage_roles',
  'map'         => 'view_dashboard',
  'import'      => 'view_reports',
  // export_xlsx is gated inside the page per entity (export_*_xlsx)
  'export_xlsx' => 'view_dashboard',

  'inspectors'  => 'view_inspectors',
  'inspector_tasks' => 'view_inspector_tasks',
  'db_tools'    => 'manage_db_tools',
];

$need = $perm_map[$page] ?? 'view_dashboard';
if ($need && function_exists('auth_can') && !auth_can($need)) {
  flash_set('err', 'ليس لديك صلاحية للوصول لهذه الصفحة.');
  redirect(url('/index.php?page=dashboard'));
}

$controller = $routes[$page];
$view = $controller;
$page_title = $title;

ob_start();
require $controller;
$controller_out = ob_get_clean();

// دعم نمط Controller -> View (إذا قام الـ Controller بتغيير $view)
if ($view !== $controller && is_string($view) && file_exists($view)) {
  ob_start();
  require $view;
  $__content = ob_get_clean();
} else {
  $__content = $controller_out;
}

// اسم الصفحة النهائي (إذا تم تعيينه داخل الصفحة)
if (isset($page_title) && is_string($page_title) && $page_title !== '') {
  $title = $page_title;
}

require __DIR__ . '/../app/views/layout.php';
