"""
Configuration Console UI views - System configuration management
"""
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.shortcuts import render

from apps.organizations.models import Hospital
from apps.px_action_center.models import PXActionSLAConfig, RoutingRule


@login_required
def config_dashboard(request):
    """Configuration dashboard - overview of system settings"""
    # Only PX Admins can access
    if not request.user.is_px_admin():
        from django.contrib import messages
        from django.shortcuts import redirect
        messages.error(request, "Only PX Admins can access configuration.")
        return redirect('dashboard:command-center')

    # Get counts
    sla_configs_count = PXActionSLAConfig.objects.filter(is_active=True).count()
    routing_rules_count = RoutingRule.objects.filter(is_active=True).count()
    hospitals_count = Hospital.objects.filter(status='active').count()

    context = {
        'sla_configs_count': sla_configs_count,
        'routing_rules_count': routing_rules_count,
        'hospitals_count': hospitals_count,
    }

    return render(request, 'config/dashboard.html', context)


@login_required
def sla_config_list(request):
    """SLA configurations list view"""
    # Only PX Admins can access
    if not request.user.is_px_admin():
        from django.contrib import messages
        from django.shortcuts import redirect
        messages.error(request, "Only PX Admins can access configuration.")
        return redirect('dashboard:command-center')

    queryset = PXActionSLAConfig.objects.select_related('hospital', 'department')

    # Apply filters
    hospital_filter = request.GET.get('hospital')
    if hospital_filter:
        queryset = queryset.filter(hospital_id=hospital_filter)

    is_active = request.GET.get('is_active')
    if is_active == 'true':
        queryset = queryset.filter(is_active=True)
    elif is_active == 'false':
        queryset = queryset.filter(is_active=False)

    # Ordering
    queryset = queryset.order_by('hospital', 'name')

    # 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 hospitals for filter
    hospitals = Hospital.objects.filter(status='active')

    context = {
        'page_obj': page_obj,
        'sla_configs': page_obj.object_list,
        'hospitals': hospitals,
        'filters': request.GET,
    }

    return render(request, 'config/sla_config.html', context)


@login_required
def routing_rules_list(request):
    """Routing rules list view"""
    # Only PX Admins can access
    if not request.user.is_px_admin():
        from django.contrib import messages
        from django.shortcuts import redirect
        messages.error(request, "Only PX Admins can access configuration.")
        return redirect('dashboard:command-center')

    queryset = RoutingRule.objects.select_related(
        'hospital', 'department', 'assign_to_user', 'assign_to_department'
    )

    # Apply filters
    hospital_filter = request.GET.get('hospital')
    if hospital_filter:
        queryset = queryset.filter(hospital_id=hospital_filter)

    is_active = request.GET.get('is_active')
    if is_active == 'true':
        queryset = queryset.filter(is_active=True)
    elif is_active == 'false':
        queryset = queryset.filter(is_active=False)

    # Ordering
    queryset = queryset.order_by('-priority', 'name')

    # 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 hospitals for filter
    hospitals = Hospital.objects.filter(status='active')

    context = {
        'page_obj': page_obj,
        'routing_rules': page_obj.object_list,
        'hospitals': hospitals,
        'filters': request.GET,
    }

    return render(request, 'config/routing_rules.html', context)

from django.views.decorators.csrf import csrf_exempt
from rich import print
@csrf_exempt
def test(request):
    import json
    from django.http import JsonResponse

    print(json.loads(request.body))

    return JsonResponse({'status': 'ok'})