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 SubmitDraftReviewLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

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

func (l *SubmitDraftReviewLogic) SubmitDraftReview(req *types.SubmitDraftReviewReq) (*types.DraftReviewPublic, error) {
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok || uid <= 0 {
		return nil, fmt.Errorf("unauthorized")
	}
	r, err := l.svcCtx.Growth.SubmitReview(l.ctx, uid, req.RefType, req.RefId, req.WorkspaceId)
	if err != nil {
		return nil, err
	}
	return growthmap.Review(r), nil
}
