package task

import (
	"fmt"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/common"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/strategy"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/storeLink"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
)

func ValidateJobResources(resources types.JobResources, taskType string) error {
	switch taskType {
	case "training":
		if resources.ScheduleStrategy == "" {
			return fmt.Errorf("must specify ScheduleStrategy")
		}
	}

	if len(resources.Clusters) == 0 {
		return fmt.Errorf("must specify at least one cluster")
	}

	for _, c := range resources.Clusters {
		if c.ClusterID == "" {
			return fmt.Errorf("must specify clusterID")
		}

		if len(c.Resources) == 0 {
			return fmt.Errorf("cluster: %s must specify at least one compute resource", c.ClusterID)
			//return errors.Wrapf(xerr.NewErrCodeMsg(1234, fmt.Sprintf("cluster: %s must specify at least one compute resource", c.ClusterID)), "")
		}
	}

	return nil
}

func CopyParams(clusters []*strategy.AssignedCluster, clusterInfos []*types.JobClusterInfo, taskType string) []*strategy.AssignedCluster {
	var result []*strategy.AssignedCluster

	for _, c := range clusters {
		for _, info := range clusterInfos {
			if c.ClusterId == info.ClusterID {
				var envs []string
				var params []string
				for k, v := range info.Runtime.Envs {
					val := common.ConvertTypeToString(v)
					if val != "" {
						env := k + storeLink.COMMA + val
						envs = append(envs, env)
					}
				}
				for k, v := range info.Runtime.Params {
					val := common.ConvertTypeToString(v)
					if val != "" {
						p := k + storeLink.COMMA + val
						params = append(params, p)
					}
				}
				cluster := &strategy.AssignedCluster{
					ClusterId:         c.ClusterId,
					ClusterName:       c.ClusterName,
					Replicas:          c.Replicas,
					ResourcesRequired: info.Resources,
					Cmd:               info.Runtime.Command,
					Envs:              envs,
					Params:            params,
				}
				result = append(result, cluster)
			}
		}
	}

	if taskType == "inference" {
		for _, c := range clusters {
			for _, r := range result {
				if c.ClusterId == r.ClusterId {
					r.ModelId = c.ModelId
					r.ModelName = c.ModelName
					r.ImageId = c.ImageId
					r.CodeId = c.CodeId
				}
			}
		}
	}

	return result
}
