"""
Account API for GetBiji.

Provides access to user account information.
"""

from typing import TYPE_CHECKING

from .models import User

if TYPE_CHECKING:
    from .client import GetBijiClient


class AccountAPI:
    """
    API for user account operations.

    This class provides methods to retrieve user account information
    including profile, VIP status, quotas, and permissions.
    """

    # API endpoint
    USER_INFO_URL = "https://get-notes.luojilab.com/voicenotes/web/user/info"

    def __init__(self, client: "GetBijiClient"):
        """
        Initialize AccountAPI.

        Args:
            client: GetBijiClient instance
        """
        self._client = client

    def get_info(self) -> User:
        """
        Get current user account information.

        Returns:
            User object with account details including:
            - uid: User ID
            - nickname: Display name
            - avatar: Avatar URL
            - cellphone: Phone number
            - is_vip: VIP membership status
            - surplus_days: Remaining VIP days
            - can_create_topics: Whether user can create knowledge bases
            - audio_duration_ms: Audio duration limit
            - remained_audio_duration: Remaining audio quota
            - create_time: Account creation timestamp

        Raises:
            AuthenticationError: If authentication fails
            APIError: If API returns an error

        Example:
            >>> user = client.account.get_info()
            >>> print(f"Hello, {user.nickname}")
            >>> if user.is_vip:
            ...     print(f"VIP: {user.surplus_days} days remaining")
        """
        response = self._client.get(self.USER_INFO_URL)
        data = response.get("c", {})
        user_data = data.get("data", {})

        return User.from_dict(user_data)

    def is_vip(self) -> bool:
        """
        Check if current user has VIP membership.

        Returns:
            True if user has active VIP membership

        Example:
            >>> if client.account.is_vip():
            ...     print("You have VIP benefits!")
        """
        user = self.get_info()
        return user.is_vip

    def get_remaining_audio_quota(self) -> int:
        """
        Get remaining audio upload quota in milliseconds.

        Returns:
            Remaining audio duration in milliseconds

        Example:
            >>> quota = client.account.get_remaining_audio_quota()
            >>> print(f"Remaining: {quota / 1000 / 60:.1f} minutes")
        """
        user = self.get_info()
        return user.remained_audio_duration

    def can_create_knowledge_bases(self) -> bool:
        """
        Check if user can create knowledge bases.

        Returns:
            True if user has permission to create knowledge bases

        Example:
            >>> if client.account.can_create_knowledge_bases():
            ...     kb = client.knowledge.create("My Notes")
        """
        user = self.get_info()
        return user.can_create_topics
