from ..base_gcp_agent import BaseGCPAgent
from ..gcp_connector import GCPConnector
from google.cloud import language_v1

class LanguageAIAgent(BaseGCPAgent):
    """
    An agent specialized in handling GCP Cloud Natural Language AI tasks.
    """

    def execute(self, command: str, **kwargs):
        """
        Executes a given command related to Cloud Natural Language AI.
        """
        if command == 'analyze_sentiment':
            return self._analyze_sentiment(**kwargs)
        else:
            raise NotImplementedError(f"Command '{command}' is not supported by LanguageAIAgent.")

    def _analyze_sentiment(self, project_id: str, text: str):
        """
        Detects the sentiment of a given text.
        """
        print(f"LanguageAIAgent: Analyzing sentiment for text in project '{project_id}'...")
        try:
            client = language_v1.LanguageServiceClient(credentials=GCPConnector.get_credentials())

            document = language_v1.Document(
                content=text,
                type_=language_v1.Document.Type.PLAIN_TEXT
            )

            response = client.analyze_sentiment(document=document)
            sentiment = response.document_sentiment

            return {"status": "success", "message": f"Sentiment analyzed for text.", "sentiment_score": sentiment.score, "sentiment_magnitude": sentiment.magnitude}
        except Exception as e:
            print(f"Error analyzing sentiment: {e}")
            return {"status": "error", "message": str(e)}
