from api_modules.redis.redis_handlers import RedisHandlers


class PageHandlers:

    @classmethod
    def retrieve_valid_page_via_token(cls, access_token: str, page_url: str) -> str:
        """
        Retrieve valid page via token. {access_token: "string", page_url: "string"} | Results: str(application)
        """
        if result := RedisHandlers.get_object_from_redis(access_token=access_token):
            if result.is_employee:
                if result.selected_company and result.selected_company.reachable_app_codes:
                    if application := result.selected_company.reachable_app_codes.get(page_url, None):
                        return application
            elif result.is_occupant:
                if result.selected_occupant and result.selected_occupant.reachable_app_codes:
                    if application := result.selected_occupant.reachable_app_codes.get(page_url, None):
                        return application
        raise ValueError("EYS_0013")

    @classmethod
    def retrieve_valid_sites_via_token(cls, access_token: str) -> list:
        """
        Retrieve valid pages via token. {"access_token": "string"} | Results: list(sites)
        """
        if result := RedisHandlers.get_object_from_redis(access_token=access_token):
            if result.is_employee:
                if result.selected_company and result.selected_company.reachable_app_codes:
                    return result.selected_company.reachable_app_codes.keys()
            elif result.is_occupant:
                if result.selected_occupant and result.selected_occupant.reachable_app_codes:
                    return result.selected_occupant.reachable_app_codes.keys()
        raise ValueError("EYS_0013")
