from django.conf import settings

from accounts.tour import should_offer_tour

from .version import __version__


def staff_ui(request):
    """Whether STAFF UI should render.

    Real staff can flip to "owner view" (a session flag) to see exactly what
    an owner sees — used when demoing to the client. This is a rendering
    switch, not an authorization boundary: every privileged endpoint still
    checks the real is_staff.
    """
    user = getattr(request, "user", None)
    return bool(
        user is not None and user.is_authenticated and user.is_staff
        and not request.session.get("owner_view")
    )


def document_dates(request):
    """Whether document dates may be rendered at all — see SHOW_DOCUMENT_DATES."""
    return {"show_document_dates": settings.SHOW_DOCUMENT_DATES}


def branding(request):
    """Per-instance branding — the only real tenancy.

    Every portal instance runs identical code; these values come from that
    instance's environment (PORTAL_SITE_NAME / PORTAL_ACCENT / ...).
    """
    return {
        "portal_name": settings.SITE_NAME,
        "brand_accent": settings.BRAND_ACCENT,
        "brand_accent_dark": settings.BRAND_ACCENT_DARK,
        "brand_header": settings.BRAND_HEADER,
        "brand_ground": settings.BRAND_GROUND,
        "brand_logo": settings.BRAND_LOGO,
        "brand_logo_height": settings.BRAND_LOGO_HEIGHT,
        "brand_rail_image": settings.BRAND_RAIL_IMAGE,
        "brand_font_css": settings.BRAND_FONT_CSS,
        "brand_font_serif": settings.BRAND_FONT_SERIF,
        "brand_font_sans": settings.BRAND_FONT_SANS,
        "brand_vivid": settings.BRAND_VIVID,
        "brand_rail_tile": settings.BRAND_RAIL_TILE,

        "app_version": __version__,
        "start_tour": should_offer_tour(getattr(request, "user", None)),
        "staff_ui": staff_ui(request),
        "owner_view": bool(request.session.get("owner_view")),
    }
