"""
Unit tests for IntegrationMapper.

python -m unittest pygeai.tests.lab.integrations.test_mappers.TestIntegrationMapper
"""

import unittest
from pygeai.lab.integrations.mappers import IntegrationMapper
from pygeai.lab.models import ToolPlugin, ToolPluginList, ToolParameter


class TestIntegrationMapper(unittest.TestCase):
    """
    Test IntegrationMapper class.

    python -m unittest pygeai.tests.lab.integrations.test_mappers.TestIntegrationMapper
    """

    def test_map_to_integration_with_plugin_key(self):
        """Test mapping integration data with 'plugin' key."""
        data = {
            "plugin": {
                "id": "test-id-123",
                "name": "Test Integration"
            }
        }

        result = IntegrationMapper.map_to_integration(data)

        self.assertIsInstance(result, ToolPlugin)
        self.assertEqual(result.id, "test-id-123")
        self.assertEqual(result.name, "Test Integration")

    def test_map_to_integration_with_toolPlugin_key(self):
        """Test mapping integration data with 'toolPlugin' key."""
        data = {
            "toolPlugin": {
                "id": "test-id-456",
                "name": "Another Integration"
            }
        }

        result = IntegrationMapper.map_to_integration(data)

        self.assertIsInstance(result, ToolPlugin)
        self.assertEqual(result.id, "test-id-456")
        self.assertEqual(result.name, "Another Integration")

    def test_map_to_integration_direct_object(self):
        """Test mapping integration data as direct object."""
        data = {
            "id": "test-id-789",
            "name": "Direct Integration"
        }

        result = IntegrationMapper.map_to_integration(data)

        self.assertIsInstance(result, ToolPlugin)
        self.assertEqual(result.id, "test-id-789")
        self.assertEqual(result.name, "Direct Integration")

    def test_map_to_integration_list_with_toolPlugins_key(self):
        """Test mapping integration list with 'toolPlugins' key (v3 format)."""
        data = {
            "toolPlugins": [
                {"id": "int-1", "name": "Integration 1"},
                {"id": "int-2", "name": "Integration 2"}
            ]
        }

        result = IntegrationMapper.map_to_integration_list(data)

        self.assertIsInstance(result, ToolPluginList)
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0].id, "int-1")
        self.assertEqual(result[0].name, "Integration 1")
        self.assertEqual(result[1].id, "int-2")
        self.assertEqual(result[1].name, "Integration 2")

    def test_map_to_integration_list_with_plugins_key(self):
        """Test mapping integration list with 'plugins' key (v4 format)."""
        data = {
            "plugins": [
                {"id": "int-3", "name": "Integration 3"},
                {"id": "int-4", "name": "Integration 4"}
            ]
        }

        result = IntegrationMapper.map_to_integration_list(data)

        self.assertIsInstance(result, ToolPluginList)
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0].id, "int-3")
        self.assertEqual(result[1].id, "int-4")

    def test_map_to_integration_list_direct_array(self):
        """Test mapping integration list as direct array."""
        data = [
            {"id": "int-5", "name": "Integration 5"},
            {"id": "int-6", "name": "Integration 6"}
        ]

        result = IntegrationMapper.map_to_integration_list(data)

        self.assertIsInstance(result, ToolPluginList)
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0].id, "int-5")
        self.assertEqual(result[1].id, "int-6")

    def test_map_to_integration_list_empty(self):
        """Test mapping empty integration list."""
        data = {"toolPlugins": []}

        result = IntegrationMapper.map_to_integration_list(data)

        self.assertIsInstance(result, ToolPluginList)
        self.assertEqual(len(result), 0)

    def test_map_to_config_with_parameters_key(self):
        """Test mapping config data with 'parameters' key."""
        data = {
            "parameters": [
                {
                    "key": "api_key",
                    "dataType": "String",
                    "isRequired": True,
                    "type": "config",
                    "value": "my-secret",
                    "fromSecret": True
                },
                {
                    "key": "endpoint",
                    "dataType": "String",
                    "isRequired": False,
                    "type": "config",
                    "value": "https://api.example.com"
                }
            ]
        }

        result = IntegrationMapper.map_to_config(data)

        self.assertIsInstance(result, list)
        self.assertEqual(len(result), 2)
        self.assertIsInstance(result[0], ToolParameter)
        self.assertEqual(result[0].key, "api_key")
        self.assertEqual(result[0].type, "config")
        self.assertTrue(result[0].from_secret)
        self.assertEqual(result[1].key, "endpoint")

    def test_map_to_config_direct_array(self):
        """Test mapping config data as direct array."""
        data = [
            {
                "key": "client_id",
                "dataType": "String",
                "isRequired": True,
                "type": "config",
                "value": "client-secret-name",
                "fromSecret": True
            }
        ]

        result = IntegrationMapper.map_to_config(data)

        self.assertIsInstance(result, list)
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].key, "client_id")
        self.assertTrue(result[0].from_secret)

    def test_map_to_config_empty(self):
        """Test mapping empty config."""
        data = {"parameters": []}

        result = IntegrationMapper.map_to_config(data)

        self.assertIsInstance(result, list)
        self.assertEqual(len(result), 0)

    def test_map_parameters(self):
        """Test _map_parameters internal method."""
        params_data = [
            {
                "key": "param1",
                "dataType": "String",
                "isRequired": False,
                "type": "config",
                "value": "value1"
            },
            {
                "key": "param2",
                "dataType": "String",
                "type": "app",
                "isRequired": True
            }
        ]

        result = IntegrationMapper._map_parameters(params_data)

        self.assertIsInstance(result, list)
        self.assertEqual(len(result), 2)
        self.assertIsInstance(result[0], ToolParameter)
        self.assertEqual(result[0].key, "param1")
        self.assertEqual(result[0].value, "value1")
        self.assertEqual(result[1].key, "param2")
        self.assertTrue(result[1].is_required)


if __name__ == "__main__":
    unittest.main()
