package auth

import (
	"log"
	"net/url"
	"testing"
	"time"

	"github.com/golang-jwt/jwt"
)

func BenchmarkConfirmationLink(b *testing.B) {

	type Register struct {
		Id       int64
		Password string
		platform string
		Expired  time.Time
	}

	key := "21321321"
	cl := NewConfirmationLink[Register](fusenMakeKey(key), "http://localhost:9900/api/auth/oauth2/register")
	for i := 0; i < b.N; i++ {

		uri, _ := cl.Generate(&Register{Id: 39, Password: "21dsadsad", platform: "google", Expired: time.Now()})
		u, _ := url.Parse(uri)
		token := u.Query()["token"]
		cl.Decrypt(token[0])
	}
}

func TestConfirmationLink(t *testing.T) {

	type Register struct {
		Id       int64
		Password string
		platform string
		Expired  time.Time
	}

	key := "21321321"

	cl := NewConfirmationLink[Register](fusenMakeKey(key), "http://localhost:9900/api/auth/oauth2/register")
	uri, _ := cl.Generate(&Register{Id: 39, Password: "21dsadsad", platform: "google", Expired: time.Now()})
	log.Println(uri)

	u, _ := url.Parse(uri)
	token := u.Query()["token"]
	log.Println(cl.Decrypt(token[0]))
}

const secret = "your-256-bit-secret"

func BenchmarkJWT(b *testing.B) {

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		claims := &jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + 1020213021,
			Issuer:    "test",
		}

		token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
		ss, err := token.SignedString([]byte(secret))
		if err != nil {
			b.Fatal(err)
		}

		_, err = jwt.Parse(ss, func(token *jwt.Token) (interface{}, error) {
			if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
				return nil, jwt.ErrSignatureInvalid
			}
			return []byte(secret), nil
		})
		if err != nil {
			b.Fatal(err)
		}
	}
}