"""
Notifications Views
"""
from django.shortcuts import render
from config.decorators import login_required_web
from notifications.models import Notification


@login_required_web
def notifications_view(request):
    """User notifications list"""
    type_filter = request.GET.get('type', '')
    is_read_filter = request.GET.get('is_read', '')
    
    notifications = Notification.objects.filter(
        user=request.user
    ).order_by('-created_at')
    
    if type_filter:
        notifications = notifications.filter(type=type_filter)
    
    if is_read_filter == 'false':
        notifications = notifications.filter(is_read=False)
    elif is_read_filter == 'true':
        notifications = notifications.filter(is_read=True)
    
    # Mark as read if requested
    if request.GET.get('mark_read') == 'all':
        notifications.update(is_read=True)
    
    unread_count = Notification.objects.filter(
        user=request.user,
        is_read=False
    ).count()
    
    context = {
        'title': 'الإشعارات - دليلك IQ',
        'description': 'إشعاراتك',
        'notifications': notifications,
        'unread_count': unread_count,
        'type_filter': type_filter,
        'is_read_filter': is_read_filter,
    }
    return render(request, 'notifications/list.html', context)
