// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1

package favorites

import (
	"context"

	"juwan-backend/app/search/api/internal/svc"
	"juwan-backend/app/search/api/internal/types"
	"juwan-backend/app/search/rpc/searchservice"
	"juwan-backend/common/utils/contextj"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddFavoriteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 添加收藏
func NewAddFavoriteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddFavoriteLogic {
	return &AddFavoriteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddFavoriteLogic) AddFavorite(req *types.FavoriteReq) (resp *types.EmptyResp, err error) {
	uid, err := contextj.UserIDFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	targetID, err := parseSnowflakeID(req.TargetId)
	if err != nil {
		return nil, err
	}

	_, err = l.svcCtx.SearchRpc.AddFavorites(l.ctx, &searchservice.AddFavoritesReq{
		UserId:     uid,
		TargetType: req.TargetType,
		TargetId:   targetID,
	})
	if err != nil {
		return nil, err
	}

	return &types.EmptyResp{}, nil
}
