from django.urls import reverse
from django.utils.functional import SimpleLazyObject

def breadcrumbs(request):
    """
    Context processor that adds breadcrumb data to the context.
    This will be used for both UI display and structured data.
    """
    path = request.path.strip('/')
    parts = path.split('/')
    breadcrumbs_list = []
    
    # Always add home
    breadcrumbs_list.append({
        'title': 'Home',
        'url': request.build_absolute_uri(reverse('events:index'))
    })
    
    # Build breadcrumbs based on URL structure
    if parts and parts[0]:
        # Events section
        if parts[0] == 'events':
            breadcrumbs_list.append({
                'title': 'Events',
                'url': request.build_absolute_uri(reverse('events:event_list'))
            })
            
            # Event detail
            if len(parts) > 1 and parts[1].isdigit():
                # We're in an event detail page, get the title from the context
                # The actual title will be set in the view
                breadcrumbs_list.append({
                    'title': 'Event Details',  # This will be replaced in the template if needed
                    'url': request.build_absolute_uri()
                })
        
        # CFP section
        elif parts[0] == 'cfp':
            breadcrumbs_list.append({
                'title': 'Call for Proposals',
                'url': request.build_absolute_uri(reverse('events:cfp_list'))
            })
            
            # CFP detail
            if len(parts) > 1 and parts[1].isdigit():
                breadcrumbs_list.append({
                    'title': 'CFP Details',
                    'url': request.build_absolute_uri()
                })
        
        # Publications
        elif parts[0] == 'publications':
            breadcrumbs_list.append({
                'title': 'Publications',
                'url': request.build_absolute_uri(reverse('events:publications'))
            })
        
        # Gallery
        elif parts[0] == 'gallery':
            breadcrumbs_list.append({
                'title': 'Gallery',
                'url': request.build_absolute_uri(reverse('events:gallery'))
            })
            
        # Archive
        elif parts[0] == 'archive':
            breadcrumbs_list.append({
                'title': 'Archive',
                'url': request.build_absolute_uri(reverse('events:archive'))
            })
    
    return {'breadcrumbs': breadcrumbs_list}
