""" Call Center Console UI views """ from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator from django.db.models import Q, Avg from django.shortcuts import get_object_or_404, render from apps.organizations.models import Hospital from .models import CallCenterInteraction @login_required def interaction_list(request): """Call center interactions list view""" queryset = CallCenterInteraction.objects.select_related( 'patient', 'hospital', 'department', 'agent' ) # 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 call_type_filter = request.GET.get('call_type') if call_type_filter: queryset = queryset.filter(call_type=call_type_filter) hospital_filter = request.GET.get('hospital') if hospital_filter: queryset = queryset.filter(hospital_id=hospital_filter) is_low_rating = request.GET.get('is_low_rating') if is_low_rating == 'true': queryset = queryset.filter(is_low_rating=True) # Search search_query = request.GET.get('search') if search_query: queryset = queryset.filter( Q(subject__icontains=search_query) | Q(caller_name__icontains=search_query) | Q(patient__mrn__icontains=search_query) ) # Date range date_from = request.GET.get('date_from') if date_from: queryset = queryset.filter(call_started_at__gte=date_from) date_to = request.GET.get('date_to') if date_to: queryset = queryset.filter(call_started_at__lte=date_to) # Ordering queryset = queryset.order_by('-call_started_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(), 'low_rating': queryset.filter(is_low_rating=True).count(), 'avg_satisfaction': queryset.filter(satisfaction_rating__isnull=False).aggregate( avg=Avg('satisfaction_rating') )['avg'] or 0, } context = { 'page_obj': page_obj, 'interactions': page_obj.object_list, 'stats': stats, 'hospitals': hospitals, 'filters': request.GET, } return render(request, 'callcenter/interaction_list.html', context) @login_required def interaction_detail(request, pk): """Call center interaction detail view""" interaction = get_object_or_404( CallCenterInteraction.objects.select_related( 'patient', 'hospital', 'department', 'agent' ), pk=pk ) context = { 'interaction': interaction, } return render(request, 'callcenter/interaction_detail.html', context)