<?php
use App\Core\Auth;
use App\Core\Database;
use App\Core\Config;

$currentUser = Auth::user();
$path = \App\Core\Request::path();
$isAdmin = Auth::isAny(['ADMIN']);
$isLeadUp = Auth::isAny(['LEAD', 'HEAD', 'ADMIN']);

$navCounts = [
    'my' => (int)Database::scalar("SELECT COUNT(*) FROM tickets WHERE assignee_id = :u AND status NOT IN ('CLOSED','REJECTED')", ['u' => Auth::id()]),
    'unassigned' => (int)Database::scalar("SELECT COUNT(*) FROM tickets WHERE assignee_id IS NULL AND status NOT IN ('CLOSED','REJECTED')"),
    'escalated' => (int)Database::scalar("SELECT COUNT(*) FROM tickets WHERE status = 'ESCALATED'"),
];
$unreadNotifs = (int)Database::scalar('SELECT COUNT(*) FROM in_app_notifications WHERE user_id = :u AND is_read = 0', ['u' => Auth::id()]);

function nav_active(string $needle, string $path): string {
    return (strpos($path, $needle) === 0) ? ' active' : '';
}
?><!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?= e(Config::get('app.name')) ?></title>
<link rel="icon" type="image/png" href="<?= asset('img/erpcombd-favicon.png') ?>">
<link rel="stylesheet" href="<?= asset('css/app.css') ?>">
</head>
<body>
<div class="app-shell">
  <aside class="sidebar">
    <div class="sidebar-brand">
      <a href="<?= url('/') ?>" style="display:flex;align-items:center;gap:10px;text-decoration:none;color:inherit">
        <img src="<?= asset('img/erpcombd-logo.png') ?>" alt="ERP.COM.BD" class="brand-logo-img">
        <div class="logo-sub">Support<br>Desk</div>
      </a>
    </div>

    <div class="nav-group">
      <a href="<?= url('/') ?>" class="nav-link<?= ($path === '/' || $path === '/dashboard') ? ' active' : '' ?>"><span class="dot"></span> Support Dashboard</a>
    </div>

    <div class="nav-group">
      <div class="nav-group-label">Ticket Management</div>
      <a href="<?= url('/tickets') ?>" class="nav-link<?= $path === '/tickets' ? ' active' : '' ?>"><span class="dot"></span> All Tickets</a>
      <a href="<?= url('/tickets/my') ?>" class="nav-link<?= nav_active('/tickets/my', $path) ?>"><span class="dot"></span> My Queue <?php if ($navCounts['my']) : ?><span class="count"><?= $navCounts['my'] ?></span><?php endif; ?></a>
      <a href="<?= url('/tickets/unassigned') ?>" class="nav-link<?= nav_active('/tickets/unassigned', $path) ?>"><span class="dot"></span> Unassigned <?php if ($navCounts['unassigned']) : ?><span class="count"><?= $navCounts['unassigned'] ?></span><?php endif; ?></a>
      <a href="<?= url('/tickets/escalated') ?>" class="nav-link<?= nav_active('/tickets/escalated', $path) ?>"><span class="dot"></span> Escalated <?php if ($navCounts['escalated']) : ?><span class="count"><?= $navCounts['escalated'] ?></span><?php endif; ?></a>
    </div>

    <div class="nav-group">
      <div class="nav-group-label">Knowledge Base</div>
      <a href="<?= url('/kb') ?>" class="nav-link<?= nav_active('/kb', $path) ?>"><span class="dot"></span> Articles</a>
    </div>

    <div class="nav-group">
      <div class="nav-group-label">Reports</div>
      <a href="<?= url('/reports') ?>" class="nav-link<?= nav_active('/reports', $path) ?>"><span class="dot"></span> Report Catalogue</a>
    </div>

    <?php if ($isLeadUp) : ?>
    <div class="nav-group">
      <div class="nav-group-label">SLA & Escalation</div>
      <a href="<?= url('/admin/calendar') ?>" class="nav-link<?= nav_active('/admin/calendar', $path) ?>"><span class="dot"></span> Business Calendar</a>
    </div>
    <?php endif; ?>

    <?php if ($isAdmin) : ?>
    <div class="nav-group">
      <div class="nav-group-label">Configuration</div>
      <a href="<?= url('/admin') ?>" class="nav-link<?= $path === '/admin' ? ' active' : '' ?>"><span class="dot"></span> Admin Home</a>
      <a href="<?= url('/admin/categories') ?>" class="nav-link<?= nav_active('/admin/categories', $path) ?>"><span class="dot"></span> Categories</a>
      <a href="<?= url('/admin/queues') ?>" class="nav-link<?= nav_active('/admin/queues', $path) ?>"><span class="dot"></span> Queues & Teams</a>
      <a href="<?= url('/admin/templates') ?>" class="nav-link<?= nav_active('/admin/templates', $path) ?>"><span class="dot"></span> Templates</a>
      <a href="<?= url('/admin/customers') ?>" class="nav-link<?= nav_active('/admin/customers', $path) ?>"><span class="dot"></span> Customers & Contracts</a>
      <a href="<?= url('/admin/users') ?>" class="nav-link<?= nav_active('/admin/users', $path) ?>"><span class="dot"></span> Agents & Roles</a>
      <a href="<?= url('/admin/permissions') ?>" class="nav-link<?= nav_active('/admin/permissions', $path) ?>"><span class="dot"></span> Roles & Permissions</a>
      <a href="<?= url('/admin/audit-log') ?>" class="nav-link<?= nav_active('/admin/audit-log', $path) ?>"><span class="dot"></span> Admin Audit Log</a>
    </div>
    <?php endif; ?>
  </aside>

  <div class="main">
    <div class="topbar">
      <div class="flex items-center gap-3">
        <button class="btn btn-ghost btn-sm" data-toggle-sidebar type="button" style="display:none">☰</button>
        <div>
          <div class="topbar-title"><?= e($pageTitle ?? 'Support Desk') ?></div>
          <div class="topbar-meta"><?= e($pageSubtitle ?? '') ?></div>
        </div>
      </div>
      <div class="topbar-right">
        <a href="<?= url('/notifications/read-all') ?>" class="btn btn-ghost btn-sm" title="In-app notifications">🔔<?= $unreadNotifs ? ' ' . $unreadNotifs : '' ?></a>
        <div class="user-chip">
          <div class="avatar"><?= e(strtoupper(substr($currentUser['name'] ?? '?', 0, 1))) ?></div>
          <div>
            <div style="font-weight:650"><?= e($currentUser['name'] ?? '') ?></div>
            <div class="text-faint text-sm"><?= e($currentUser['role_name'] ?? '') ?></div>
          </div>
        </div>
        <a href="<?= url('/logout') ?>" class="btn btn-sm">Log out</a>
      </div>
    </div>

    <div class="content">
      <?php if ($msg = flash_get('success')) : ?><div class="alert alert-success" data-flash><?= e($msg) ?></div><?php endif; ?>
      <?php if ($msg = flash_get('error')) : ?><div class="alert alert-error" data-flash><?= e($msg) ?></div><?php endif; ?>
      <?php if ($msg = flash_get('info')) : ?><div class="alert alert-info" data-flash><?= e($msg) ?></div><?php endif; ?>

      <?php $content(); ?>
    </div>
  </div>
</div>
<script src="<?= asset('js/app.js') ?>"></script>
</body>
</html>
