"""Unit tests for ExcelParserService."""

import pytest
from unittest.mock import Mock, patch
from uuid import uuid4

from src.extraction_v2.excel_parser_service import (
    ExcelParserService,
    get_excel_parser_service
)


class TestExcelParserService:
    """Tests for ExcelParserService class."""

    def test_service_can_be_imported(self):
        """Test that service can be imported."""
        assert ExcelParserService is not None

    def test_factory_function_works(self):
        """Test that factory function returns service instance."""
        # Clear cache
        get_excel_parser_service.cache_clear()

        with patch('src.extraction_v2.excel_parser_service.get_excel_image_extraction_service'), \
             patch('src.extraction_v2.excel_parser_service.ShapeExtractor'):
            service = get_excel_parser_service()

            assert service is not None
            assert isinstance(service, ExcelParserService)

    def test_service_initializes_with_both_dependencies(self):
        """Test that service initializes with both image and shape dependencies."""
        mock_image_service = Mock()
        mock_shape_extractor = Mock()

        service = ExcelParserService(
            image_extraction_service=mock_image_service,
            shape_extractor=mock_shape_extractor
        )

        assert service.image_extraction_service is mock_image_service
        assert service.shape_extractor is mock_shape_extractor

    def test_parse_excel_file_signature_is_correct(self):
        """Test that parse_excel_file has correct signature (simplified)."""
        mock_image_service = Mock()
        mock_shape_extractor = Mock()

        service = ExcelParserService(
            image_extraction_service=mock_image_service,
            shape_extractor=mock_shape_extractor
        )

        # Check method signature using inspect
        import inspect
        sig = inspect.signature(service.parse_excel_file)
        params = sig.parameters

        # Should have these parameters
        assert 'file_content' in params
        assert 'file_id' in params
        assert 'original_filename' in params
        assert 'max_characters' in params

        # Should NOT have these parameters (removed)
        assert 'replace_images_with_descriptions' not in params
        assert 'large_file_threshold' not in params
        assert 'description_detail' not in params
        assert 'unique_filename' not in params
        assert 'use_pipe_separators' not in params

    def test_image_preprocessing_methods_exist(self):
        """Test that image pre-processing methods exist."""
        mock_image_service = Mock()
        mock_shape_extractor = Mock()

        service = ExcelParserService(
            image_extraction_service=mock_image_service,
            shape_extractor=mock_shape_extractor
        )

        # Check that image preprocessing methods exist
        assert hasattr(service, '_extract_images_with_descriptions')
        assert hasattr(service, '_create_position_descriptions_map')
        assert hasattr(service, '_replace_images_with_descriptions')

    def test_uses_docling_converter(self):
        """Test that service uses Docling DocumentConverter."""
        mock_image_service = Mock()
        mock_shape_extractor = Mock()

        with patch('src.extraction_v2.excel_parser_service.DocumentConverter'):
            service = ExcelParserService(
                image_extraction_service=mock_image_service,
                shape_extractor=mock_shape_extractor
            )

            assert hasattr(service, 'converter')
