{% load navigation_tags %}

<!-- Permission-Aware Breadcrumb Navigation -->
<nav class="breadcrumb-navigation" aria-label="Breadcrumb">
    <div class="breadcrumb-container">
        <ol class="breadcrumb-list" role="list">
            {% for breadcrumb in breadcrumbs %}
            <li class="breadcrumb-item" role="listitem">
                {% if not forloop.last %}
                <a href="{{ breadcrumb.url }}" 
                   class="breadcrumb-link"
                   aria-label="Navigate to {{ breadcrumb.title }}">
                    {% if forloop.first %}
                    <span>🏠</span>
                    {% endif %}
                    <span class="breadcrumb-text">{{ breadcrumb.title }}</span>
                </a>
                <span>›</span>
                {% else %}
                <span class="breadcrumb-current" aria-current="page">
                    <span class="breadcrumb-text">{{ breadcrumb.title }}</span>
                </span>
                {% endif %}
            </li>
            {% endfor %}
        </ol>
        
        <!-- Contextual Actions -->
        <div class="breadcrumb-actions">
            {% get_contextual_help as help_items %}
            {% if help_items %}
            <button type="button" 
                    class="breadcrumb-help-btn"
                    onclick="toggleContextualHelp()"
                    aria-label="Show contextual help"
                    title="Get help for this page">
                <span>❓</span>
            </button>
            {% endif %}
            
            <!-- Page Actions (if any) -->
            <div class="breadcrumb-page-actions">
                <!-- This would be populated by specific page templates -->
                {% block breadcrumb_actions %}{% endblock %}
            </div>
        </div>
    </div>
    
    <!-- Contextual Help Panel -->
    {% if help_items %}
    <div class="contextual-help-panel" id="contextualHelpPanel" style="display: none;">
        <div class="help-panel-header">
            <h3 class="help-panel-title">
                <span>💡</span>
                Page Help
            </h3>
            <button type="button" 
                    class="help-panel-close"
                    onclick="toggleContextualHelp()"
                    aria-label="Close help panel">
                <span>✕</span>
            </button>
        </div>
        <div class="help-panel-content">
            {% for help_item in help_items %}
            <div class="help-item">
                <h4 class="help-item-title">{{ help_item.title }}</h4>
                <p class="help-item-content">{{ help_item.content }}</p>
            </div>
            {% endfor %}
            
            <!-- Keyboard Shortcuts -->
            {% navigation_keyboard_shortcuts as shortcuts %}
            <div class="help-shortcuts">
                <h4 class="help-shortcuts-title">Keyboard Shortcuts</h4>
                <div class="shortcuts-grid">
                    {% for action, shortcut in shortcuts.items %}
                    <div class="shortcut-item">
                        <span class="shortcut-key">{{ shortcut }}</span>
                        <span class="shortcut-action">{{ action|title }}</span>
                    </div>
                    {% endfor %}
                </div>
            </div>
        </div>
    </div>
    {% endif %}
</nav>

<style>
/* Breadcrumb Navigation Styles */
.breadcrumb-navigation {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid #e5e7eb;
    padding: 0.75rem 0;
    position: sticky;
    top: 48px; /* Account for branch bar */
    z-index: 40;
    transition: all 0.3s ease;
}

.dark .breadcrumb-navigation {
    background: rgba(15, 23, 42, 0.95);
    border-bottom-color: #334155;
}

.breadcrumb-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    max-width: 100%;
    margin: 0 auto;
    padding: 0 1.5rem;
}

