from unittest import TestCase
import uuid
from pygeai.lab.managers import AILabManager
from pygeai.lab.plugins.tools.models import ToolParameter
from pygeai.core.common.exceptions import MissingRequirementException, InvalidAPIResponseException


class TestAILabSetToolPluginConfigIntegration(TestCase):

    def setUp(self):
        self.ai_lab_manager = AILabManager()
        self.plugin_id = "test-plugin-id"
        self.tool_parameters = self.__load_parameters()

    def __set_tool_plugin_config(self, plugin_id: str = None, parameters: list = None):
        return self.ai_lab_manager.set_tool_plugin_config(
            plugin_id_or_name=self.plugin_id if plugin_id is None else plugin_id,
            parameters=self.tool_parameters if parameters is None else parameters,
        )

    def __load_parameters(self):
        random_str = str(uuid.uuid4())
        return [
            ToolParameter(
                key="api_key",
                data_type="String",
                description=f"{random_str}_config",
                is_required=True,
                type="config",
                value=random_str
            )
        ]

    def test_set_tool_plugin_config(self):
        result = self.__set_tool_plugin_config()
        self.assertEqual(
            result.content,
            "Tool plugin configuration set successfully",
            "Expected successful parameter update"
        )

    def test_set_tool_plugin_config_invalid_plugin_id(self):
        invalid_id = "invalid-plugin-id-12345"

        with self.assertRaises(InvalidAPIResponseException) as context:
            self.__set_tool_plugin_config(plugin_id=invalid_id)

        self.assertIn(
            f"Unable to set configuration for tool-plugin {invalid_id}",
            str(context.exception),
            "Expected error for invalid tool-plugin ID"
        )

    def test_set_tool_plugin_config_no_plugin_id(self):
        with self.assertRaises(MissingRequirementException) as context:
            self.__set_tool_plugin_config(plugin_id="")

        self.assertIn(
            "plugin_id_or_name must be provided",
            str(context.exception),
            "Expected error for missing plugin ID"
        )

    def test_set_tool_plugin_config_no_parameters(self):
        self.tool_parameters = []

        with self.assertRaises(MissingRequirementException) as exception:
            self.__set_tool_plugin_config()
        self.assertIn(
            "Parameters list must be provided and non-empty",
            str(exception.exception),
            "Expected error when trying to send a request without parameters"
        )

    def test_set_tool_plugin_config_invalid_parameter(self):
        with self.assertRaises(InvalidAPIResponseException) as exception:
            self.__set_tool_plugin_config()
        self.assertIn(
            "Parameter api_key not found in this tool-plugin",
            str(exception.exception),
            "Expected error when trying to send an invalid parameter"
        )
