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

package review

import (
	"context"

	"juwan-backend/app/review/api/internal/svc"
	"juwan-backend/app/review/api/internal/types"
	reviewpb "juwan-backend/app/review/rpc/pb"

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

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

// 获取公开评价列表
func NewListReviewsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListReviewsLogic {
	return &ListReviewsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ListReviewsLogic) ListReviews(req *types.PageReq) (resp *types.ReviewListResp, err error) {
	sealed := false
	result, err := l.svcCtx.ReviewRpc.SearchReviews(l.ctx, &reviewpb.SearchReviewsReq{
		Offset: req.Offset,
		Limit:  req.Limit,
		Sealed: &sealed,
	})
	if err != nil {
		return nil, err
	}

	items := make([]types.Review, 0, len(result.GetReviews()))
	for _, r := range result.GetReviews() {
		items = append(items, toAPIReview(r))
	}
	return &types.ReviewListResp{
		Items: items,
		Meta: types.PageMeta{
			Total:  int64(len(items)),
			Offset: req.Offset,
			Limit:  req.Limit,
		},
	}, nil
}
