package cloud

import (
	"context"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/httputils"
	v1 "k8s.io/api/core/v1"

	"github.com/zeromicro/go-zero/core/logx"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
)

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

type ParticipantResp struct {
	Code int         `json:"code"`
	Msg  string      `json:"message"`
	Data *v1.PodList `json:"data"` // 改成结构体
}

type ClusterInfo struct {
	Name   string `json:"name"`
	Server string `json:"server"`
}

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

func (l *PodsListLogic) PodsList(req *types.PodsListReq) (resp *types.PodsListResp, err error) {
	resp = &types.PodsListResp{}
	// query cluster http url.
	var clusterInfoList []ClusterInfo
	l.svcCtx.DbEngin.Raw("select ta.server,tc.name from t_adapter ta,t_cluster tc where ta.id = tc.adapter_id and   ta.resource_type = '01'").Scan(&clusterInfoList)
	for _, clusterInfo := range clusterInfoList {
		participantResp := ParticipantResp{}
		param := map[string]string{
			"clusterName": clusterInfo.Name,
		}
		httputils.HttpGetWithResult(param, clusterInfo.Server+"/api/v1/pod/list", &participantResp)

		resp.Data = append(resp.Data, participantResp.Data)
	}

	return resp, nil
}
