from unittest.mock import MagicMock, patch

from scrobbles.scrobblers import jellyfin_scrobble_media, mopidy_scrobble_media


def test_jellyfin_scrobble_video_with_no_imdb_id():
    with patch("scrobbles.scrobblers.Video") as mock_video_class:
        mock_video_class.find_or_create.return_value = None

        post_data = {
            "ItemType": "Video",
            "Name": "Test Video",
            "Provider_imdb": "",
            "PlaybackPosition": "00:05:00",
            "NotificationType": "PlaybackProgress",
            "UtcTimestamp": "2024-01-15T10:30:00Z",
        }

        result = jellyfin_scrobble_media(post_data, 1)

        mock_video_class.find_or_create.assert_called_once_with(None)


def test_jellyfin_scrobble_media_ignores_progress_with_zero_position():

    post_data = {
        "ItemType": "Audio",
        "PlaybackPosition": "00:00:00",
        "NotificationType": "PlaybackProgress",
    }

    result = jellyfin_scrobble_media(post_data, 1)

    assert result is None


def test_mopidy_scrobble_handles_missing_mopidy_uri():

    with patch("scrobbles.scrobblers.Track") as mock_track_class:
        with patch("scrobbles.scrobblers.parse_mopidy_uri", return_value=None):
            mock_track = MagicMock()
            mock_track.scrobble_for_user = MagicMock(return_value=MagicMock())
            mock_track_class.find_or_create.return_value = mock_track

            post_data = {
                "name": "Test Song",
                "artist": "Test Artist",
                "album": "Test Album",
                "run_time": 180000,
            }

            result = mopidy_scrobble_media(post_data, 1)

            mock_track_class.find_or_create.assert_called_once_with(
                title="Test Song",
                artist_name="Test Artist",
                album_name="Test Album",
                run_time_seconds=180000,
                mbid=None,
                artist_mbid=None,
                album_mbid=None,
                trust_webhook_data=True,
            )
