"""Read-only query selectors for the tenants app."""

from __future__ import annotations

from django.db.models import QuerySet

from simorgh.apps.tenants.models import Tenant, TenantStatus

__all__ = [
    "get_tenant_by_slug",
    "get_tenant_by_id",
    "get_active_tenants",
]


def get_tenant_by_slug(slug: str) -> Tenant | None:
    """Return the Tenant with the given slug, or ``None`` if not found."""
    return Tenant.objects.filter(slug=slug).first()


def get_tenant_by_id(tenant_id: int) -> Tenant:
    """Return the Tenant with the given PK.

    Raises ``Tenant.DoesNotExist`` when not found.
    """
    return Tenant.objects.get(pk=tenant_id)


def get_active_tenants() -> QuerySet:
    """Return all Tenants with status=ACTIVE, ordered by slug."""
    return Tenant.objects.filter(status=TenantStatus.ACTIVE).order_by("slug")
