{% macro nav_link(name, url, icon=None) %}
    <li class="nav-item mx-2">
        <a class="nav-link" href="{{ url }}">
            {% if icon %}
                <i class="material-icons">{{ icon }}</i>
            {% endif %}
            {{ name }}
        </a>
    </li>
{% endmacro %}

{% macro dropdown(title, icon, children) %}
    <li class="nav-item dropdown dropdown-hover mx-2">
        <a role="button" class="nav-link ps-2 d-flex cursor-pointer align-items-center" id="{{ title | replace(' ', '') }}" data-bs-toggle="dropdown" aria-expanded="false">
            {% if icon %}
                <span class="material-symbols-outlined" style="font-variation-settings: 'FILL' 0, 'wght' 400, 'GRAD' 0, 'opsz' 24;">{{ icon }}</span>
            {% endif %}
            {{ title }}
            <img src="{{ url_for('static', filename='assets/img/down-arrow-dark.svg') }}" alt="down-arrow" class="arrow ms-2">
        </a>
        <div class="dropdown-menu dropdown-menu-animation" aria-labelledby="{{ title | replace(' ', '') }}">
            <ul class="list-group w-100">
                {% for child in children %}
                    {% if child.roles %}
                        {% if current_user.has_roles(*child.roles) %}
                            <li class="nav-item dropdown-subitem list-group-item border-0 p-0">
                                <a class="dropdown-item ps-3 border-radius-md mb-1" href="{{ child.url }}">
                                    <span>{{ child.name }}</span>
                                </a>
                            </li>
                        {% endif %}
                    {% else %}
                        <li class="nav-item dropdown-subitem list-group-item border-0 p-0">
                            <a class="dropdown-item ps-3 border-radius-md mb-1" href="{{ child.url }}">
                                <span>{{ child.name }}</span>
                            </a>
                        </li>
                    {% endif %}
                {% endfor %}
            </ul>
        </div>
    </li>
{% endmacro %}

