<?php
declare(strict_types=1);

require_once __DIR__ . '/../app/init.php';
app_require_login();

$requestedCompanyId = (int) ($_GET['company_id'] ?? 0);
$companyId = app_effective_company_id($requestedCompanyId > 0 ? $requestedCompanyId : null, false);
$companies = app_current_user_company_options();
if ($companyId === null && !empty($companies[0]['id'])) {
    $companyId = (int) $companies[0]['id'];
}
if ($companyId !== null && $companyId > 0) {
    app_set_context_company_id($companyId);
}

$company = $companyId ? app_company_by_id((int) $companyId) : null;
$displayName = app_normalize_whitespace((string) (app_current_user()['full_name'] ?? ''));
if ($displayName === '') {
    $displayName = app_current_username();
}

$isArabic = app_ui_language() === 'ar';
$txt = static fn (string $en, string $ar): string => $isArabic ? $ar : $en;
$query = $companyId ? ('?company_id=' . urlencode((string) $companyId)) : '';
$hasAccountingAccess = app_current_role_can_any_module(app_accounting_workspace_module_slugs());
$hasStudentAccess = app_current_role_can_any_module(app_student_workspace_module_slugs());
$hasPosAccess = app_current_role_can_any_module(app_pos_workspace_module_slugs());
$hasHrAccess = app_current_role_can_any_module(app_hr_workspace_module_slugs());
$hasExcellenceAccess = app_current_role_can_any_module(app_excellence_workspace_module_slugs());
$hasAnyModuleAccess = $hasAccountingAccess || $hasStudentAccess || $hasPosAccess || $hasHrAccess || $hasExcellenceAccess;
$cards = [
    [
        'title' => $txt('Student Accounts', 'حسابات الطلاب'),
        'subtitle' => $txt('Student billing and collections workspace.', 'مساحة فواتير الطلاب والتحصيلات.'),
        'href' => 'student_accounts.php' . $query,
        'icon' => 'ST',
        'accent' => '#9b5de5',
        'side' => 'left',
        'disabled' => false,
        'visible' => $hasStudentAccess,
    ],
    [
        'title' => $txt('General Accounting', 'الحسابات العامة'),
        'subtitle' => $txt('GL, invoices, payments, assets, and accounting reports.', 'القيود والفواتير والمدفوعات والأصول والتقارير المحاسبية.'),
        'href' => 'accounting.php' . $query,
        'icon' => 'GL',
        'accent' => '#635b8f',
        'side' => 'left',
        'disabled' => false,
        'visible' => $hasAccountingAccess,
    ],
    [
        'title' => $txt('Point of Sale', 'نقاط البيع'),
        'subtitle' => $txt('POS terminal, sessions, receipts, refunds, split payments, and cash control.', 'شاشة نقاط البيع، جلسات أمين الصندوق، الإيصالات، المرتجعات، الدفعات المقسمة، ورقابة النقدية.'),
        'href' => 'pos_accounts.php' . $query,
        'icon' => 'POS',
        'accent' => '#14b8a6',
        'side' => 'right',
        'disabled' => false,
        'visible' => $hasPosAccess,
    ],
    [
        'title' => $txt('EXcellence Accounts', 'حسابات EXcellence'),
        'subtitle' => $txt('Medical device rentals, hospital operation costs, admin expenses, and salaries.', 'تأجير الأجهزة الطبية للمستشفيات، تكاليف العمليات، المصروفات الإدارية، والرواتب.'),
        'href' => 'excellence_accounts.php' . $query,
        'icon' => 'EX',
        'accent' => '#0f766e',
        'side' => 'right',
        'disabled' => false,
        'visible' => $hasExcellenceAccess,
    ],
    [
        'title' => $txt('Dashboards', 'لوحات المؤشرات'),
        'subtitle' => $txt('Management dashboards and designed reports.', 'لوحات متابعة وتقارير مصممة.'),
        'href' => 'index.php' . $query,
        'icon' => 'DB',
        'accent' => '#f59e0b',
        'side' => 'left',
        'disabled' => false,
        'visible' => $hasAnyModuleAccess,
    ],
    [
        'title' => $txt('Human Resources', 'الموارد البشرية'),
        'subtitle' => $txt('Employees, attendance, leave, payroll, and HR reports.', 'الموظفون والحضور والإجازات والرواتب وتقارير الموارد البشرية.'),
        'href' => 'hr.php' . $query,
        'icon' => 'HR',
        'accent' => '#ef4444',
        'side' => 'right',
        'disabled' => false,
        'visible' => $hasHrAccess,
    ],
];
$cards = array_values(array_filter($cards, static fn (array $card): bool => !empty($card['visible'])));
?>
<!doctype html>
<html <?= app_html_attrs() ?>>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?= app_escape((string) app_config('app_name')) ?> - <?= app_escape($txt('Home', 'الصفحة الرئيسية')) ?></title>
  <link rel="icon" href="assets/favicon.svg" type="image/svg+xml">
  <link rel="shortcut icon" href="assets/favicon.svg" type="image/svg+xml">
  <link rel="stylesheet" href="assets/styles.css">