.breadcrumb-list {
    display: flex;
    align-items: center;
    list-style: none;
    margin: 0;
    padding: 0;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.breadcrumb-item {
    display: flex;
    align-items: center;
}

.breadcrumb-link {
    display: flex;
    align-items: center;
    color: #6b7280;
    text-decoration: none;
    padding: 0.25rem 0.5rem;
    border-radius: 0.375rem;
    transition: all 0.2s ease;
    font-size: 0.875rem;
    font-weight: 500;
}

.breadcrumb-link:hover {
    color: #00308a;
    background: rgba(0, 48, 138, 0.1);
    transform: translateY(-1px);
}

.dark .breadcrumb-link {
    color: #94a3b8;
}

.dark .breadcrumb-link:hover {
    color: #86c662;
    background: rgba(134, 198, 98, 0.1);
}

.breadcrumb-home-icon {
    width: 1rem;
    height: 1rem;
    margin-right: 0.375rem;
}

.breadcrumb-text {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 150px;
}

.breadcrumb-separator {
    color: #9ca3af;
    font-size: 0.75rem;
    margin: 0 0.5rem;
}

.dark .breadcrumb-separator {
    color: #6b7280;
}

.breadcrumb-current {
    color: #111827;
    font-weight: 600;
    font-size: 0.875rem;
    padding: 0.25rem 0.5rem;
    background: rgba(134, 198, 98, 0.1);
    border-radius: 0.375rem;
    border: 1px solid rgba(134, 198, 98, 0.2);
}

.dark .breadcrumb-current {
    color: #f1f5f9;
    background: rgba(134, 198, 98, 0.15);
}

.breadcrumb-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.breadcrumb-help-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    background: rgba(59, 130, 246, 0.1);
    border: 1px solid rgba(59, 130, 246, 0.2);
    border-radius: 0.5rem;
    color: #3b82f6;
    cursor: pointer;
    transition: all 0.2s ease;
}

.breadcrumb-help-btn:hover {
    background: rgba(59, 130, 246, 0.2);
    transform: scale(1.05);
}

.breadcrumb-page-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* Contextual Help Panel */
.contextual-help-panel {
    position: absolute;
    top: 100%;
    right: 1.5rem;
    width: 320px;
    background: white;
    border: 1px solid #e5e7eb;
    border-radius: 0.75rem;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
    z-index: 50;
    animation: slideDown 0.3s ease;
}

.dark .contextual-help-panel {
    background: #1e293b;
    border-color: #334155;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
}

.help-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem;
    border-bottom: 1px solid #e5e7eb;
}

.dark .help-panel-header {
    border-bottom-color: #334155;
}

.help-panel-title {
    font-size: 1rem;
    font-weight: 600;
    color: #111827;
    margin: 0;
}

.dark .help-panel-title {
    color: #f1f5f9;
}

.help-panel-close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 1.5rem;
    height: 1.5rem;
    background: none;
    border: none;
    color: #6b7280;
    cursor: pointer;
    border-radius: 0.25rem;
    transition: all 0.2s ease;
}

.help-panel-close:hover {
    background: rgba(107, 114, 128, 0.1);
    color: #374151;
}

.dark .help-panel-close {
    color: #94a3b8;
}

.dark .help-panel-close:hover {
    background: rgba(148, 163, 184, 0.1);
    color: #cbd5e1;
}

.help-panel-content {
    padding: 1rem;
    max-height: 400px;
    overflow-y: auto;
}

.help-item {
    margin-bottom: 1rem;
}

.help-item:last-child {
    margin-bottom: 0;
}

.help-item-title {
    font-size: 0.875rem;
    font-weight: 600;
    color: #374151;
    margin: 0 0 0.25rem 0;
}

.dark .help-item-title {
    color: #d1d5db;
}

.help-item-content {
    font-size: 0.8125rem;
    color: #6b7280;
    margin: 0;
    line-height: 1.5;
}

.dark .help-item-content {
    color: #9ca3af;
}

.help-shortcuts {
    margin-top: 1.5rem;
    padding-top: 1rem;
    border-top: 1px solid #e5e7eb;
}

.dark .help-shortcuts {
    border-top-color: #334155;
}

.help-shortcuts-title {
    font-size: 0.875rem;
    font-weight: 600;
    color: #374151;
    margin: 0 0 0.75rem 0;
}

