"""Unit tests for NFO service folder creation.

Tests that the NFO service correctly creates series folders when they don't exist.
"""
import tempfile
from pathlib import Path
from unittest.mock import AsyncMock, patch

import pytest

from src.core.services.nfo_service import NFOService


class TestNFOServiceFolderCreation:
    """Test NFO service creates folders when needed."""
    
    @pytest.fixture
    def temp_anime_dir(self):
        """Create temporary anime directory."""
        with tempfile.TemporaryDirectory() as tmpdir:
            yield Path(tmpdir)
    
    @pytest.fixture
    def nfo_service(self, temp_anime_dir):
        """Create NFO service with temporary directory."""
        return NFOService(
            tmdb_api_key="test_api_key",
            anime_directory=str(temp_anime_dir),
            image_size="original",
            auto_create=False
        )
    
    @pytest.mark.asyncio
    async def test_create_nfo_creates_missing_folder(
        self, nfo_service, temp_anime_dir
    ):
        """Test that create_tvshow_nfo creates folder if it doesn't exist."""
        serie_folder = "Test Series"
        folder_path = temp_anime_dir / serie_folder
        
        # Verify folder doesn't exist initially
        assert not folder_path.exists()
        
        # Mock TMDB client responses
        mock_search_results = {
            "results": [
                {
                    "id": 12345,
                    "name": "Test Series",
                    "first_air_date": "2023-01-01",
                    "overview": "Test overview",
                    "vote_average": 8.5
                }
            ]
        }
        
        mock_details = {
            "id": 12345,
            "name": "Test Series",
            "first_air_date": "2023-01-01",
            "overview": "Test overview",
            "vote_average": 8.5,
            "genres": [{"id": 16, "name": "Animation"}],
            "networks": [{"name": "Test Network"}],
            "status": "Returning Series",
            "number_of_seasons": 1,
            "number_of_episodes": 12,
            "poster_path": "/test_poster.jpg",
            "backdrop_path": "/test_backdrop.jpg"
        }
        
        mock_content_ratings = {
            "results": [
                {"iso_3166_1": "DE", "rating": "12"}
            ]
        }
        
        with patch.object(
            nfo_service.tmdb_client, 'search_tv_show',
            new_callable=AsyncMock
        ) as mock_search, \
            patch.object(
                nfo_service.tmdb_client, 'get_tv_show_details',
                new_callable=AsyncMock
            ) as mock_details_call, \
            patch.object(
                nfo_service.tmdb_client, 'get_tv_show_content_ratings',
                new_callable=AsyncMock
            ) as mock_ratings, \
            patch.object(
                nfo_service, '_download_media_files',
                new_callable=AsyncMock
            ) as mock_download:
            
            mock_search.return_value = mock_search_results
            mock_details_call.return_value = mock_details
            mock_ratings.return_value = mock_content_ratings
            mock_download.return_value = {
                "poster": False,
                "logo": False,
                "fanart": False
            }
            
            # Call create_tvshow_nfo
            nfo_path = await nfo_service.create_tvshow_nfo(
                serie_name="Test Series",
                serie_folder=serie_folder,
                year=2023,
                download_poster=False,
                download_logo=False,
                download_fanart=False
            )
        
        # Verify folder was created
        assert folder_path.exists()
        assert folder_path.is_dir()
        
        # Verify NFO file was created
        assert nfo_path.exists()
        assert nfo_path.name == "tvshow.nfo"
        assert nfo_path.parent == folder_path
