"""
Social Media Monitoring UI views
"""
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.db.models import Q
from django.shortcuts import get_object_or_404, render

from apps.organizations.models import Hospital

from .models import SocialMention


@login_required
def mention_list(request):
    """Social media mentions list view"""
    queryset = SocialMention.objects.select_related(
        'hospital', 'department', 'px_action', 'responded_by'
    )
    
    # Apply RBAC filters
    user = request.user
    if user.is_px_admin():
        pass
    elif user.hospital:
        queryset = queryset.filter(hospital=user.hospital)
    else:
        queryset = queryset.none()
    
    # Apply filters
    platform_filter = request.GET.get('platform')
    if platform_filter:
        queryset = queryset.filter(platform=platform_filter)
    
    sentiment_filter = request.GET.get('sentiment')
    if sentiment_filter:
        queryset = queryset.filter(sentiment=sentiment_filter)
    
    hospital_filter = request.GET.get('hospital')
    if hospital_filter:
        queryset = queryset.filter(hospital_id=hospital_filter)
    
    # Search
    search_query = request.GET.get('search')
    if search_query:
        queryset = queryset.filter(
            Q(content__icontains=search_query) |
            Q(author_username__icontains=search_query)
        )
    
    # Date range
    date_from = request.GET.get('date_from')
    if date_from:
        queryset = queryset.filter(posted_at__gte=date_from)
    
    date_to = request.GET.get('date_to')
    if date_to:
        queryset = queryset.filter(posted_at__lte=date_to)
    
    # Ordering
    queryset = queryset.order_by('-posted_at')
    
    # Pagination
    page_size = int(request.GET.get('page_size', 25))
    paginator = Paginator(queryset, page_size)
    page_number = request.GET.get('page', 1)
    page_obj = paginator.get_page(page_number)
    
    # Get filter options
    hospitals = Hospital.objects.filter(status='active')
    if not user.is_px_admin() and user.hospital:
        hospitals = hospitals.filter(id=user.hospital.id)
    
    # Statistics
    stats = {
        'total': queryset.count(),
        'positive': queryset.filter(sentiment='positive').count(),
        'negative': queryset.filter(sentiment='negative').count(),
        'neutral': queryset.filter(sentiment='neutral').count(),
    }
    
    context = {
        'page_obj': page_obj,
        'mentions': page_obj.object_list,
        'stats': stats,
        'hospitals': hospitals,
        'filters': request.GET,
    }
    
    return render(request, 'social/mention_list.html', context)


@login_required
def mention_detail(request, pk):
    """Social media mention detail view"""
    mention = get_object_or_404(SocialMention.objects.select_related('hospital', 'department', 'px_action', 'responded_by'),pk=pk)
    
    context = {
        'mention': mention,
    }
    
    return render(request, 'social/mention_detail.html', context)
