"""Integration tests for Figma wrapper using mock API responses"""

import pytest
from unittest.mock import Mock, patch
from dss.tools.figma import FigmaWrapper, FigmaAPIError


@pytest.mark.integration
class TestFigmaWrapperWithMocks:
    """Test Figma wrapper with mocked API responses"""

    def test_init_with_mock_credentials(self, mock_figma_token, mock_figma_file_key):
        """Test initializing FigmaWrapper with mock credentials"""
        wrapper = FigmaWrapper(
            api_token=mock_figma_token,
            file_key=mock_figma_file_key,
            use_cache=False
        )

        assert wrapper.api_token == mock_figma_token
        assert wrapper.file_key == mock_figma_file_key
        assert wrapper.headers["X-Figma-Token"] == mock_figma_token

    def test_extract_themes_with_mock_response(
        self,
        mock_figma_token,
        mock_figma_file_key,
        mock_figma_response
    ):
        """Test extracting themes from mock Figma response"""
        wrapper = FigmaWrapper(
            api_token=mock_figma_token,
            file_key=mock_figma_file_key,
            use_cache=False
        )

        # Mock the API call
        with patch.object(wrapper, 'get_variables', return_value=mock_figma_response):
            themes = wrapper.extract_themes()

            # Should extract Light and Dark themes
            assert "Light" in themes
            assert "Dark" in themes

            # Check Light theme has tokens
            light_theme = themes["Light"]
            assert light_theme.name == "DSS Light"
            assert len(light_theme.tokens) > 0

            # Check Dark theme has tokens
            dark_theme = themes["Dark"]
            assert dark_theme.name == "DSS Dark"
            assert len(dark_theme.tokens) > 0

    def test_build_mode_map(self, mock_figma_token, mock_figma_file_key, mock_figma_response):
        """Test building mode ID to theme name mapping"""
        wrapper = FigmaWrapper(
            api_token=mock_figma_token,
            file_key=mock_figma_file_key
        )

        variable_collections = mock_figma_response["meta"]["variableCollections"]
        mode_map = wrapper._build_mode_map(variable_collections)

        # Should map mode IDs to names
        assert "1:0" in mode_map
        assert mode_map["1:0"] == "Light"
        assert "1:1" in mode_map
        assert mode_map["1:1"] == "Dark"

    def test_convert_figma_color_to_rgb(self, mock_figma_token, mock_figma_file_key):
        """Test converting Figma color format to RGB"""
        wrapper = FigmaWrapper(
            api_token=mock_figma_token,
            file_key=mock_figma_file_key
        )

        # Figma color format: {r: 0-1, g: 0-1, b: 0-1, a: 0-1}
        figma_color = {
            "r": 0.0,
            "g": 0.4,
            "b": 0.8,
            "a": 1.0
        }

        rgb_string = wrapper._format_value(figma_color, "color")

        # Should convert to rgb(0, 102, 204)
        assert "rgb(" in rgb_string
        assert "0" in rgb_string  # Red component
        assert "102" in rgb_string  # Green component
        assert "204" in rgb_string  # Blue component

    def test_handle_api_errors(self, mock_figma_token, mock_figma_file_key):
        """Test handling Figma API errors"""
        import requests

        wrapper = FigmaWrapper(
            api_token=mock_figma_token,
            file_key=mock_figma_file_key,
            use_cache=False
        )

        # Mock 403 Forbidden error
        with patch('requests.get') as mock_get:
            mock_response = Mock()
            mock_response.status_code = 403

            # Properly simulate HTTPError
            http_error = requests.exceptions.HTTPError()
            http_error.response = mock_response
            mock_response.raise_for_status.side_effect = http_error

            mock_get.return_value = mock_response

            with pytest.raises(FigmaAPIError) as exc_info:
                wrapper.get_variables()

            assert "Invalid Figma API token" in str(exc_info.value) or "403" in str(exc_info.value)

    def test_handle_404_not_found(self, mock_figma_token, mock_figma_file_key):
        """Test handling file not found error"""
        wrapper = FigmaWrapper(
            api_token=mock_figma_token,
            file_key=mock_figma_file_key,
            use_cache=False
        )

        # Mock 404 Not Found error
        with patch('requests.get') as mock_get:
            mock_response = Mock()
            mock_response.status_code = 404
            mock_response.raise_for_status.side_effect = Exception("404 Not Found")
            mock_get.return_value = mock_response

            with pytest.raises(FigmaAPIError) as exc_info:
                wrapper.get_variables()

            assert "not found" in str(exc_info.value).lower()


@pytest.mark.integration
class TestFigmaTokenConversion:
    """Test Figma token type conversions"""

    def test_map_figma_type_to_dtcg(self, mock_figma_token, mock_figma_file_key):
        """Test mapping Figma types to DTCG types"""
        wrapper = FigmaWrapper(
            api_token=mock_figma_token,
            file_key=mock_figma_file_key
        )

        assert wrapper._map_figma_type_to_dtcg("COLOR") == "color"
        assert wrapper._map_figma_type_to_dtcg("FLOAT") == "number"
        assert wrapper._map_figma_type_to_dtcg("STRING") == "string"
        assert wrapper._map_figma_type_to_dtcg("BOOLEAN") == "boolean"
        assert wrapper._map_figma_type_to_dtcg("UNKNOWN") == "other"

    def test_map_dtcg_type_to_category(self, mock_figma_token, mock_figma_file_key):
        """Test mapping DTCG types to DSS categories"""
        from dss.models.theme import TokenCategory

        wrapper = FigmaWrapper(
            api_token=mock_figma_token,
            file_key=mock_figma_file_key
        )

        assert wrapper._map_dtcg_type_to_category("color") == TokenCategory.COLOR
        assert wrapper._map_dtcg_type_to_category("dimension") == TokenCategory.SPACING
        assert wrapper._map_dtcg_type_to_category("fontSize") == TokenCategory.TYPOGRAPHY
        assert wrapper._map_dtcg_type_to_category("shadow") == TokenCategory.SHADOW
        assert wrapper._map_dtcg_type_to_category("unknown") == TokenCategory.OTHER
