package utils

import (
	"kontor-api-fiber/pkg/schema"
	"time"

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

func GenerateToken(user schema.Profile) (string, error) {
	// Create the Claims
	claims := jwt.MapClaims{
		"name":  user.FirstName + ", " + user.LastName,
		"admin": true,
		"exp":   time.Now().Add(time.Hour * 72).Unix(),
	}

	// Create token
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

	// Generate encoded token and send it as response.
	t, err := token.SignedString([]byte("secret"))
	if err != nil {
		return "", err
	}
	return t, nil
}

func VerifyToken(tokenString string) (bool, error) {
	token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
		return []byte("secret"), nil
	})
	if err != nil {
		return false, err
	}

	return token.Valid, nil
}
