from __future__ import annotations

from dataclasses import dataclass

from .constants import MODE_ADMIN


@dataclass(frozen=True)
class AssistantCapabilityContext:
    mode: str
    business_mode_requested: bool = False
    business_mode_enabled: bool = False
    staff_allowed: bool = False

    @property
    def business_skills_allowed(self) -> bool:
        return (
            self.mode == MODE_ADMIN
            and self.business_mode_requested
            and self.business_mode_enabled
            and self.staff_allowed
        )


def build_capability_context(
    *, mode: str, settings_obj, request=None, business_mode_requested: bool = False
) -> AssistantCapabilityContext:
    user = getattr(request, "user", None)
    is_staff = bool(getattr(user, "is_staff", False))
    requires_staff = bool(
        getattr(settings_obj, "business_assistant_requires_staff", True)
    )
    staff_allowed = is_staff if requires_staff else True
    return AssistantCapabilityContext(
        mode=mode,
        business_mode_requested=business_mode_requested,
        business_mode_enabled=bool(
            getattr(settings_obj, "business_assistant_enabled", False)
        ),
        staff_allowed=staff_allowed,
    )
