"""Tests for Serie.ensure_folder_with_year() method."""

import pytest
from src.core.entities.series import Serie


class TestSerieEnsureFolderWithYear:
    """Test suite for ensure_folder_with_year method."""
    
    def test_ensure_folder_with_year_adds_year(self):
        """Test that ensure_folder_with_year adds year to folder name."""
        serie = Serie(
            key="perfect-blue",
            name="Perfect Blue",
            site="aniworld.to",
            folder="Perfect Blue",
            episodeDict={1: [1, 2, 3]},
            year=1997
        )
        
        result = serie.ensure_folder_with_year()
        
        assert result == "Perfect Blue (1997)"
        assert serie.folder == "Perfect Blue (1997)"
    
    def test_ensure_folder_with_year_already_has_year(self):
        """Test that ensure_folder_with_year doesn't duplicate year."""
        serie = Serie(
            key="blue-exorcist",
            name="Blue Exorcist",
            site="aniworld.to",
            folder="Blue Exorcist (2011)",
            episodeDict={1: [1, 2, 3]},
            year=2011
        )
        
        result = serie.ensure_folder_with_year()
        
        assert result == "Blue Exorcist (2011)"
        assert serie.folder == "Blue Exorcist (2011)"
    
    def test_ensure_folder_with_year_no_year_available(self):
        """Test that ensure_folder_with_year returns folder unchanged if no year."""
        serie = Serie(
            key="unknown-anime",
            name="Unknown Anime",
            site="aniworld.to",
            folder="Unknown Anime",
            episodeDict={1: [1, 2, 3]},
            year=None
        )
        
        result = serie.ensure_folder_with_year()
        
        assert result == "Unknown Anime"
        assert serie.folder == "Unknown Anime"
    
    def test_ensure_folder_with_year_sanitizes_name(self):
        """Test that ensure_folder_with_year uses sanitized_folder property."""
        serie = Serie(
            key="attack-on-titan",
            name="Attack on Titan: Final Season",
            site="aniworld.to",
            folder="Attack on Titan Final",  # Old folder without year
            episodeDict={1: [1, 2, 3]},
            year=2020
        )
        
        result = serie.ensure_folder_with_year()
        
        # Should use sanitized version of name_with_year
        assert "(2020)" in result
        assert serie.folder == result
        # Colon should be removed by sanitization
        assert ":" not in result
    
    def test_ensure_folder_with_year_updates_folder_property(self):
        """Test that folder property is updated when year is added."""
        serie = Serie(
            key="dororo",
            name="Dororo",
            site="aniworld.to",
            folder="Dororo",
            episodeDict={1: [1, 2, 3]},
            year=2019
        )
        
        original_folder = serie.folder
        result = serie.ensure_folder_with_year()
        
        assert original_folder == "Dororo"
        assert result == "Dororo (2019)"
        assert serie.folder == "Dororo (2019)"
        assert serie.folder != original_folder
