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

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

func (l *ListWorkspacesLogic) ListWorkspaces() (*types.WorkspaceListData, error) {
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok || uid <= 0 {
		return nil, fmt.Errorf("unauthorized")
	}
	list, cur, err := l.svcCtx.Growth.ListWorkspaces(l.ctx, uid)
	if err != nil {
		return nil, err
	}
	return &types.WorkspaceListData{List: growthmap.WorkspaceList(list), CurrentWorkspaceId: cur}, nil
}
