package inference

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

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

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

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

func (l *GetAdaptersByModelLogic) GetAdaptersByModel(req *types.GetAdaptersByModelReq) (resp *types.GetAdaptersByModelResp, err error) {
	resp = &types.GetAdaptersByModelResp{}

	adapterList, err := l.svcCtx.Scheduler.AiStorages.GetAdaptersByType("1")
	if err != nil {
		return nil, err
	}

	for _, adapter := range adapterList {
		var clusterAvail []*types.ClusterAvail
		clusters, err := l.svcCtx.Scheduler.AiStorages.GetClustersByAdapterId(adapter.Id)
		if err != nil {
			return nil, err
		}

		for _, cluster := range clusters.List {

			cmap, found := l.svcCtx.Scheduler.AiService.InferenceAdapterMap[adapter.Id]
			if !found {
				continue
			}

			iCluster, found := cmap[cluster.Id]
			if !found {
				continue
			}

			exist := iCluster.CheckModelExistence(l.ctx, req.ModelName, req.ModelType)

			if exist {
				c := &types.ClusterAvail{
					ClusterId:   cluster.Id,
					ClusterName: cluster.Name,
				}
				clusterAvail = append(clusterAvail, c)
			}
		}

		if len(clusterAvail) == 0 {
			continue
		}

		adapterAvail := &types.AdapterAvail{
			AdapterId:   adapter.Id,
			AdapterName: adapter.Name,
			Clusters:    clusterAvail,
		}

		resp.Adapters = append(resp.Adapters, adapterAvail)
	}

	return resp, nil
}