</head>
<body class="portal-body">
<?= app_ui_controls_html('portal') ?>
<main class="portal-shell">
  <header class="portal-topbar">
    <div class="portal-user">
      <a class="portal-power" href="logout.php" title="<?= app_escape($txt('Logout', 'تسجيل الخروج')) ?>"><?= app_escape($txt('Logout', 'خروج')) ?></a>
      <span class="portal-avatar"><img src="<?= app_escape(app_logo_path_for_header($company)) ?>" alt="Company"></span>
      <span>
        <strong><?= app_escape($displayName) ?></strong>
        <small><?= app_escape(app_role_label((string) (app_current_user()['role'] ?? 'accountant'))) ?></small>
      </span>
    </div>
    <div class="portal-actions">
      <?php if (count($companies) > 1): ?>
        <a href="select_company.php"><?= app_escape($txt('Switch Company', 'تغيير الشركة')) ?></a>
      <?php endif; ?>
      <a href="change_password.php"><?= app_escape($txt('Change Password', 'تغيير كلمة المرور')) ?></a>
      <a class="is-home" href="home.php<?= app_escape($query) ?>"><?= app_escape($txt('Home', 'الصفحة الرئيسية')) ?></a>
    </div>
  </header>

  <section class="portal-map" aria-label="<?= app_escape($txt('ERP modules', 'مكونات النظام')) ?>">
    <div class="portal-center">
      <div class="portal-center-card">
        <span class="portal-center-kicker"><?= app_escape((string) ($company['name'] ?? app_config('app_name'))) ?></span>
        <h1><?= app_escape($txt('ORION', 'ORION')) ?></h1>
        <div class="portal-orbit-logo">
          <img src="<?= app_escape(app_logo_path_for_header($company)) ?>" alt="Logo">
        </div>
        <h2><?= app_escape($txt('ERP', 'ERP')) ?></h2>
      </div>
    </div>

    <div class="portal-module-list portal-module-list--left">
      <?php foreach (array_filter($cards, static fn (array $card): bool => $card['side'] === 'left') as $card): ?>
        <a class="portal-module-card<?= !empty($card['disabled']) ? ' is-disabled' : '' ?>" href="<?= app_escape((string) $card['href']) ?>" style="--portal-accent: <?= app_escape((string) $card['accent']) ?>;">
          <span class="portal-module-text">
            <strong><?= app_escape((string) $card['title']) ?></strong>
            <small><?= app_escape((string) $card['subtitle']) ?></small>
          </span>
          <span class="portal-module-icon"><?= app_escape((string) $card['icon']) ?></span>
        </a>
      <?php endforeach; ?>
    </div>

    <div class="portal-module-list portal-module-list--right">
      <?php foreach (array_filter($cards, static fn (array $card): bool => $card['side'] === 'right') as $card): ?>
        <a class="portal-module-card" href="<?= app_escape((string) $card['href']) ?>" style="--portal-accent: <?= app_escape((string) $card['accent']) ?>;">
          <span class="portal-module-icon"><?= app_escape((string) $card['icon']) ?></span>
          <span class="portal-module-text">
            <strong><?= app_escape((string) $card['title']) ?></strong>
            <small><?= app_escape((string) $card['subtitle']) ?></small>
          </span>
        </a>
      <?php endforeach; ?>
    </div>
  </section>
</main>
<script src="assets/app.js"></script>
</body>
</html>
