/*
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 funkwhale_test

import (
	"testing"
	"time"

	"github.com/spf13/viper"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.uploadedlobster.com/scotty/internal/backends/funkwhale"
	"go.uploadedlobster.com/scotty/internal/config"
)

func TestInitConfig(t *testing.T) {
	c := viper.New()
	c.Set("token", "thetoken")
	service := config.NewServiceConfig("test", c)
	backend := funkwhale.FunkwhaleApiBackend{}
	err := backend.InitConfig(&service)
	assert.NoError(t, err)
}

func TestFunkwhaleListeningAsListen(t *testing.T) {
	fwListen := funkwhale.Listening{
		CreationDate: "2023-11-09T23:59:29.022005Z",
		User: funkwhale.User{
			UserName: "outsidecontext",
		},
		Track: funkwhale.Track{
			Title:         "Oweynagat",
			RecordingMBID: "c0a1fc94-5f04-4a5f-bc09-e5de0c49cd12",
			Position:      5,
			DiscNumber:    1,
			Tags:          []string{"foo", "bar"},
			Artist: funkwhale.Artist{
				Name:       "Dool",
				ArtistMBID: "24412926-c7bd-48e8-afad-8a285b42e131",
			},
			Album: funkwhale.Album{
				Title:       "Here Now, There Then",
				ReleaseMBID: "d7f22677-9803-4d21-ba42-081b633a6f68",
			},
			Uploads: []funkwhale.Upload{
				{
					Duration: 414,
				},
			},
		},
	}
	listen := fwListen.AsListen()
	assert := assert.New(t)
	assert.Equal(time.Unix(1699574369, 0).Unix(), listen.ListenedAt.Unix())
	assert.Equal(fwListen.User.UserName, listen.UserName)
	assert.Equal(time.Duration(414*time.Second), listen.Duration)
	assert.Equal(fwListen.Track.Title, listen.TrackName)
	assert.Equal(fwListen.Track.Album.Title, listen.ReleaseName)
	assert.Equal([]string{fwListen.Track.Artist.Name}, listen.ArtistNames)
	assert.Equal(fwListen.Track.Position, listen.Track.TrackNumber)
	assert.Equal(fwListen.Track.DiscNumber, listen.Track.DiscNumber)
	assert.Equal(fwListen.Track.Tags, listen.Track.Tags)
	// assert.Equal(backends.FunkwhaleClientName, listen.AdditionalInfo["disc_number"])
	assert.Equal(fwListen.Track.RecordingMBID, listen.RecordingMBID)
	assert.Equal(fwListen.Track.Album.ReleaseMBID, listen.ReleaseMBID)
	assert.Equal(fwListen.Track.Artist.ArtistMBID, listen.ArtistMBIDs[0])
	assert.Equal(funkwhale.FunkwhaleClientName, listen.AdditionalInfo["media_player"])
}

func TestFunkwhaleFavoriteTrackAsLove(t *testing.T) {
	favorite := funkwhale.FavoriteTrack{
		CreationDate: "2023-11-09T23:59:29.022005Z",
		User: funkwhale.User{
			UserName: "outsidecontext",
		},
		Track: funkwhale.Track{
			Title:         "Oweynagat",
			RecordingMBID: "c0a1fc94-5f04-4a5f-bc09-e5de0c49cd12",
			Position:      5,
			DiscNumber:    1,
			Tags:          []string{"foo", "bar"},
			Artist: funkwhale.Artist{
				Name:       "Dool",
				ArtistMBID: "24412926-c7bd-48e8-afad-8a285b42e131",
			},
			Album: funkwhale.Album{
				Title:       "Here Now, There Then",
				ReleaseMBID: "d7f22677-9803-4d21-ba42-081b633a6f68",
			},
			Uploads: []funkwhale.Upload{
				{
					Duration: 414,
				},
			},
		},
	}
	love := favorite.AsLove()
	assert := assert.New(t)
	assert.Equal(time.Unix(1699574369, 0).Unix(), love.Created.Unix())
	assert.Equal(favorite.User.UserName, love.UserName)
	assert.Equal(time.Duration(414*time.Second), love.Duration)
	assert.Equal(favorite.Track.Title, love.TrackName)
	assert.Equal(favorite.Track.Album.Title, love.ReleaseName)
	assert.Equal([]string{favorite.Track.Artist.Name}, love.ArtistNames)
	assert.Equal(favorite.Track.Position, love.Track.TrackNumber)
	assert.Equal(favorite.Track.DiscNumber, love.Track.DiscNumber)
	assert.Equal(favorite.Track.Tags, love.Track.Tags)
	assert.Equal(favorite.Track.RecordingMBID, love.RecordingMBID)
	assert.Equal(favorite.Track.RecordingMBID, love.Track.RecordingMBID)
	assert.Equal(favorite.Track.Album.ReleaseMBID, love.ReleaseMBID)
	require.Len(t, love.Track.ArtistMBIDs, 1)
	assert.Equal(favorite.Track.Artist.ArtistMBID, love.ArtistMBIDs[0])
	assert.Equal(funkwhale.FunkwhaleClientName, love.AdditionalInfo["media_player"])
}