/*

 Copyright (c) [2023] [pcm]
 [pcm-coordinator] is licensed under Mulan PSL v2.
 You can use this software according to the terms and conditions of the Mulan PSL v2.
 You may obtain a copy of Mulan PSL v2 at:
          http://license.coscl.org.cn/MulanPSL2
 THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 See the Mulan PSL v2 for more details.

*/

package core

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

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

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

func (l *ListClusterLogic) ListCluster(req *types.ListClusterReq) (*types.ListClusterResp, error) {

	var clusters []types.ComputeCluster
	var clustersModel *[]models.ComputeCluster

	//var centersModel []models.ComputeCenter
	var resp types.ListClusterResp

	l.svcCtx.DbEngin.Raw("select * from compute_cluster where center_id = ?", req.CenterId).Scan(&clustersModel)

	if clustersModel == nil {
		resp.Code = 200
		resp.Msg = "success"
		resp.Data.TotalCount = 0
		return &resp, nil
	}
	var clustersModelV = *clustersModel

	for _, clusterModel := range clustersModelV {
		var cluster types.ComputeCluster
		cluster.Id = clusterModel.Id
		cluster.Name = clusterModel.Name.String
		cluster.Type = clusterModel.Type.String
		cluster.JcceDomainId = clusterModel.JcceDomainId.Int64
		cluster.JcceDomainName = clusterModel.JcceDomainName.String
		cluster.Longitude = clusterModel.Longitude.Float64
		cluster.Latitude = clusterModel.Latitude.Float64
		cluster.Description = clusterModel.Description.String
		clusters = append(clusters, cluster)
	}

	resp.Code = 200
	resp.Msg = "success"
	resp.Data.TotalCount = len(clusters)
	resp.Data.Clusters = clusters
	return &resp, nil

}
