package utm

import (
	"context"
	"fmt"

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

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

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

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

func (l *ListUtmLinksLogic) ListUtmLinks() (*types.UtmLinkListData, error) {
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok || uid <= 0 {
		return nil, fmt.Errorf("unauthorized")
	}
	list, err := l.svcCtx.Growth.ListUtmLinks(l.ctx, uid)
	if err != nil {
		return nil, err
	}
	out := make([]types.UtmLinkPublic, 0, len(list))
	for _, u := range list {
		out = append(out, publicView(l.svcCtx.Config, u))
	}
	return &types.UtmLinkListData{List: out}, nil
}
