from typing import Optional

from rio import URL, GuardEvent

from src.ezgg_lan_manager.services.UserService import UserService
from src.ezgg_lan_manager.types.UserSession import UserSession


# Guards pages against access from users that are NOT logged in
def logged_in_guard(event: GuardEvent) -> Optional[URL]:
    try:
        _ = event.session[UserSession].user_id
        return None
    except KeyError:
        return URL("./")

# Guards pages against access from users that ARE logged in
def not_logged_in_guard(event: GuardEvent) -> Optional[URL]:
    try:
        _ = event.session[UserSession].user_id
        return URL("./")
    except KeyError:
        return None

# Guards pages against access from users that are NOT logged in and NOT team members
def team_guard(event: GuardEvent) -> Optional[URL]:
    try:
        user_id = event.session[UserSession].user_id
        is_team_member = event.session[UserSession].is_team_member
        if user_id and is_team_member:
            return None
        return URL("./")
    except KeyError:
        return URL("./")
