from django.urls import reverse

from apps.accounts.models import User


def get_assignable_users(hospital):
    return (
        User.objects.filter(
            is_active=True,
            hospital=hospital,
            groups__name="PX Employee",
        )
        .select_related("department")
        .distinct()
        .order_by("first_name", "last_name")
    )


def build_public_track_url(entity_type, reference):
    try:
        from django.contrib.sites.shortcuts import get_current_site
        site = get_current_site(None)
        domain = site.domain
    except Exception:
        domain = "localhost:8000"

    path = reverse("core:public_track")
    return f"https://{domain}{path}?type={entity_type}&reference={reference}"
