package outcomes

import (
	"context"
	"fmt"

	"apps/backend/internal/logic/growthmap"
	"apps/backend/internal/middleware"
	growthDomain "apps/backend/internal/module/growth/domain"
	"apps/backend/internal/svc"
	"apps/backend/internal/types"

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

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

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

func (l *ListOutcomesLogic) ListOutcomes(req *types.ListOutcomesReq) (*types.OutcomeListData, error) {
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok || uid <= 0 {
		return nil, fmt.Errorf("unauthorized")
	}
	list, total, err := l.svcCtx.Growth.ListOutcomes(l.ctx, uid, growthDomain.OutcomeListFilter{
		Kind: req.Kind, Confidence: req.Confidence, SourceType: req.SourceType,
		From: req.From, To: req.To, Page: req.Page, PageSize: req.PageSize,
	})
	if err != nil {
		return nil, err
	}
	return &types.OutcomeListData{
		List: growthmap.OutcomeList(list),
		Pagination: growthmap.Pagination(req.Page, req.PageSize, total),
	}, nil
}
