"""Unit tests for service protocol definitions."""

import pytest
from typing import Any, Dict, List, Optional, Protocol, runtime_checkable
from pathlib import Path
from uuid import UUID

from src.services.protocols import (
    YouTubeServiceProtocol,
    MediaServiceProtocol,
    TranscriptionServiceProtocol,
    EnhancementServiceProtocol,
    ExportServiceProtocol,
    BatchProcessorProtocol,
    ProgressCallback,
)


class TestProtocolDefinitions:
    """Test that all service protocols are properly defined."""

    def test_youtube_service_protocol_definition(self):
        """Test YouTubeServiceProtocol has required methods."""
        # Verify protocol is runtime checkable
        assert hasattr(YouTubeServiceProtocol, '__subclasshook__')
        
        # Verify required methods exist
        methods = ['extract_metadata', 'batch_extract']
        for method in methods:
            assert hasattr(YouTubeServiceProtocol, method)

    def test_media_service_protocol_definition(self):
        """Test MediaServiceProtocol has required methods."""
        assert hasattr(MediaServiceProtocol, '__subclasshook__')
        
        methods = [
            'download_media', 'preprocess_audio', 'validate_file_size',
            'check_audio_quality', 'get_media_info', 'create_media_file_record',
            'update_media_file_status', 'get_media_file_by_id',
            'get_pending_media_files', 'get_ready_media_files',
            'process_media_pipeline', 'get_telemetry_data', 'clear_telemetry_data'
        ]
        for method in methods:
            assert hasattr(MediaServiceProtocol, method)

    def test_transcription_service_protocol_definition(self):
        """Test TranscriptionServiceProtocol has required methods."""
        assert hasattr(TranscriptionServiceProtocol, '__subclasshook__')
        
        methods = [
            'transcribe_file', 'transcribe_audio', 'create_transcription_job',
            'get_job_status', 'cancel_job'
        ]
        for method in methods:
            assert hasattr(TranscriptionServiceProtocol, method)

    def test_enhancement_service_protocol_definition(self):
        """Test EnhancementServiceProtocol has required methods."""
        assert hasattr(EnhancementServiceProtocol, '__subclasshook__')
        
        methods = [
            'initialize', 'enhance_transcript', 'enhance_transcript_batch',
            'enhance_transcription_result'
        ]
        for method in methods:
            assert hasattr(EnhancementServiceProtocol, method)

    def test_export_service_protocol_definition(self):
        """Test ExportServiceProtocol has required methods."""
        assert hasattr(ExportServiceProtocol, '__subclasshook__')
        
        methods = [
            'export_transcript', 'export_batch', 'get_supported_formats'
        ]
        for method in methods:
            assert hasattr(ExportServiceProtocol, method)

    def test_batch_processor_protocol_definition(self):
        """Test BatchProcessorProtocol has required methods."""
        assert hasattr(BatchProcessorProtocol, '__subclasshook__')
        
        methods = [
            'add_task', 'process_tasks', 'get_progress', 'cancel_task',
            'get_task_status', 'get_completed_tasks'
        ]
        for method in methods:
            assert hasattr(BatchProcessorProtocol, method)

    def test_progress_callback_protocol_definition(self):
        """Test ProgressCallback protocol definition."""
        assert hasattr(ProgressCallback, '__subclasshook__')
        assert hasattr(ProgressCallback, '__call__')


class TestProtocolTypeHints:
    """Test that protocol methods have proper type hints."""

    def test_youtube_service_method_signatures(self):
        """Test YouTubeServiceProtocol method signatures."""
        # This test ensures the protocol methods have the expected signatures
        # We can't easily test this at runtime, but we can verify the protocol exists
        assert YouTubeServiceProtocol is not None
        
        # Test that it's a Protocol
        assert issubclass(YouTubeServiceProtocol, Protocol)

    def test_media_service_method_signatures(self):
        """Test MediaServiceProtocol method signatures."""
        assert MediaServiceProtocol is not None
        assert issubclass(MediaServiceProtocol, Protocol)

    def test_transcription_service_method_signatures(self):
        """Test TranscriptionServiceProtocol method signatures."""
        assert TranscriptionServiceProtocol is not None
        assert issubclass(TranscriptionServiceProtocol, Protocol)

    def test_enhancement_service_method_signatures(self):
        """Test EnhancementServiceProtocol method signatures."""
        assert EnhancementServiceProtocol is not None
        assert issubclass(EnhancementServiceProtocol, Protocol)

    def test_export_service_method_signatures(self):
        """Test ExportServiceProtocol method signatures."""
        assert ExportServiceProtocol is not None
        assert issubclass(ExportServiceProtocol, Protocol)

    def test_batch_processor_method_signatures(self):
        """Test BatchProcessorProtocol method signatures."""
        assert BatchProcessorProtocol is not None
        assert issubclass(BatchProcessorProtocol, Protocol)


class TestProtocolCompatibility:
    """Test that existing services are compatible with protocols."""

    def test_media_service_compatibility(self):
        """Test that existing MediaService implements MediaServiceProtocol."""
        from src.services.media_service import MediaService
        
        # This test will fail if MediaService doesn't implement all required methods
        # We're testing the interface compatibility, not the actual implementation
        assert MediaService is not None

    def test_transcription_service_compatibility(self):
        """Test that existing TranscriptionService implements TranscriptionServiceProtocol."""
        from src.services.transcription_service import TranscriptionService
        
        assert TranscriptionService is not None

    def test_youtube_service_compatibility(self):
        """Test that existing YouTubeMetadataService implements YouTubeServiceProtocol."""
        from src.services.youtube_service import YouTubeMetadataService
        
        assert YouTubeMetadataService is not None

    def test_enhancement_service_compatibility(self):
        """Test that existing DeepSeekEnhancementService implements EnhancementServiceProtocol."""
        from src.services.enhancement.service import DeepSeekEnhancementService
        
        assert DeepSeekEnhancementService is not None


class TestProtocolImportability:
    """Test that all protocols can be imported correctly."""

    def test_protocol_imports(self):
        """Test that all protocols can be imported from the protocols module."""
        from src.services.protocols import (
            YouTubeServiceProtocol,
            MediaServiceProtocol,
            TranscriptionServiceProtocol,
            EnhancementServiceProtocol,
            ExportServiceProtocol,
            BatchProcessorProtocol,
            ProgressCallback,
        )
        
        # Verify all protocols are imported
        protocols = [
            YouTubeServiceProtocol,
            MediaServiceProtocol,
            TranscriptionServiceProtocol,
            EnhancementServiceProtocol,
            ExportServiceProtocol,
            BatchProcessorProtocol,
            ProgressCallback,
        ]
        
        for protocol in protocols:
            assert protocol is not None
            assert hasattr(protocol, '__subclasshook__')


if __name__ == "__main__":
    pytest.main([__file__])
