from unittest import TestCase
from pygeai.lab.managers import AILabManager
from pygeai.lab.plugins.tools.models import ToolPluginList
from pygeai.lab.models import FilterSettings
import copy


class TestAILabListToolPluginsIntegration(TestCase):

    def setUp(self):
        self.ai_lab_manager = AILabManager()
        self.filter_settings = FilterSettings(
            allow_external=False,
            allow_drafts=True,
            access_scope="private"
        )

    def __list_tool_plugins(self, filter_settings: FilterSettings = None):
        filter_settings = filter_settings if filter_settings != None else self.filter_settings
        return self.ai_lab_manager.list_tool_plugins(filter_settings=filter_settings)

    def test_private_list_tool_plugins(self):
        result = self.__list_tool_plugins()
        self.assertIsInstance(result, ToolPluginList, "Expected a list of tool-plugins")

        for plugin in result.plugins:
            self.assertTrue(plugin.access_scope == "private", "Expected all tool-plugins to be private")

    def test_public_list_tool_plugins(self):
        filter_settings = copy.deepcopy(self.filter_settings)
        filter_settings.access_scope = "public"

        result = self.__list_tool_plugins(filter_settings=filter_settings)
        self.assertIsInstance(result, ToolPluginList, "Expected a list of tool-plugins")

        for plugin in result.plugins:
            self.assertTrue(plugin.access_scope == "public", "Expected all tool-plugins to be public")

    def test_list_tool_plugins_small_count(self):
        filter_settings = copy.deepcopy(self.filter_settings)
        filter_settings.count = 2

        result = self.__list_tool_plugins(filter_settings=filter_settings)
        self.assertIsInstance(result, ToolPluginList, "Expected a list of tool-plugins")

        self.assertLessEqual(len(result.plugins), 2, "Expected list of tool-plugins returned to be 2 or less")

    def test_list_tool_plugins_big_count(self):
        filter_settings = copy.deepcopy(self.filter_settings)
        filter_settings.count = 500

        result = self.__list_tool_plugins(filter_settings=filter_settings)
        self.assertIsInstance(result, ToolPluginList, "Expected a list of tool-plugins")

        self.assertLessEqual(len(result.plugins), 500, "Expected list of tool-plugins returned to be 500 or less")

    def test_list_tool_plugins_allowing_draft(self):
        result = self.__list_tool_plugins()
        validated = any(plugin.is_draft == True for plugin in result.plugins)
        self.assertTrue(
            validated,
            "Expected at least one tool-plugin to be a draft"
        )

    def test_list_tool_plugins_no_draft(self):
        filter_settings = copy.deepcopy(self.filter_settings)
        filter_settings.allow_drafts = False
        result = self.__list_tool_plugins(filter_settings=filter_settings)

        validated = any(plugin.is_draft == True for plugin in result.plugins)
        self.assertFalse(
            validated,
            "Expected no draft tool-plugins in the list"
        )
