{% if navigation_render_location_v2|default('page') == 'global-host' or not global_role_navigation_host_v2|default(false) %}
{#
  UI/UX V2 Phase 1E.1 — Unified Firm Admin navigation.
  Presentation only. Existing routes and route-level permissions remain authoritative.
#}
{% set _firm_admin_path = request.url.path %}
{% set _firm_admin_tab = request.query_params.get('tab', 'overview') %}
{% set navigation_aria_label_v2 = 'Firm administration navigation' %}
{% set navigation_items_v2 = [
  {
    'label': 'Dashboard',
    'visible': true,
    'active': _firm_admin_path.startswith('/firm-admin/dashboard'),
    'children': [
      {'label': 'Overview', 'url': '/firm-admin/dashboard?tab=overview', 'active': _firm_admin_path.startswith('/firm-admin/dashboard') and _firm_admin_tab == 'overview'},
      {'label': 'Branches', 'url': '/firm-admin/dashboard?tab=branches', 'active': _firm_admin_path.startswith('/firm-admin/dashboard') and _firm_admin_tab == 'branches'},
      {'label': 'Users & Roles', 'url': '/firm-admin/dashboard?tab=users', 'active': _firm_admin_path.startswith('/firm-admin/dashboard') and _firm_admin_tab == 'users'},
      {'label': 'Firm Settings', 'url': '/firm-admin/dashboard?tab=firm-settings', 'active': _firm_admin_path.startswith('/firm-admin/dashboard') and _firm_admin_tab == 'firm-settings'},
      {'label': 'Services Setup', 'url': '/firm-admin/dashboard?tab=services', 'active': _firm_admin_path.startswith('/firm-admin/dashboard') and _firm_admin_tab == 'services'},
      {'label': 'Financial Years', 'url': '/firm-admin/dashboard?tab=financial-years', 'active': _firm_admin_path.startswith('/firm-admin/dashboard') and _firm_admin_tab == 'financial-years'},
      {'label': 'Reports', 'url': '/firm-admin/dashboard?tab=reports', 'active': _firm_admin_path.startswith('/firm-admin/dashboard') and _firm_admin_tab == 'reports'},
      {'label': 'Wizards', 'url': '/firm-admin/dashboard?tab=wizards', 'active': _firm_admin_path.startswith('/firm-admin/dashboard') and _firm_admin_tab == 'wizards'},
      {'label': 'Audit Logs', 'url': '/firm-admin/dashboard?tab=audit-logs', 'active': _firm_admin_path.startswith('/firm-admin/dashboard') and _firm_admin_tab == 'audit-logs'}
    ]
  },
  {
    'label': 'Clients',
    'visible': true,
    'active': _firm_admin_path.startswith('/clients'),
    'children': [
      {'label': 'Client List', 'url': '/clients', 'active': _firm_admin_path == '/clients'},
      {'label': 'Add Client', 'url': '/clients/new', 'active': _firm_admin_path.startswith('/clients/new')},
      {'label': 'Import Clients', 'url': '/clients/import', 'active': _firm_admin_path.startswith('/clients/import')},
      {'label': 'Export Clients', 'url': '/clients/export', 'active': _firm_admin_path.startswith('/clients/export')}
    ]
  },
  {
    'label': 'Services',
    'visible': true,
    'active': _firm_admin_path.startswith('/services') and not _firm_admin_path.startswith('/services/engagements'),
    'children': [
      {'label': 'Firm Services', 'url': '/services', 'active': _firm_admin_path == '/services'},
      {'label': 'Service Catalogue', 'url': '/services/catalogue', 'active': _firm_admin_path.startswith('/services/catalogue')},
      {'label': 'Task Templates', 'url': '/services/templates', 'active': _firm_admin_path.startswith('/services/templates')},
      {'label': 'Bulk Imports', 'url': '/services/bulk-imports', 'active': _firm_admin_path.startswith('/services/bulk-imports')}
    ]
  },
  {
    'label': 'Engagements',
    'url': '/services/engagements',
    'visible': true,
    'active': _firm_admin_path.startswith('/services/engagements')
  },
  {
    'label': 'Team',
    'visible': true,
    'active': _firm_admin_path.startswith('/employees') or _firm_admin_path.startswith('/consultants') or _firm_admin_path.startswith('/firm-admin/users'),
    'children': [
      {'label': 'Employees', 'url': '/employees', 'active': _firm_admin_path == '/employees'},
      {'label': 'Add Employee', 'url': '/employees/new', 'active': _firm_admin_path.startswith('/employees/new')},
      {'label': 'Work Allocation', 'url': '/employees/work', 'active': _firm_admin_path.startswith('/employees/work')},
      {'label': 'Attendance', 'url': '/employees/attendance', 'active': _firm_admin_path.startswith('/employees/attendance')},
      {'label': 'Leave', 'url': '/employees/leave', 'active': _firm_admin_path.startswith('/employees/leave')},
      {'label': 'Consultants', 'url': '/consultants', 'active': _firm_admin_path.startswith('/consultants')},
      {'label': 'Add Partner / Manager / Staff', 'url': '/firm-admin/dashboard?tab=users', 'active': _firm_admin_path.startswith('/firm-admin/users')}
    ]
  },
  {
    'label': 'Documents',
    'url': '/documents',
    'visible': true,
    'active': _firm_admin_path.startswith('/documents')
  },
  {
    'label': 'Billing',
    'url': '/billing',
    'visible': true,
    'active': _firm_admin_path.startswith('/billing')
  },
  {
    'label': 'Firm Setup',
    'visible': true,
    'active': _firm_admin_path.startswith('/system-settings') or _firm_admin_path.startswith('/email/settings'),
    'children': [
      {'label': 'Settings Dashboard', 'url': '/system-settings', 'active': _firm_admin_path == '/system-settings'},
      {'label': 'Branches', 'url': '/system-settings/branches', 'active': _firm_admin_path.startswith('/system-settings/branches')},
      {'label': 'Users', 'url': '/system-settings/users', 'active': _firm_admin_path.startswith('/system-settings/users')},
      {'label': 'Roles & Permissions', 'url': '/system-settings/rbac/roles', 'active': _firm_admin_path.startswith('/system-settings/rbac')},
      {'label': 'Financial Years', 'url': '/system-settings/financial-years', 'active': _firm_admin_path.startswith('/system-settings/financial-years')},
      {'label': 'Branding', 'url': '/system-settings/branding', 'active': _firm_admin_path.startswith('/system-settings/branding')},
      {'label': 'Firm Email Settings', 'url': '/email/settings', 'active': _firm_admin_path.startswith('/email/settings')},
      {'label': 'Audit Logs', 'url': '/system-settings/audit-logs', 'active': _firm_admin_path.startswith('/system-settings/audit-logs')}
    ]
  },
  {
    'label': 'Alerts',
    'url': '/alerts',
    'visible': true,
    'active': _firm_admin_path.startswith('/alerts'),
    'badge': unread_alert_count if unread_alert_count is defined and unread_alert_count > 0 else none
  }
] %}

<div class="relative left-1/2 w-screen -translate-x-1/2 border-b border-slate-200 bg-white shadow-sm" data-uiux-v2-pilot="firm-admin" data-uiux-v2-layout="full-width-navigation">
  {% include "ui/templates/components/navigation_v2.html" %}
  {% include "ui/templates/components/mobile_navigation_v2.html" %}
</div>

<script>
(function () {
  "use strict";

  function initialiseFirmAdminDropdowns() {
    var root = document.querySelector('[data-uiux-v2-pilot="firm-admin"]');
    if (!root || root.dataset.dropdownAutoCloseInitialised === 'true') return;
    root.dataset.dropdownAutoCloseInitialised = 'true';

    var dropdowns = Array.from(root.querySelectorAll('details'));

    function closeAllExcept(except) {
      dropdowns.forEach(function (details) {
        if (details !== except) details.removeAttribute('open');
      });
    }

    dropdowns.forEach(function (details) {
      details.addEventListener('toggle', function () {
        if (details.open) closeAllExcept(details);
      });

      details.querySelectorAll('a[href]').forEach(function (link) {
        link.addEventListener('click', function () {
          details.removeAttribute('open');
        });
      });
    });

    document.addEventListener('click', function (event) {
      if (!root.contains(event.target)) closeAllExcept(null);
    });

    document.addEventListener('keydown', function (event) {
      if (event.key === 'Escape') closeAllExcept(null);
    });
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initialiseFirmAdminDropdowns, { once: true });
  } else {
    initialiseFirmAdminDropdowns();
  }
})();
</script>
{% endif %}
