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

import (
	"reflect"
	"testing"

	"github.com/spf13/viper"
	"github.com/stretchr/testify/assert"
	"go.uploadedlobster.com/scotty/internal/backends"
	"go.uploadedlobster.com/scotty/internal/backends/deezer"
	"go.uploadedlobster.com/scotty/internal/backends/dump"
	"go.uploadedlobster.com/scotty/internal/backends/funkwhale"
	"go.uploadedlobster.com/scotty/internal/backends/jspf"
	"go.uploadedlobster.com/scotty/internal/backends/lastfm"
	"go.uploadedlobster.com/scotty/internal/backends/listenbrainz"
	"go.uploadedlobster.com/scotty/internal/backends/maloja"
	"go.uploadedlobster.com/scotty/internal/backends/scrobblerlog"
	"go.uploadedlobster.com/scotty/internal/backends/spotify"
	"go.uploadedlobster.com/scotty/internal/backends/subsonic"
	"go.uploadedlobster.com/scotty/internal/config"
	"go.uploadedlobster.com/scotty/internal/models"
)

func TestResolveBackend(t *testing.T) {
	c := viper.New()
	c.Set("backend", "dump")
	service := config.NewServiceConfig("test", c)
	backend, err := backends.ResolveBackend[models.ListensImport](&service)
	assert.NoError(t, err)
	assert.IsType(t, &dump.DumpBackend{}, backend)
}

func TestResolveBackendUnknown(t *testing.T) {
	c := viper.New()
	c.Set("backend", "foo")
	service := config.NewServiceConfig("test", c)
	_, err := backends.ResolveBackend[models.ListensImport](&service)
	assert.EqualError(t, err, "unknown backend \"foo\"")
}

func TestResolveBackendInvalidInterface(t *testing.T) {
	c := viper.New()
	c.Set("backend", "dump")
	service := config.NewServiceConfig("test", c)
	_, err := backends.ResolveBackend[models.ListensExport](&service)
	assert.EqualError(t, err, "backend dump does not implement ListensExport")
}

func TestGetBackends(t *testing.T) {
	backends := backends.GetBackends()
	for _, info := range backends {
		if info.Name == "dump" {
			assert.Containsf(t, info.ImportCapabilities, "listens",
				"ImportCapabilities for \"dump\" expected to contain \"listens\"")
			assert.Emptyf(t, info.ExportCapabilities,
				"ExportCapabilities for \"dump\" expected to be empty")
			return // Finish the test
		}
	}

	// If we got here the "dump" backend was not included
	t.Errorf("GetBackends() did not return expected bacend \"dump\"")
}

func TestImplementsInterfaces(t *testing.T) {
	expectInterface[models.ListensExport](t, &deezer.DeezerApiBackend{})
	expectInterface[models.LovesExport](t, &deezer.DeezerApiBackend{})
	// expectInterface[models.LovesImport](t, &deezer.DeezerApiBackend{})

	expectInterface[models.ListensImport](t, &dump.DumpBackend{})
	expectInterface[models.LovesImport](t, &dump.DumpBackend{})

	expectInterface[models.ListensExport](t, &funkwhale.FunkwhaleApiBackend{})
	// expectInterface[models.ListensImport](t, &funkwhale.FunkwhaleApiBackend{})
	expectInterface[models.LovesExport](t, &funkwhale.FunkwhaleApiBackend{})
	// expectInterface[models.LovesImport](t, &funkwhale.FunkwhaleApiBackend{})

	// expectInterface[models.ListensExport](t, &jspf.JSPFBackend{})
	expectInterface[models.ListensImport](t, &jspf.JSPFBackend{})
	// expectInterface[models.LovesExport](t, &jspf.JSPFBackend{})
	expectInterface[models.LovesImport](t, &jspf.JSPFBackend{})

	// expectInterface[models.ListensExport](t, &lastfm.LastfmApiBackend{})
	// expectInterface[models.ListensImport](t, &lastfm.LastfmApiBackend{})
	expectInterface[models.LovesExport](t, &lastfm.LastfmApiBackend{})
	expectInterface[models.LovesImport](t, &lastfm.LastfmApiBackend{})

	expectInterface[models.ListensExport](t, &listenbrainz.ListenBrainzApiBackend{})
	expectInterface[models.ListensImport](t, &listenbrainz.ListenBrainzApiBackend{})
	expectInterface[models.LovesExport](t, &listenbrainz.ListenBrainzApiBackend{})
	expectInterface[models.LovesImport](t, &listenbrainz.ListenBrainzApiBackend{})

	expectInterface[models.ListensExport](t, &maloja.MalojaApiBackend{})
	expectInterface[models.ListensImport](t, &maloja.MalojaApiBackend{})

	expectInterface[models.ListensExport](t, &spotify.SpotifyApiBackend{})
	expectInterface[models.LovesExport](t, &spotify.SpotifyApiBackend{})
	// expectInterface[models.LovesImport](t, &spotify.SpotifyApiBackend{})

	expectInterface[models.ListensExport](t, &scrobblerlog.ScrobblerLogBackend{})
	expectInterface[models.ListensImport](t, &scrobblerlog.ScrobblerLogBackend{})

	expectInterface[models.LovesExport](t, &subsonic.SubsonicApiBackend{})
	// expectInterface[models.LovesImport](t, &subsonic.SubsonicApiBackend{})
}

func expectInterface[T interface{}](t *testing.T, backend models.Backend) {
	ok, name := backends.ImplementsInterface[T](&backend)
	if !ok {
		t.Errorf("%v expected to implement %v", reflect.TypeOf(backend).Name(), name)
	}
}