from .base import fetch, BASE_URL
from ..models import Profile, Topbar, UserSearchResult, MostPlayedEntry

class UsersAPI:
    async def get_profile(self, user_id: str) -> Profile | None:
        data = await fetch(f"{BASE_URL}/v2/profile/{user_id}")
        return Profile.from_dict(data) if data else None

    async def get_topbar(self, user_id: str) -> Topbar | None:
        data = await fetch(f"{BASE_URL}/v2/user/{user_id}/topbar")
        return Topbar.from_dict(data) if data else None

    async def first_place_scores(self, user_id: str) -> list[Score] | None:
        data = await fetch(f"{BASE_URL}/v2/user/{user_id}/firstPlaceScores")
        from ..models import Score
        return [Score.from_dict(d) for d in data] if data else None

    async def activity_heatmap(self, user_id: str) -> dict | None:
        return await fetch(f"{BASE_URL}/v2/stats/{user_id}/charts?type=heatmap")

    async def most_played(self, user_id: str, limit: int = 50) -> list[MostPlayedEntry] | None:
        data = await fetch(f"{BASE_URL}/getMostPlayedBeatmaps/{user_id}?limit={limit}")
        return [MostPlayedEntry.from_dict(d) for d in data] if data else None

    async def mapped_beatmaps(self, user_id: str, limit: int = 50) -> list[Beatmap] | None:
        from ..models import Beatmap
        data = await fetch(f"{BASE_URL}/getBeatmaps?mapperId={user_id}&limit={limit}")
        return [Beatmap.from_dict(d) for d in data] if data else None

    async def search(self, query: str, limit: int = 10) -> list[UserSearchResult] | None:
        data = await fetch(f"{BASE_URL}/v2/users/search?query={query}&limit={limit}")
        return [UserSearchResult.from_dict(d) for d in data] if data else None