"""Integration tests for YouTube service interactions."""

import pytest
from pathlib import Path

from src.services.mocks import create_mock_youtube_service
from src.services.protocols import YouTubeServiceProtocol


class TestYouTubeServiceIntegration:
    """Test YouTube service interactions and workflows."""

    @pytest.fixture
    def youtube_service(self):
        """Create mock YouTube service for testing."""
        return create_mock_youtube_service()

    @pytest.mark.asyncio
    async def test_metadata_extraction_workflow(self, youtube_service):
        """Test complete metadata extraction workflow."""
        url = "https://youtube.com/watch?v=mock123"
        metadata = await youtube_service.extract_metadata(url)
        
        assert metadata["title"] == "Mock YouTube Video"
        assert metadata["duration"] == 120
        assert metadata["channel"] == "Mock Channel"
        assert metadata["description"] == "Mock video description"
        assert metadata["upload_date"] == "2024-01-01"
        assert metadata["view_count"] == 1000
        assert metadata["like_count"] == 100

    @pytest.mark.asyncio
    async def test_batch_metadata_extraction(self, youtube_service):
        """Test batch metadata extraction with multiple URLs."""
        urls = [
            "https://youtube.com/watch?v=video1",
            "https://youtube.com/watch?v=video2",
            "https://youtube.com/watch?v=video3"
        ]
        
        results = await youtube_service.batch_extract(urls)
        assert len(results) == 3
        
        for result in results:
            assert "success" in result
            assert "url" in result
            if result["success"]:
                assert "data" in result
                assert result["data"]["title"] == "Mock YouTube Video"

    @pytest.mark.asyncio
    async def test_error_handling_in_batch_extraction(self, youtube_service):
        """Test error handling in batch extraction workflows."""
        urls = [
            "https://youtube.com/watch?v=valid1",
            "https://youtube.com/watch?v=invalid",
            "https://youtube.com/watch?v=valid2"
        ]
        
        results = await youtube_service.batch_extract(urls)
        assert len(results) == 3
        
        # Check that some succeeded and some failed
        success_count = sum(1 for r in results if r["success"])
        assert success_count > 0
        assert success_count < len(results)

    @pytest.mark.asyncio
    async def test_metadata_structure_consistency(self, youtube_service):
        """Test that metadata structure is consistent across extractions."""
        urls = [
            "https://youtube.com/watch?v=test1",
            "https://youtube.com/watch?v=test2"
        ]
        
        for url in urls:
            metadata = await youtube_service.extract_metadata(url)
            required_fields = ["title", "duration", "channel", "description", "upload_date"]
            for field in required_fields:
                assert field in metadata
                assert metadata[field] is not None

    @pytest.mark.asyncio
    async def test_service_protocol_compliance(self, youtube_service):
        """Test that YouTube service properly implements its protocol."""
        from src.services.protocols import validate_protocol_implementation, YouTubeServiceProtocol
        
        assert validate_protocol_implementation(youtube_service, YouTubeServiceProtocol)


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