/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>

This file is part of Scotty.

Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.

Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/

package spotify_test

import (
	"encoding/json"
	"os"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.uploadedlobster.com/scotty/backends/spotify"
)

func TestSpotifyListenAsListen(t *testing.T) {
	data, err := os.ReadFile("testdata/listen.json")
	require.NoError(t, err)
	spListen := spotify.Listen{}
	err = json.Unmarshal(data, &spListen)
	require.NoError(t, err)
	listen := spListen.AsListen()
	listenedAt, _ := time.Parse(time.RFC3339, "2023-11-21T15:24:33.361Z")
	assert.Equal(t, listenedAt, listen.ListenedAt)
	assert.Equal(t, time.Duration(413826*time.Millisecond), listen.Duration)
	assert.Equal(t, "Oweynagat", listen.TrackName)
	assert.Equal(t, "Here Now, There Then", listen.ReleaseName)
	assert.Equal(t, []string{"Dool"}, listen.ArtistNames)
	assert.Equal(t, 5, listen.TrackNumber)
	assert.Equal(t, 1, listen.DiscNumber)
	assert.Equal(t, "DES561620801", listen.ISRC)
	info := listen.AdditionalInfo
	assert.Equal(t, "spotify.com", info["music_service"])
	assert.Equal(t, "https://open.spotify.com/track/2JKUgGuXK3dEvyuIJ4Yj2V", info["origin_url"])
	assert.Equal(t, "https://open.spotify.com/track/2JKUgGuXK3dEvyuIJ4Yj2V", info["spotify_id"])
	assert.Equal(t, "https://open.spotify.com/album/5U1umzRH4EONHWsFgPtRbA", info["spotify_album_id"])
	assert.Equal(t, []string{"https://open.spotify.com/artist/101HSR6JTJqe3DBh6rb8kz"}, info["spotify_artist_ids"])
	assert.Equal(t, []string{"https://open.spotify.com/artist/101HSR6JTJqe3DBh6rb8kz"}, info["spotify_album_artist_ids"])
}

func TestSavedTrackAsLove(t *testing.T) {
	data, err := os.ReadFile("testdata/track.json")
	require.NoError(t, err)
	track := spotify.SavedTrack{}
	err = json.Unmarshal(data, &track)
	require.NoError(t, err)
	love := track.AsLove()
	created, _ := time.Parse(time.RFC3339, "2022-02-13T21:46:08Z")
	assert.Equal(t, created, love.Created)
	assert.Equal(t, time.Duration(187680*time.Millisecond), love.Duration)
	assert.Equal(t, "Death to the Holy", love.TrackName)
}