package workspaces

import (
	"context"
	"fmt"

	"apps/backend/internal/logic/growthmap"
	"apps/backend/internal/middleware"
	"apps/backend/internal/svc"
	"apps/backend/internal/types"

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

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

func NewListDraftReviewsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListDraftReviewsLogic {
	return &ListDraftReviewsLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *ListDraftReviewsLogic) ListDraftReviews(req *types.ListDraftReviewsReq) (*types.DraftReviewListData, error) {
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok || uid <= 0 {
		return nil, fmt.Errorf("unauthorized")
	}
	list, total, err := l.svcCtx.Growth.ListReviews(l.ctx, uid, req.Status, req.Page, req.PageSize)
	if err != nil {
		return nil, err
	}
	return &types.DraftReviewListData{
		List: growthmap.ReviewList(list),
		Pagination: growthmap.Pagination(req.Page, req.PageSize, total),
	}, nil
}
