package auth

import (
	"errors"
	"time"

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

type Claims struct {
	Account string `json:"account"`
	jwt.RegisteredClaims
}

func GenerateToken(secret []byte, issuer string, account string, ttl time.Duration) (string, time.Time, error) {
	if account == "" {
		return "", time.Time{}, errors.New("account is required")
	}
	expiresAt := time.Now().Add(ttl)
	claims := Claims{
		Account: account,
		RegisteredClaims: jwt.RegisteredClaims{
			Issuer:    issuer,
			Subject:   account,
			ExpiresAt: jwt.NewNumericDate(expiresAt),
			IssuedAt:  jwt.NewNumericDate(time.Now()),
		},
	}
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	signed, err := token.SignedString(secret)
	return signed, expiresAt, err
}

func ParseToken(secret []byte, issuer string, tokenString string) (*Claims, error) {
	if tokenString == "" {
		return nil, errors.New("token is required")
	}
	token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (any, error) {
		return secret, nil
	}, jwt.WithIssuer(issuer))
	if err != nil {
		return nil, err
	}
	claims, ok := token.Claims.(*Claims)
	if !ok || !token.Valid {
		return nil, errors.New("invalid token")
	}
	return claims, nil
}
