package routers

import (
	"time"

	"github.com/go-chi/jwtauth/v5"
	"github.com/tech/sendico/pkg/model"
	"github.com/tech/sendico/server/interface/api/sresponse"
	emodel "github.com/tech/sendico/server/interface/model"
)

func (er *HttpEndpointRouter) CreateAccessToken(user *model.Account) (sresponse.TokenData, error) {
	return er.CreateAccessTokenForClient(user, "")
}

func (er *HttpEndpointRouter) CreateAccessTokenForClient(user *model.Account, clientID string) (sresponse.TokenData, error) {
	ja := jwtauth.New(er.signature.Algorithm, er.signature.PrivateKey, er.signature.PublicKey)
	_, res, err := ja.Encode(emodel.Account2ClaimsForClient(user, er.config.Expiration.Account, clientID))
	token := sresponse.TokenData{
		Token:      res,
		Expiration: time.Now().Add(time.Duration(er.config.Expiration.Account) * time.Hour),
	}
	return token, err
}

func (er *HttpEndpointRouter) CreatePendingToken(user *model.Account, ttlMinutes int) (sresponse.TokenData, error) {
	ja := jwtauth.New(er.signature.Algorithm, er.signature.PrivateKey, er.signature.PublicKey)
	_, res, err := ja.Encode(emodel.PendingAccount2Claims(user, ttlMinutes))
	token := sresponse.TokenData{
		Token:      res,
		Expiration: time.Now().Add(time.Duration(ttlMinutes) * time.Minute),
	}
	return token, err
}
