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

import (
	"encoding/json"
	"io"
	"net/http"
	"time"

	"go.uploadedlobster.com/scotty/internal/auth"
	"golang.org/x/oauth2"
)

type deezerStrategy struct {
	conf oauth2.Config
}

func (s deezerStrategy) Config() oauth2.Config {
	return s.conf
}

func (s deezerStrategy) AuthCodeURL(verifier string, state string) string {
	return s.conf.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(verifier))
}

func (s deezerStrategy) ExchangeToken(code auth.CodeResponse, verifier string) (*oauth2.Token, error) {
	// Deezer has a non-standard token exchange, expecting all parameters in the URL's query
	req, err := http.NewRequest(http.MethodGet, s.conf.Endpoint.TokenURL, nil)
	if err != nil {
		return nil, err
	}

	q := req.URL.Query()
	q.Add("app_id", s.conf.ClientID)
	q.Add("secret", s.conf.ClientSecret)
	q.Add("code", code.Code)
	q.Add("output", "json")
	req.URL.RawQuery = q.Encode()

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}

	reqBody, err := io.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

	token := deezerToken{}
	if err = json.Unmarshal(reqBody, &token); err != nil {
		return nil, err
	}

	return token.Token(), nil
}

type deezerToken struct {
	AccessToken string `json:"access_token"`
	ExpiresIn   int64  `json:"expires"`
}

func (t deezerToken) Token() *oauth2.Token {
	token := &oauth2.Token{AccessToken: t.AccessToken}
	if t.ExpiresIn > 0 {
		token.Expiry = time.Now().Add(time.Duration(t.ExpiresIn * time.Second.Nanoseconds()))
	}
	return token
}