package auth

import (
	"context"

	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

type RefreshLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRefreshLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RefreshLogic {
	return &RefreshLogic{ctx: ctx, svcCtx: svcCtx}
}

func (l *RefreshLogic) Refresh(req *types.AuthRefreshReq) (*types.AuthTokenData, error) {
	pair, err := l.svcCtx.AuthToken.Refresh(l.ctx, req.RefreshToken)
	if err != nil {
		return nil, err
	}
	return &types.AuthTokenData{
		AccessToken:  pair.AccessToken,
		RefreshToken: pair.RefreshToken,
		ExpiresIn:    pair.ExpiresIn,
		UID:          pair.UID,
		TokenType:    pair.TokenType,
	}, nil
}
