"""Integration test for NFO creation with missing folder.

Tests that NFO creation works when the series folder doesn't exist yet.
"""
from pathlib import Path
from unittest.mock import AsyncMock, patch

import pytest

from src.core.services.nfo_service import NFOService
from src.core.services.tmdb_client import TMDBClient


@pytest.mark.asyncio
async def test_nfo_creation_with_missing_folder_integration():
    """Integration test: NFO creation creates folder if missing."""
    # Use actual temp directory for this test
    import tempfile
    with tempfile.TemporaryDirectory() as tmpdir:
        anime_dir = Path(tmpdir)
        serie_folder = "Test Anime Series"
        folder_path = anime_dir / serie_folder
        
        # Verify folder doesn't exist
        assert not folder_path.exists()
        
        # Create NFO service
        nfo_service = NFOService(
            tmdb_api_key="test_key",
            anime_directory=str(anime_dir),
            image_size="original"
        )
        
        # Mock TMDB responses
        mock_search = {
            "results": [{
                "id": 99999,
                "name": "Test Anime Series",
                "first_air_date": "2023-01-01",
                "overview": "Test",
                "vote_average": 8.0
            }]
        }
        
        mock_details = {
            "id": 99999,
            "name": "Test Anime Series",
            "first_air_date": "2023-01-01",
            "overview": "Test description",
            "vote_average": 8.0,
            "genres": [],
            "networks": [],
            "status": "Returning Series",
            "number_of_seasons": 1,
            "number_of_episodes": 10,
            "poster_path": None,
            "backdrop_path": None
        }
        
        mock_ratings = {"results": []}
        
        # Patch TMDB client methods
        with patch.object(
            nfo_service.tmdb_client, 'search_tv_show',
            new_callable=AsyncMock
        ) as mock_search_method, \
            patch.object(
                nfo_service.tmdb_client, 'get_tv_show_details',
                new_callable=AsyncMock
            ) as mock_details_method, \
            patch.object(
                nfo_service.tmdb_client, 'get_tv_show_content_ratings',
                new_callable=AsyncMock
            ) as mock_ratings_method, \
            patch.object(
                nfo_service, '_download_media_files',
                new_callable=AsyncMock
            ) as mock_download:
            
            mock_search_method.return_value = mock_search
            mock_details_method.return_value = mock_details
            mock_ratings_method.return_value = mock_ratings
            mock_download.return_value = {
                "poster": False,
                "logo": False,
                "fanart": False
            }
            
            # Create NFO - this should create the folder
            nfo_path = await nfo_service.create_tvshow_nfo(
                serie_name="Test Anime Series",
                serie_folder=serie_folder,
                year=2023,
                download_poster=False,
                download_logo=False,
                download_fanart=False
            )
        
        # Verify folder was created
        assert folder_path.exists(), "Series folder should have been created"
        assert folder_path.is_dir(), "Series folder should be a directory"
        
        # Verify NFO file exists
        assert nfo_path.exists(), "NFO file should exist"
        assert nfo_path.name == "tvshow.nfo", "NFO file should be named tvshow.nfo"
        assert nfo_path.parent == folder_path, "NFO should be in series folder"
        
        # Verify NFO file has content
        nfo_content = nfo_path.read_text()
        assert "<tvshow>" in nfo_content, "NFO should contain tvshow tag"
        assert "<title>Test Anime Series</title>" in nfo_content, "NFO should contain title"
        
        print(f"✓ Test passed: Folder created at {folder_path}")
        print(f"✓ NFO file created at {nfo_path}")
