<?php
declare(strict_types=1);

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

// Clear any existing module context when entering the portal
@setcookie('orion_module_ctx', '', [
    'expires'  => time() - 3600,
    'path'     => '/',
    'samesite' => 'Lax',
]);
unset($_COOKIE['orion_module_ctx']);

$companyId = app_integer($_GET['company_id'] ?? app_context_company_id() ?? 0);
if ($companyId <= 0) {
    app_redirect('select_company.php');
}
if (!app_current_user_can_access_company($companyId)) {
    app_redirect('select_company.php');
}
app_set_context_company_id($companyId);

$company     = app_company_by_id($companyId);
$companyName = (string) ($company['name'] ?? 'Company');
$displayName = app_normalize_whitespace((string) (app_current_user()['full_name'] ?? ''));
if ($displayName === '') { $displayName = app_current_username(); }
$role        = (string) (app_current_user()['role'] ?? 'accountant');
$isAdmin     = app_is_admin();
$hasPosAccess = app_current_role_can_any_module(app_pos_workspace_module_slugs());

$q = 'company_id=' . urlencode((string) $companyId);
?>
<!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($companyName) ?> — <?= app_escape(app_t('Module Portal')) ?></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">
  <style>
    .portal-root { min-height: 100vh; display: flex; flex-direction: column; }

    .portal-topbar {
      display: flex; align-items: center; justify-content: space-between;
      padding: 14px 28px;
      background: var(--surface);
      border-bottom: 1px solid var(--border);
      position: sticky; top: 0; z-index: 100;
    }
    .portal-topbar-brand {
      font-family: 'Playfair Display','Georgia',serif;
      font-size: 18px; font-weight: 700;
      color: var(--gold, #c9a84c);
      letter-spacing: 0.02em;
      text-decoration: none;
    }
    .portal-topbar-center {
      font-size: 13px; font-weight: 700;
      color: var(--ink); text-align: center;
    }
    .portal-topbar-center span {
      display: block; font-size: 11px; font-weight: 400;
      color: var(--muted); margin-top: 2px;
    }
    .portal-topbar-right { display: flex; align-items: center; gap: 12px; }

    .portal-avatar {
      width: 36px; height: 36px; border-radius: 50%;
      background: linear-gradient(135deg, var(--gold, #c9a84c), #b8922a);
      color: #06091a;
      display: flex; align-items: center; justify-content: center;
      font-size: 13px; font-weight: 800;
    }
    .portal-logout-btn {
      padding: 7px 14px; border-radius: 8px;
      border: 1px solid var(--border2); background: transparent;
      color: var(--muted); font-size: 12px; font-weight: 700;
      cursor: pointer; text-decoration: none;
      transition: all 0.15s;
    }
    .portal-logout-btn:hover { color: var(--red, #ff5c5c); border-color: var(--red, #ff5c5c); }

    .portal-canvas {
      flex: 1; display: flex; align-items: center; justify-content: center;
      padding: 40px 24px; position: relative; overflow: hidden;
    }
    .portal-canvas::before {
      content: ''; position: absolute; inset: 0;
      background:
        radial-gradient(circle at 20% 20%, rgba(201,168,76,0.06), transparent 35%),
        radial-gradient(circle at 80% 80%, rgba(74,158,255,0.05), transparent 35%),
        radial-gradient(circle at 50% 50%, rgba(34,211,200,0.04), transparent 40%);
      pointer-events: none;
    }
    .portal-mind-map {
      position: relative; width: 900px; max-width: 100%; height: 540px;
    }

    .portal-hub {
      position: absolute; top: 50%; left: 50%;
      transform: translate(-50%, -50%);
      width: 200px; height: 200px; z-index: 10;
    }
    .portal-hub-inner {
      width: 100%; height: 100%; border-radius: 50%;
      background: var(--surface);
      border: 3px solid var(--gold, #c9a84c);
      box-shadow:
        0 0 0 8px var(--gold-dim, rgba(201,168,76,0.12)),
        0 12px 40px rgba(0,0,0,0.3);
      display: flex; flex-direction: column;
      align-items: center; justify-content: center;
      text-align: center; padding: 18px;
    }
    .portal-hub-logo {
      width: 70px; height: 70px;
      border-radius: 16px; object-fit: contain; padding: 4px;
      background: rgba(255,255,255,0.08);
      border: 1px solid var(--border2);
      margin-bottom: 10px;
    }
    .portal-hub-title {
      font-family: 'Playfair Display',Georgia,serif;
      font-size: 15px; font-weight: 700;
      color: var(--gold, #c9a84c); line-height: 1.2;
    }
    .portal-hub-sub { font-size: 11px; color: var(--muted); margin-top: 4px; }

    .portal-hub-ring {
      position: absolute; top: 50%; left: 50%;
      width: 210px; height: 210px;
      border-radius: 50%;
      border: 2px solid var(--gold, #c9a84c);
      animation: pulse-ring 2.4s ease-out infinite;
      pointer-events: none;
    }
    .portal-hub-ring:nth-child(2) { animation-delay: 0.8s; }
    .portal-hub-ring:nth-child(3) { animation-delay: 1.6s; }
    @keyframes pulse-ring {
      0%   { transform: translate(-50%,-50%) scale(1);   opacity: 0.6; }
      100% { transform: translate(-50%,-50%) scale(1.4); opacity: 0; }
    }

    .portal-hub-dot {
      position: absolute;
      width: 10px; height: 10px; border-radius: 50%;
      background: var(--gold, #c9a84c);
      box-shadow: 0 0 8px var(--gold, #c9a84c);
      animation: dot-blink 2s ease-in-out infinite;
    }
    @keyframes dot-blink { 0%,100% { opacity: 1; } 50% { opacity: 0.3; } }

    .portal-lines { position: absolute; inset: 0; pointer-events: none; z-index: 1; }

    .portal-node {
      position: absolute;
      display: flex; align-items: center; gap: 14px;
      text-decoration: none; z-index: 5;
      transition: transform 0.2s ease;
    }
    .portal-node:hover { transform: scale(1.05); }
    .portal-node:hover .portal-node-label {
      box-shadow: 0 10px 32px rgba(0,0,0,0.25);
    }
    .portal-node-icon {
      width: 80px; height: 96px; border-radius: 18px;
      display: flex; align-items: center; justify-content: center;
      font-size: 36px; flex-shrink: 0;
      box-shadow: 0 8px 22px rgba(0,0,0,0.2);
    }
    .portal-node-label {
      background: var(--surface);
      border: 1px solid var(--border2);
      border-radius: 16px;
      padding: 14px 22px;
      font-size: 16px; font-weight: 700;
      color: var(--ink);
      white-space: nowrap;
      box-shadow: 0 6px 20px rgba(0,0,0,0.16);
      transition: all 0.2s;
      min-width: 180px;
    }
    .portal-node-label small {
      display: block;
      font-size: 11px; font-weight: 500;
      color: var(--muted); margin-top: 3px;
    }

    .portal-node--gold .portal-node-icon   { background: linear-gradient(135deg, #c9a84c, #f0c96a); color: #06091a; }
    .portal-node--purple .portal-node-icon { background: linear-gradient(135deg, #6d28d9, #a78bfa); color: #fff; }
    .portal-node--red .portal-node-icon    { background: linear-gradient(135deg, #c0392b, #ff5c5c); color: #fff; }
    .portal-node--indigo .portal-node-icon { background: linear-gradient(135deg, #3730a3, #818cf8); color: #fff; }
    .portal-node--teal .portal-node-icon   { background: linear-gradient(135deg, #0f766e, #2dd4bf); color: #fff; font-size: 24px; font-weight: 900; letter-spacing: 0.04em; }

    .portal-node--pos-tl  { top: 70px;    left: 30px;  flex-direction: row-reverse; }
    .portal-node--pos-tr  { top: 70px;    right: 30px; }
    .portal-node--pos-bl  { bottom: 70px; left: 30px;  flex-direction: row-reverse; }
    .portal-node--pos-br  { bottom: 70px; right: 30px; }
    .portal-node--pos-tc  { top: 0; left: 50%; transform: translateX(-50%); }
    .portal-node--pos-tc:hover { transform: translateX(-50%) scale(1.05); }
    .portal-node--pos-bc  { bottom: 0; left: 50%; transform: translateX(-50%); }
    .portal-node--pos-bc:hover { transform: translateX(-50%) scale(1.05); }

    .portal-footer {
      text-align: center; padding: 14px;
      font-size: 11px; color: var(--muted);
      border-top: 1px solid var(--border);
      background: var(--surface);
    }

    @media (max-width: 920px) {
      .portal-mind-map {
        width: 100%; height: auto;
        display: grid; grid-template-columns: 1fr 1fr;
        gap: 16px; padding: 16px;
      }
      .portal-hub, .portal-lines { display: none; }
      .portal-node {
        position: static !important;
        flex-direction: row !important;
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: 16px;
        padding: 16px;
        transform: none !important;
        gap: 12px;
      }
      .portal-node-icon { width: 60px; height: 68px; border-radius: 14px; font-size: 28px; }
      .portal-node-label {
        border: none; padding: 0; background: none;
        box-shadow: none; font-size: 14px; min-width: 0;
      }
    }
    @media (max-width: 560px) { .portal-mind-map { grid-template-columns: 1fr; } }

    .portal-topbar .ui-switcher {
      display: flex !important; flex-direction: row !important;
      gap: 6px !important; margin: 0 !important;
    }
    .portal-topbar .ui-toggle {
      min-height: 30px !important;
      padding: 5px 10px !important;
      border-radius: 999px !important;
      font-size: 11px !important;
    }
  </style>
</head>
<body>
<div class="portal-root">

  <div class="portal-topbar">
    <a class="portal-topbar-brand" href="portal.php?<?= $q ?>">
      <?= app_escape(app_t((string) app_config('app_name'))) ?>
    </a>

    <div class="portal-topbar-center">
      <?= app_escape($companyName) ?>
      <span><?= app_escape(app_t('Choose a module to continue')) ?></span>
    </div>

    <div class="portal-topbar-right">
      <?= app_ui_controls_html('nav') ?>
      <div class="portal-avatar"><?= app_escape(mb_strtoupper(mb_substr($displayName, 0, 2))) ?></div>
      <a href="logout.php" class="portal-logout-btn"><?= app_escape(app_t('Logout')) ?></a>
    </div>
  </div>

  <div class="portal-canvas">
    <div class="portal-mind-map">

      <svg class="portal-lines" viewBox="0 0 900 540" preserveAspectRatio="xMidYMid meet">
        <line x1="450" y1="270" x2="200" y2="120" stroke="rgba(201,168,76,0.28)" stroke-width="1.5" stroke-dasharray="5 5"/>
        <line x1="450" y1="270" x2="700" y2="120" stroke="rgba(201,168,76,0.28)" stroke-width="1.5" stroke-dasharray="5 5"/>
        <line x1="450" y1="270" x2="200" y2="420" stroke="rgba(201,168,76,0.28)" stroke-width="1.5" stroke-dasharray="5 5"/>
        <line x1="450" y1="270" x2="700" y2="420" stroke="rgba(201,168,76,0.28)" stroke-width="1.5" stroke-dasharray="5 5"/>
        <line x1="450" y1="270" x2="450" y2="492" stroke="rgba(201,168,76,0.28)" stroke-width="1.5" stroke-dasharray="5 5"/>
      </svg>

      <div class="portal-hub">
        <div class="portal-hub-ring"></div>
        <div class="portal-hub-ring"></div>
        <div class="portal-hub-ring"></div>
        <?php
          $hubDots = [
            ['top'=>'-8px','left'=>'50%','transform'=>'translateX(-50%)'],
            ['bottom'=>'-8px','left'=>'50%','transform'=>'translateX(-50%)'],
            ['left'=>'-8px','top'=>'50%','transform'=>'translateY(-50%)'],
            ['right'=>'-8px','top'=>'50%','transform'=>'translateY(-50%)'],
          ];
          foreach ($hubDots as $dot): ?>
          <div class="portal-hub-dot" style="<?php
            foreach ($dot as $prop => $val): echo $prop.':'.$val.';'; endforeach;
          ?>"></div>
        <?php endforeach; ?>
        <div class="portal-hub-inner">
          <img class="portal-hub-logo"
               src="<?= app_escape(app_logo_path_for_header($company)) ?>"
               alt="<?= app_escape($companyName) ?>"
               onerror="this.src='assets/logo.svg'">
          <div class="portal-hub-title"><?= app_escape($companyName) ?></div>
          <div class="portal-hub-sub">ERP Portal</div>
        </div>
      </div>

      <!-- 1. الحسابات العامة -->
      <a href="index.php?<?= $q ?>&module_ctx=accounting"
         class="portal-node portal-node--gold portal-node--pos-tl"
         title="<?= app_escape(app_t('General Accounts')) ?>">
        <div class="portal-node-icon">📒</div>
        <div class="portal-node-label">
          <?= app_escape(app_t('General Accounts')) ?>
          <small><?= app_escape(app_t('Journals, invoices, reports')) ?></small>
        </div>
      </a>

      <!-- 2. الموارد البشرية -->
      <a href="index.php?<?= $q ?>&module_ctx=hr"
         class="portal-node portal-node--red portal-node--pos-tr"
         title="<?= app_escape(app_t('Human Resources')) ?>">
        <div class="portal-node-icon">👥</div>
        <div class="portal-node-label">
          <?= app_escape(app_t('Human Resources')) ?>
          <small><?= app_escape(app_t('Employees & payroll')) ?></small>
        </div>
      </a>

      <!-- 3. حسابات الطلاب -->
      <a href="module.php?module=sales_invoices&<?= $q ?>&module_ctx=students"
         class="portal-node portal-node--purple portal-node--pos-bl"
         title="<?= app_escape(app_t('Student Accounts')) ?>">
        <div class="portal-node-icon">🎓</div>
        <div class="portal-node-label">
          <?= app_escape(app_t('Student Accounts')) ?>
          <small><?= app_escape(app_t('Tuition invoices & payments')) ?></small>
        </div>
      </a>

      <!-- 4. الإعدادات -->
      <?php if ($isAdmin): ?>
      <a href="module.php?module=companies&<?= $q ?>&module_ctx=settings"
         class="portal-node portal-node--indigo portal-node--pos-br"
         title="<?= app_escape(app_t('System Settings')) ?>">
        <div class="portal-node-icon">⚙️</div>
        <div class="portal-node-label">
          <?= app_escape(app_t('System Settings')) ?>
          <small><?= app_escape(app_t('Companies, users & config')) ?></small>
        </div>
      </a>
      <?php else: ?>
      <a href="change_password.php"
         class="portal-node portal-node--indigo portal-node--pos-br"
         title="<?= app_escape(app_t('My Profile')) ?>">
        <div class="portal-node-icon">👤</div>
        <div class="portal-node-label">
          <?= app_escape(app_t('My Profile')) ?>
          <small><?= app_escape(app_t('Change password & preferences')) ?></small>
        </div>
      </a>
      <?php endif; ?>

      <!-- 5. EXcellence Accounts -->
      <a href="excellence_accounts.php?<?= $q ?>"
         class="portal-node portal-node--teal portal-node--pos-bc"
         title="<?= app_escape(app_t('EXcellence Accounts')) ?>">
        <div class="portal-node-icon">EX</div>
        <div class="portal-node-label">
          <?= app_escape(app_t('EXcellence Accounts')) ?>
          <small><?= app_escape(app_t('Medical Device Operations')) ?></small>
        </div>
      </a>

      <?php if ($hasPosAccess): ?>
      <a href="pos_accounts.php?<?= $q ?>"
         class="portal-node portal-node--teal portal-node--pos-tc"
         title="<?= app_escape(app_t('Point of Sale')) ?>">
        <div class="portal-node-icon">POS</div>
        <div class="portal-node-label">
          <?= app_escape(app_t('Point of Sale')) ?>
          <small><?= app_escape(app_t('Cashier sessions and receipts')) ?></small>
        </div>
      </a>
      <?php endif; ?>

    </div>
  </div>

  <div class="portal-footer">
    <?= app_escape((string) app_config('app_name')) ?>
    &bull; <?= app_escape($companyName) ?>
    &bull; <?= app_escape(date('Y')) ?>
  </div>

</div>

<script src="assets/app.js"></script>
</body>
</html>