<div class="container position-sticky z-index-sticky top-0">
    <div class="row">
        <div class="col-12">
            <nav class="navbar navbar-expand-lg navbar-light bg-white z-index-3 py-3">
                <div class="container-fluid px-0">
                    <a class="navbar-brand font-weight-bolder ms-sm-3  d-none d-md-block" href=" https://www.askeveai.com " rel="tooltip" title="Realised by Josako & Kobe" data-placement="bottom" target="_blank">
                        EveAI
                    </a>
                    <a class="navbar-brand font-weight-bolder ms-sm-3  d-block d-md-none" href=" https://www.flow-it.net " rel="tooltip" title="Realised by Josako & Kobe" data-placement="bottom" target="_blank">
                        EveAI
                    </a>
                    <a href="session_defaults" class="btn btn-sm  bg-gradient-primary  mb-0 ms-auto d-lg-none d-block">
                        {% if 'tenant' in session %}
                            TENANT: {{ session['tenant'].get('name', 'None') }}
                        {% endif %}
                    </a>
                    <button class="navbar-toggler shadow-none ms-md-2" type="button" data-bs-toggle="collapse" data-bs-target="#navigation" aria-controls="navigation" aria-expanded="false" aria-label="Toggle navigation">
                          <span class="navbar-toggler-icon mt-2">
                                <span class="navbar-toggler-bar bar1"></span>
                                <span class="navbar-toggler-bar bar2"></span>
                                <span class="navbar-toggler-bar bar3"></span>
                          </span>
                    </button>
                    <div class="collapse navbar-collapse w-100 pt-3 pb-2 py-lg-0" id="navigation">
                        <ul class="navbar-nav navbar-nav-hover mx-auto">
                            {% if current_user.is_authenticated %}
                                {{ dropdown('Tenants', 'source_environment', [
                                    {'name': 'Tenants', 'url': 'user/tenants', 'roles': ['Super User', 'Partner Admin']},
                                    {'name': 'Consent Versions', 'url': 'user/consent_versions', 'roles': ['Super User']},
                                    {'name': 'Tenant Overview', 'url': 'user/tenant_overview', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Edit Tenant', 'url': 'user/tenant/' ~ session['tenant'].get('id'), 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Tenant Consent History', 'url': 'user/tenant_consents_history', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Tenant Consent Status', 'url': 'user/consent/tenant', 'roles': ['Tenant Admin']},
                                    {'name': 'Tenant Consent Status', 'url': 'user/tenants/' ~ session['tenant'].get('id') ~ '/consents', 'roles': ['Super User', 'Partner Admin']},
                                    {'name': 'Consent Renewal', 'url': 'user/consent/tenant_renewal', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},

                                    {'name': 'Tenant Partner Services', 'url': 'user/tenant_partner_services', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Tenant Makes', 'url': 'user/tenant_makes', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Tenant Projects', 'url': 'user/tenant_projects', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Users', 'url': 'user/view_users', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                ]) }}
                            {% endif %}
                            {% if current_user.is_authenticated and current_user.has_roles('Super User') %}
                                {% set partner_menu_items = [
                                    {'name': 'Partners', 'url': 'partner/partners', 'roles': ['Super User']},
                                    {'name': 'Partner Services', 'url': 'partner/partner_services', 'roles': ['Super User']}
                                ] %}

                                {% if 'partner' in session and session['partner'] %}
                                    {% set partner_menu_items = partner_menu_items + [
                                        {'name': 'Edit Partner', 'url': 'partner/partner/' ~ session['partner'].get('id'), 'roles': ['Super User']}
                                    ] %}
                                {% endif %}

                                {{ dropdown('Partners', 'partner_exchange', partner_menu_items) }}
                            {% endif %}
                            {% if current_user.is_authenticated %}
                                {{ dropdown('Documents', 'note_stack', [
                                    {'name': 'Catalogs', 'url': 'document/catalogs', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Processors', 'url': 'document/processors', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Retrievers', 'url': 'document/retrievers', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Add Document', 'url': 'document/add_document', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Add URL', 'url': 'document/add_url', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Documents', 'url': 'document/documents', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Document Processing', 'url': 'document/documents_processing', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Library Operations', 'url': 'document/library_operations', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                ]) }}
                            {% endif %}
                            {% if current_user.is_authenticated %}
                                {{ dropdown('Interactions', 'hub', [
                                    {'name': 'Specialists', 'url': 'interaction/specialists', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Specialist Magic Links', 'url': 'interaction/specialist_magic_links', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Assets', 'url': 'interaction/assets', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Data Capsules', 'url': 'interaction/eveai_data_capsules', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Chat Sessions', 'url': 'interaction/chat_sessions', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                ]) }}
                            {% endif %}
                            {% if current_user.is_authenticated %}
                                {{ dropdown('Entitlements', 'settings', [
                                    {'name': 'License Tiers', 'url': 'entitlements/license_tiers', 'roles': ['Super User', 'Partner Admin']},
                                    {'name': 'Trigger Actions', 'url': 'administration/trigger_actions', 'roles': ['Super User']},
                                    {'name': 'Licenses', 'url': 'entitlements/licenses', 'roles': ['Super User', 'Tenant Admin', 'Partner Admin']},
                                    {'name': 'Active Usage', 'url': 'entitlements/active_usage', 'roles': ['Super User', 'Tenant Admin', 'Partner Admin']},
                                ]) }}
                            {% endif %}
                            {% if current_user.is_authenticated %}
                                {{ dropdown(current_user.user_name, 'person', [
                                    {'name': 'Session Defaults', 'url': 'session_defaults', 'roles': ['Super User', 'Tenant Admin']},
                                    {'name': 'Release Notes', 'url': 'release_notes', 'roles': ['Super User', 'Partner Admin', 'Tenant Admin']},
                                    {'name': 'Logout', 'url': 'logout'}
                                ]) }}
                            {% else %}
                                {{ dropdown('Account', 'person', [
                                    {'name': 'Login', 'url': 'login'}
                                ]) }}
                            {% endif %}
                        </ul>
                    </div>
                </div>
            </nav>
        </div>
    </div>
</div>