.dark .help-shortcuts-title {
    color: #d1d5db;
}

.shortcuts-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.5rem;
}

.shortcut-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.375rem 0.5rem;
    background: rgba(243, 244, 246, 0.5);
    border-radius: 0.375rem;
}

.dark .shortcut-item {
    background: rgba(55, 65, 81, 0.5);
}

.shortcut-key {
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: 0.75rem;
    font-weight: 600;
    color: #374151;
    background: white;
    padding: 0.125rem 0.375rem;
    border-radius: 0.25rem;
    border: 1px solid #d1d5db;
}

.dark .shortcut-key {
    color: #d1d5db;
    background: #374151;
    border-color: #4b5563;
}

.shortcut-action {
    font-size: 0.75rem;
    color: #6b7280;
}

.dark .shortcut-action {
    color: #9ca3af;
}

/* Responsive Design */
@media (max-width: 768px) {
    .breadcrumb-container {
        padding: 0 1rem;
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }
    
    .breadcrumb-text {
        max-width: 100px;
    }
    
    .contextual-help-panel {
        right: 1rem;
        left: 1rem;
        width: auto;
    }
    
    .breadcrumb-actions {
        align-self: flex-end;
    }
}

@media (max-width: 640px) {
    .breadcrumb-list {
        flex-wrap: nowrap;
        overflow-x: auto;
        scrollbar-width: none;
        -ms-overflow-style: none;
    }
    
    .breadcrumb-list::-webkit-scrollbar {
        display: none;
    }
    
    .breadcrumb-text {
        max-width: 80px;
    }
}

/* Animations */
@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Focus states for accessibility */
.breadcrumb-link:focus,
.breadcrumb-help-btn:focus,
.help-panel-close:focus {
    outline: 2px solid #86c662;
    outline-offset: 2px;
}

/* Print styles */
@media print {
    .breadcrumb-actions,
    .contextual-help-panel {
        display: none !important;
    }
    
    .breadcrumb-navigation {
        background: white !important;
        border-bottom: 1px solid #000 !important;
        position: static !important;
    }
}
</style>

<script>
// Contextual help panel functionality
function toggleContextualHelp() {
    const panel = document.getElementById('contextualHelpPanel');
    if (panel) {
        const isVisible = panel.style.display !== 'none';
        panel.style.display = isVisible ? 'none' : 'block';
        
        // Close panel when clicking outside
        if (!isVisible) {
            document.addEventListener('click', closeHelpOnOutsideClick);
        } else {
            document.removeEventListener('click', closeHelpOnOutsideClick);
        }
    }
}

function closeHelpOnOutsideClick(event) {
    const panel = document.getElementById('contextualHelpPanel');
    const helpBtn = document.querySelector('.breadcrumb-help-btn');
    
    if (panel && !panel.contains(event.target) && !helpBtn.contains(event.target)) {
        panel.style.display = 'none';
        document.removeEventListener('click', closeHelpOnOutsideClick);
    }
}

// Keyboard shortcuts for help
document.addEventListener('keydown', function(e) {
    if (e.key === 'F1') {
        e.preventDefault();
        toggleContextualHelp();
    }
    
    if (e.key === 'Escape') {
        const panel = document.getElementById('contextualHelpPanel');
        if (panel && panel.style.display !== 'none') {
            panel.style.display = 'none';
            document.removeEventListener('click', closeHelpOnOutsideClick);
        }
    }
});

// Auto-hide breadcrumb on scroll (optional)
let lastScrollTop = 0;
window.addEventListener('scroll', function() {
    const breadcrumb = document.querySelector('.breadcrumb-navigation');
    const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
    
    if (scrollTop > lastScrollTop && scrollTop > 100) {
        // Scrolling down
        breadcrumb.style.transform = 'translateY(-100%)';
    } else {
        // Scrolling up
        breadcrumb.style.transform = 'translateY(0)';
    }
    
    lastScrollTop = scrollTop;
});
</script>