from .base_agent import BaseAgent
from ..aws_connector import AWSConnector

class ComprehendAgent(BaseAgent):
    """
    An agent specialized in handling AWS Comprehend tasks.
    """

    def execute(self, command: str, **kwargs):
        """
        Executes a given command related to Comprehend.
        """
        if command == 'detect_sentiment':
            return self._detect_sentiment(**kwargs)
        else:
            raise NotImplementedError(f"Command '{command}' is not supported by ComprehendAgent.")

    def _detect_sentiment(self, region: str, text: str, language_code: str = 'en'):
        """
        Detects the sentiment of a given text.
        """
        print(f"ComprehendAgent: Detecting sentiment for text in region {region}...")
        try:
            comprehend_client = AWSConnector.get_client('comprehend', region_name=region)

            response = comprehend_client.detect_sentiment(
                Text=text,
                LanguageCode=language_code
            )

            sentiment = response['Sentiment']
            sentiment_score = response['SentimentScore']

            return {"status": "success", "message": f"Sentiment detected for text.", "sentiment": sentiment, "sentiment_score": sentiment_score}
        except Exception as e:
            print(f"Error detecting sentiment: {e}")
            return {"status": "error", "message": str(e)}
