package updater

import (
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/storeLink"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
	"net/http"
	"strconv"
)

func UpdateDeployInstanceStatus(svc *svc.ServiceContext, instance *models.AiInferDeployInstance) {
	amap, found := svc.Scheduler.AiService.InferenceAdapterMap[strconv.FormatInt(instance.AdapterId, 10)]
	if !found {
		return
	}
	cmap, found := amap[strconv.FormatInt(instance.ClusterId, 10)]
	if !found {
		return
	}
	h := http.Request{}
	ins, err := cmap.GetInferDeployInstance(h.Context(), instance.InstanceId)
	if err != nil {
		return
	}
	switch instance.ClusterType {
	case storeLink.TYPE_OCTOPUS:
		switch ins.Status {
		case "running":
			instance.Status = constants.Running
		case "stopped":
			instance.Status = constants.Stopped
		default:
			instance.Status = ins.Status
		}

	case storeLink.TYPE_SHUGUANGAI:
		switch ins.Status {
		case "Running":
			instance.Status = constants.Running
		case "Terminated":
			instance.Status = constants.Stopped
		default:
			instance.Status = ins.Status
		}
	}
	err = svc.Scheduler.AiStorages.UpdateInferDeployInstance(instance)
	if err != nil {
		return
	}
}
