# account.py
import requests
import nlpearl  # Import the main module to access the global api_key
from ._helpers import _get_api_url


class Account:
    @classmethod
    def _get_version(cls):
        """Get current API version."""
        return getattr(nlpearl, 'api_version', 'v2')
    
    @classmethod
    def _check_v2_only(cls, method_name):
        """Raise error if method is V2-only but V1 is selected."""
        if cls._get_version() == "v1":
            raise ValueError(
                f"Account.{method_name}() is only available in API v2. "
                f"Current version is v1. Set pearl.api_version = 'v2' to use this method."
            )
    
    @classmethod
    def get_account(cls):
        """
        Retrieves account details.
        
        Available in: V1 and V2
        
        Returns:
            dict: Account information including name, total agents, credit balance, and status.
        """
        if nlpearl.api_key is None:
            raise ValueError("API key is not set. Set the api_key first using 'pearl.api_key = YOUR_API_KEY'")

        headers = {"Authorization": f"Bearer {nlpearl.api_key}"}
        url = f"{_get_api_url()}/Account"
        response = requests.get(url, headers=headers)
        return response.json()
    
    @classmethod
    def get_users(cls):
        """
        Retrieves the list of users for the account.
        
        Available in: V2 only
        
        Returns:
            list: List of users with email, roleId, roleName, and createdAt.
        """
        cls._check_v2_only("get_users")
        
        if nlpearl.api_key is None:
            raise ValueError("API key is not set. Set the api_key first using 'pearl.api_key = YOUR_API_KEY'")

        headers = {"Authorization": f"Bearer {nlpearl.api_key}"}
        url = f"{_get_api_url()}/Account/Users"
        response = requests.get(url, headers=headers)
        return response.json()
    
    @classmethod
    def get_phone_numbers(cls):
        """
        Retrieves all active phone numbers of the account.
        
        Available in: V2 only
        
        Returns:
            list: List of phone numbers with id, number, and direction.
        """
        cls._check_v2_only("get_phone_numbers")
        
        if nlpearl.api_key is None:
            raise ValueError("API key is not set. Set the api_key first using 'pearl.api_key = YOUR_API_KEY'")

        headers = {"Authorization": f"Bearer {nlpearl.api_key}"}
        url = f"{_get_api_url()}/Account/PhoneNumbers"
        response = requests.get(url, headers=headers)
        return response.json()
    
    @classmethod
    def get_voices(cls):
        """
        Retrieves the available voices grouped by language.
        
        Available in: V2 only
        
        Returns:
            list: List of language groups, each containing available voices.
        """
        cls._check_v2_only("get_voices")
        
        if nlpearl.api_key is None:
            raise ValueError("API key is not set. Set the api_key first using 'pearl.api_key = YOUR_API_KEY'")

        headers = {"Authorization": f"Bearer {nlpearl.api_key}"}
        url = f"{_get_api_url()}/Account/Voices"
        response = requests.get(url, headers=headers)
        return response.json()

