"""Premium capability checks (single place for all entitlement decisions).

Every premium *behaviour* in the app asks a question here instead of reading the
subscription state directly. Today each capability maps 1:1 to "does this user
have an active subscription", but centralising the checks means future
subscription *tiers* (e.g. a seller-only or messaging-only plan) can change what
each capability returns **without touching the call sites** in chat, ads, the
marketplace or templates.

All checks resolve through :func:`app.premium.services.is_user_premium`, which is
the single source of truth (`status == ACTIVE` and not expired). They are safe to
call with an anonymous user (returns ``False``).
"""

from __future__ import annotations

from app.premium.services import is_user_currently_premium


def has_premium(user) -> bool:
    """True when ``user`` is an authenticated member with active premium.

    Resolves through :func:`is_user_currently_premium` (an ACTIVE subscription
    whose ``end_date`` is still in the future) — never a stored flag — so feature
    capabilities fail automatically the moment premium lapses.
    """
    if not getattr(user, "is_authenticated", False):
        return False
    return is_user_currently_premium(user)


def can_send_unlimited_messages(user) -> bool:
    """Premium members bypass the per-conversation free message quota entirely."""
    return has_premium(user)


def can_view_without_ads(user) -> bool:
    """Premium members get an ad-free experience (banner + rewarded prompts)."""
    return has_premium(user)


def should_show_premium_badge(user) -> bool:
    """Whether to render the premium badge for ``user`` (member or seller)."""
    return has_premium(user)


def can_feature_listing(user) -> bool:
    """Whether ``user`` may feature a listing at all (premium capability)."""
    return has_premium(user)


def max_featured_listings(user) -> int:
    """How many listings ``user`` may feature at once.

    Centralises the "one featured listing" limit so a future higher tier can
    simply return a larger number without changing any feature logic.
    """
    return 1 if has_premium(user) else 0
