def user_palette(request):
    if request.user.is_authenticated:
        return {"user_palette": request.user.palette}
    return {"user_palette": "palette-default"}


def navbar_context(request):
    if not request.user.is_authenticated:
        return {}
    from django.db.models import Max, Q
    from django.urls import reverse
    from apps.epic.models import Room

    recent_room = (
        Room.objects.filter(
            Q(owner=request.user) | Q(gate_slots__gamer=request.user)
        )
        .annotate(last_event=Max("events__timestamp"))
        .filter(last_event__isnull=False)
        .order_by("-last_event")
        .distinct()
        .first()
    )
    if recent_room is None:
        return {}
    if recent_room.table_status:
        url = reverse("epic:room", args=[recent_room.id])
    else:
        url = reverse("epic:gatekeeper", args=[recent_room.id])
    return {"navbar_recent_room_url": url}