//go:build test
// +build test

package jwt

import (
	"sync"

	"crussell/auth"
)

const testSecret = "test-secret-key-for-testing-only"

var (
	once        sync.Once
	initialized bool
)

func init() {
	Init()
}

func Init() {
	once.Do(func() {
		if auth.TokenAuth == nil {
			auth.InitJWT(testSecret)
		}
		initialized = true
	})
}

func EnsureInitialized() {
	if !initialized {
		Init()
	}
}

func GenerateTestToken(userID, role string) string {
	EnsureInitialized()
	token, _, err := auth.GenerateToken(userID, role)
	if err != nil {
		panic("failed to generate test token: " + err.Error())
	}
	return token
}

func GenerateAdminToken() string {
	return GenerateTestToken("admintest001", "admin")
}

func GenerateVerifiedUserToken(userID string) string {
	return GenerateTestToken(userID, "verified_email")
}

func GenerateUnverifiedUserToken(userID string) string {
	return GenerateTestToken(userID, "unverified_email")
}

func GenerateUserToken(userID string) string {
	return GenerateVerifiedUserToken(userID)
}

func GetTestSecret() string {
	return testSecret
}

func SetTestSecret(secret string) {
	auth.InitJWT(secret)
}
