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

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 lastfm

import (
	"net/url"

	"github.com/shkh/lastfm-go/lastfm"
	"go.uploadedlobster.com/scotty/internal/auth"
	"golang.org/x/oauth2"
)

type lastfmStrategy struct {
	client      *lastfm.Api
	redirectUrl *url.URL
}

func (s lastfmStrategy) Config() oauth2.Config {
	return oauth2.Config{}
}

func (s lastfmStrategy) AuthCodeURL(verifier string, state string) auth.AuthUrl {
	// Last.fm does not use OAuth2, but the provided authorization flow with
	// callback URL is close enough we can shoehorn it into the existing
	// authentication strategy.
	// TODO: Investigate and use callback-less flow with api.GetAuthTokenUrl(token)
	url := s.client.GetAuthRequestUrl(s.redirectUrl.String())
	return auth.AuthUrl{
		Url:   url,
		State: "", // last.fm does not use state
		Param: "token",
	}
}

func (s lastfmStrategy) ExchangeToken(code auth.CodeResponse, verifier string) (*oauth2.Token, error) {
	// The token is directly valid
	return &oauth2.Token{AccessToken: code.Code}, nil
}