package user

import (
	"context"

	"github.com/perfect-panel/server/internal/model/group"
	"github.com/perfect-panel/server/internal/svc"
	"github.com/perfect-panel/server/internal/types"
	"github.com/perfect-panel/server/pkg/logger"
	"github.com/perfect-panel/server/pkg/tool"
	"github.com/perfect-panel/server/pkg/xerr"
	"github.com/pkg/errors"
)

type GetUserSubscribeLogic struct {
	logger.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// Get user subcribe
func NewGetUserSubscribeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserSubscribeLogic {
	return &GetUserSubscribeLogic{
		Logger: logger.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserSubscribeLogic) GetUserSubscribe(req *types.GetUserSubscribeListRequest) (resp *types.GetUserSubscribeListResponse, err error) {
	data, err := l.svcCtx.UserModel.QueryUserSubscribe(l.ctx, req.UserId, 0, 1, 2, 3, 4)
	if err != nil {
		l.Errorw("[GetUserSubscribeLogs] Get User Subscribe Error:", logger.Field("err", err.Error()))
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "Get User Subscribe Error")
	}

	resp = &types.GetUserSubscribeListResponse{
		List:  make([]types.UserSubscribe, 0),
		Total: int64(len(data)),
	}

	// 收集所有 node_group_id，批量查分组名
	groupIdSet := make(map[int64]struct{})
	for _, item := range data {
		if item.NodeGroupId > 0 {
			groupIdSet[item.NodeGroupId] = struct{}{}
		}
	}
	groupNames := make(map[int64]string)
	if len(groupIdSet) > 0 {
		ids := make([]int64, 0, len(groupIdSet))
		for id := range groupIdSet {
			ids = append(ids, id)
		}
		var groups []group.NodeGroup
		if err := l.svcCtx.DB.WithContext(l.ctx).Where("id IN ?", ids).Find(&groups).Error; err == nil {
			for _, g := range groups {
				groupNames[g.Id] = g.Name
			}
		}
	}

	for _, item := range data {
		var sub types.UserSubscribe
		tool.DeepCopy(&sub, item)
		sub.Short, _ = tool.FixedUniqueString(item.Token, 8, "")
		sub.NodeGroupId = item.NodeGroupId
		sub.NodeGroupName = groupNames[item.NodeGroupId]
		resp.List = append(resp.List, sub)
	}
	return
}
