from .core import (
    get_user_info,
    get_user_groups,
    get_owned_groups,
    username_to_id,
    get_user_rap,
    get_full_body_thumbnail,
    get_last_location,
    get_friends_count,
    is_email_verified,
    is_premium,
    get_followers_count,
    get_following_count
)

state = {}

def init():
    state["ready"] = True

def search(username):
    if not state.get("ready"):
        raise RuntimeError("not initialized")

    userdata = get_user_info(username)
    if not userdata:
        return {"error": "user not found"}

    userid = username_to_id(username)
    if not userid:
        return {"error": "could not resolve user id"}

    groups = get_user_groups(username) or []
    owned = get_owned_groups(username) or []
    rap = get_user_rap(userid)
    avatar = get_full_body_thumbnail(username)
    lastlocation = get_last_location(username)
    friends = get_friends_count(username)
    email = is_email_verified(username)
    premium = is_premium(username)
    followers = get_followers_count(username)
    following = get_following_count(username)

    return {
        **userdata,
        "groupsjoined": len(groups),
        "groupsowned": len(owned),
        "rap": rap,
        "avatar": avatar,
        "lastlocation": lastlocation,
        "friendscount": friends,
        "followerscount": followers,
        "followingcount": following,
        "emailverified": email,
        "premium": premium
    }