package inference

import (
	"context"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"

	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"

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

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

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

func (l *InstanceCenterLogic) InstanceCenter(req *types.InstanceCenterReq) (*types.InstanceCenterResp, error) {
	// todo: add your logic here and delete this line
	var resp types.InstanceCenterResp

	var InstanceCenter *[]models.AiInstanceCenter
	var InstanceCenterLists []types.InstanceCenterList

	if req.InstanceType == 0 {
		tx := l.svcCtx.DbEngin.Raw("select * from ai_instance_center where instance_name like CONCAT(?, '%')", req.InstanceName).Scan(&InstanceCenter)
		if tx.Error == nil {
			logx.Error("find nil")
		}
	} else if req.InstanceType != 0 && req.InstanceClass == "" {
		tx := l.svcCtx.DbEngin.Raw("select * from ai_instance_center where instance_type = ? and instance_name like CONCAT(?, '%')", req.InstanceType, req.InstanceName).Scan(&InstanceCenter)
		if tx.Error == nil {
			logx.Error("find nil")

		}
	} else {
		tx := l.svcCtx.DbEngin.Raw("select * from ai_instance_center where instance_type = ? and instance_class = ? and instance_name like CONCAT(?, '%')", req.InstanceType, req.InstanceClass, req.InstanceName).Scan(&InstanceCenter)
		if tx.Error == nil {
			logx.Error("find nil")
		}
	}

	if InstanceCenter == nil {
		resp.Code = 200
		resp.Msg = "No content found"
	} else {
		var instanceCenter = *InstanceCenter

		for _, instanceCenter := range instanceCenter {
			var instanceCenterlist types.InstanceCenterList
			instanceCenterlist.InstanceName = instanceCenter.InstanceName
			instanceCenterlist.InstanceType = instanceCenter.InstanceType
			instanceCenterlist.InstanceClass = instanceCenter.InstanceClass
			instanceCenterlist.Description = instanceCenter.Description
			instanceCenterlist.InstanceClassChinese = instanceCenter.InstanceClassChinese
			instanceCenterlist.Version = instanceCenter.Version
			instanceCenterlist.LogoPath = instanceCenter.LogoPath
			InstanceCenterLists = append(InstanceCenterLists, instanceCenterlist)
		}
		resp.Code = 200
		resp.Msg = "success"
		resp.InstanceCenterList = InstanceCenterLists
		resp.TotalCount = len(InstanceCenterLists)
	}

	return &resp, nil
}
