import requests

class ConvaiCharacterClient:
    """
    Python plugin client for interacting with the character-related REST endpoints
    of a Convai-like Flask backend.
    """

    def __init__(self, server_url="http://localhost:5000"):
        self.server_url = server_url.rstrip('/')

    def upload_pdf(self, file_path, character_id):
        """
        Upload a PDF file to the server to update the character's knowledge.
        
        :param file_path: Path to the PDF file.
        :param character_id: ID of the character to update.
        :return: JSON response from the server.
        """
        url = f"{self.server_url}/api/character/upload"
        with open(file_path, 'rb') as f:
            files = {'file': f}
            data = {'character_id': character_id}
            response = requests.post(url, files=files, data=data)
        response.raise_for_status()
        return response.json()

    def fetch_character(self, character_id):
        """
        Retrieve the character data by ID.
        
        :param character_id: ID of the character to fetch.
        :return: JSON response containing character details.
        """
        url = f"{self.server_url}/api/character/{character_id}"
        response = requests.get(url)
        response.raise_for_status()
        return response.json()

    def update_character(self, character_id, update_fields):
        """
        Update character attributes via POST.
        
        :param character_id: ID of the character to update.
        :param update_fields: Dict of fields to update.
        :return: JSON response from the server.
        """
        url = f"{self.server_url}/api/character/update_character/{character_id}"
        response = requests.post(url, json=update_fields)
        response.raise_for_status()
        return response.json()

    def toggle_button(self, character_id):
        """
        Toggle the button state for a character.
        
        :param character_id: ID of the character.
        :return: JSON response with the new button state.
        """
        url = f"{self.server_url}/api/character/api/toggle_button"
        payload = {'character_id': character_id}
        response = requests.post(url, json=payload)
        response.raise_for_status()
        return response.json()