package auth

import (
	"context"
	"encoding/json/v2"
	"strconv"

	"github.com/pocketbase/pocketbase/tools/types"
	"golang.org/x/oauth2"
)

func init() {
	Providers[NameStrava] = wrapFactory(NewStravaProvider)
}

var _ Provider = (*Strava)(nil)

// NameStrava is the unique name of the Strava provider.
const NameStrava string = "strava"

// Strava allows authentication via Strava OAuth2.
type Strava struct {
	BaseProvider
}

// NewStravaProvider creates new Strava provider instance with some defaults.
func NewStravaProvider() *Strava {
	return &Strava{BaseProvider{
		ctx:         context.Background(),
		order:       25,
		logo:        `<svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" width="2500" height="2500"><path d="M0 0h16v16H0z" fill="#fc4c02"/><g fill="#fff" fill-rule="evenodd"><path d="M6.9 8.8l2.5 4.5 2.4-4.5h-1.5l-.9 1.7-1-1.7z" opacity=".6"/><path d="M7.2 2.5l3.1 6.3H4zm0 3.8l1.2 2.5H5.9z"/></g></svg>`,
		displayName: "Strava",
		pkce:        true,
		scopes: []string{
			"profile:read_all",
		},
		authURL:     "https://www.strava.com/oauth/authorize",
		tokenURL:    "https://www.strava.com/api/v3/oauth/token",
		userInfoURL: "https://www.strava.com/api/v3/athlete",
	}}
}

// FetchAuthUser returns an AuthUser instance based on the Strava's user api.
//
// API reference: https://developers.strava.com/docs/authentication/
func (p *Strava) FetchAuthUser(token *oauth2.Token) (*AuthUser, error) {
	data, err := p.FetchRawUserInfo(token)
	if err != nil {
		return nil, err
	}

	rawUser := map[string]any{}
	if err := json.Unmarshal(data, &rawUser); err != nil {
		return nil, err
	}

	extracted := struct {
		Id              int64  `json:"id"`
		FirstName       string `json:"firstname"`
		LastName        string `json:"lastname"`
		Username        string `json:"username"`
		ProfileImageURL string `json:"profile"`

		// At the time of writing, Strava OAuth2 doesn't support returning the user email address
		// Email string `json:"email"`
	}{}
	if err := json.Unmarshal(data, &extracted); err != nil {
		return nil, err
	}

	user := &AuthUser{
		Name:         extracted.FirstName + " " + extracted.LastName,
		Username:     extracted.Username,
		AvatarURL:    extracted.ProfileImageURL,
		RawUser:      rawUser,
		AccessToken:  token.AccessToken,
		RefreshToken: token.RefreshToken,
	}

	user.Expiry, _ = types.ParseDateTime(token.Expiry)

	if extracted.Id != 0 {
		user.Id = strconv.FormatInt(extracted.Id, 10)
	}

	return user, nil
}
