from .. import constants,exceptions 
from websockets.asyncio.client import connect
import aiohttp
import json

class CharMan():
    def __init__(self,client):
        self._client = client
    
    async def fetch_character_info(self,botID):
        """
        Fetch the character information from the ChattedRooms API.
        This method is used to get the character information by its ID.
        
        Args:
            botID (int): The ID of the character.
        
        Returns:
            dict: The character information.
        """
        async with self._client.session.get(f"{self._client.base_url}api/v1/characters/getchardef/{botID}", headers=self._client.headers) as response:
            if response.status == 200:
                j = json.loads(response.json()["json_content"])
                return constants.CRFullCharacter(
                    id=botID,
                    name=j.get("char_name", ""),
                    subtitle=j.get("personality", ""),
                    visibility=j.get("visibility", 0),
                    description=j.get("description", ""),
                    avatar=f"https://static.chattedrooms.com/bot-pfp-{botID}.webp",
                    tags=j.get("tags", [])
                )
            elif response.status == 404:
                raise exceptions.CharacterNotFound(f"Character with ID {botID} not found.")
            else:
                raise Exception(f"Failed to fetch character information: {response.status}")