"""
Tests for the data loader utility.
"""

import unittest
from pathlib import Path
import tempfile
import json
import sys
import os

# Add src to path for imports
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))

from src.utils.data_loader import DataLoader


class TestDataLoader(unittest.TestCase):
    """Test cases for DataLoader class."""
    
    def setUp(self):
        """Set up test fixtures."""
        self.temp_dir = tempfile.mkdtemp()
        self.data_loader = DataLoader(self.temp_dir)
        
        # Create test data files
        self.create_test_artists_file()
        self.create_test_recordings_file()
    
    def tearDown(self):
        """Clean up test fixtures."""
        import shutil
        shutil.rmtree(self.temp_dir)
    
    def create_test_artists_file(self):
        """Create a test artists JSON file."""
        test_artists = {
            "artists": {
                "Test Artist": "test-artist-mbid-123",
                "Another Artist": "test-artist-mbid-456"
            },
            "name_variations": {
                "TEST": "Test",
                "ft.": "feat."
            },
            "metadata": {
                "description": "Test artist data",
                "version": "1.0"
            }
        }
        
        artists_file = Path(self.temp_dir) / "known_artists.json"
        with open(artists_file, 'w') as f:
            json.dump(test_artists, f)
    
    def create_test_recordings_file(self):
        """Create a test recordings JSON file."""
        test_recordings = {
            "recordings": [
                {
                    "artist_mbid": "test-artist-mbid-123",
                    "title": "Test Song",
                    "recording_mbid": "test-recording-mbid-789"
                }
            ],
            "metadata": {
                "description": "Test recording data",
                "version": "1.0"
            }
        }
        
        recordings_file = Path(self.temp_dir) / "known_recordings.json"
        with open(recordings_file, 'w') as f:
            json.dump(test_recordings, f)
    
    def test_load_known_artists(self):
        """Test loading known artists from JSON file."""
        artists = self.data_loader.load_known_artists()
        
        self.assertIn("Test Artist", artists)
        self.assertEqual(artists["Test Artist"], "test-artist-mbid-123")
        self.assertEqual(len(artists), 2)
    
    def test_load_name_variations(self):
        """Test loading name variations from JSON file."""
        variations = self.data_loader.load_name_variations()
        
        self.assertIn("TEST", variations)
        self.assertEqual(variations["TEST"], "Test")
        self.assertEqual(len(variations), 2)
    
    def test_load_known_recordings(self):
        """Test loading known recordings from JSON file."""
        recordings = self.data_loader.load_known_recordings()
        
        expected_key = ("test-artist-mbid-123", "Test Song")
        self.assertIn(expected_key, recordings)
        self.assertEqual(recordings[expected_key], "test-recording-mbid-789")
        self.assertEqual(len(recordings), 1)
    
    def test_missing_files_graceful_handling(self):
        """Test graceful handling of missing data files."""
        # Create a new data loader with empty directory
        empty_dir = tempfile.mkdtemp()
        empty_loader = DataLoader(empty_dir)
        
        # Should not raise exceptions
        artists = empty_loader.load_known_artists()
        recordings = empty_loader.load_known_recordings()
        variations = empty_loader.load_name_variations()
        
        self.assertEqual(artists, {})
        self.assertEqual(recordings, {})
        self.assertEqual(variations, {})
        
        # Clean up
        import shutil
        shutil.rmtree(empty_dir)
    
    def test_reload_data(self):
        """Test reloading data from files."""
        # Load data initially
        initial_artists = self.data_loader.load_known_artists()
        self.assertEqual(len(initial_artists), 2)
        
        # Reload data
        self.data_loader.reload_data()
        
        # Should still have the same data
        reloaded_artists = self.data_loader.load_known_artists()
        self.assertEqual(reloaded_artists, initial_artists)


if __name__ == '__main__':
    unittest.main() 