/*
Copyright 2017 The Kubernetes Authors.
Copyright 2017-2024 The Volcano Authors.

Modifications made by Volcano authors:
- Added task status utility functions for preemption and completion checks

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package api

import (
	"fmt"

	v1 "k8s.io/api/core/v1"
	clientcache "k8s.io/client-go/tools/cache"

	schedulingv1beta1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
)

// PodKey returns the string key of a pod.
func PodKey(pod *v1.Pod) TaskID {
	key, err := clientcache.MetaNamespaceKeyFunc(pod)
	if err != nil {
		return TaskID(fmt.Sprintf("%v/%v", pod.Namespace, pod.Name))
	}
	return TaskID(key)
}

func getTaskStatus(pod *v1.Pod) TaskStatus {
	switch pod.Status.Phase {
	case v1.PodRunning:
		if pod.DeletionTimestamp != nil {
			return Releasing
		}

		return Running
	case v1.PodPending:
		if pod.DeletionTimestamp != nil {
			return Releasing
		}

		if len(pod.Spec.NodeName) == 0 {
			return Pending
		}
		return Bound
	case v1.PodUnknown:
		return Unknown
	case v1.PodSucceeded:
		return Succeeded
	case v1.PodFailed:
		return Failed
	}

	return Unknown
}

// PreemptableStatus checks whether the task can be preempted
func PreemptableStatus(status TaskStatus) bool {
	switch status {
	case Bound, Running:
		return true
	default:
		return false
	}
}

// AllocatedStatus checks whether the tasks has AllocatedStatus
func AllocatedStatus(status TaskStatus) bool {
	switch status {
	case Bound, Binding, Running, Allocated:
		return true
	default:
		return false
	}
}

// CompletedStatus checks whether the tasks are completed (regardless of failure or success)
func CompletedStatus(status TaskStatus) bool {
	switch status {
	case Failed, Succeeded:
		return true
	default:
		return false
	}
}

// ScheduledStatus checks whether the tasks are running, bound, failed or succeeded
func ScheduledStatus(status TaskStatus) bool {
	switch status {
	case Running, Bound, Failed, Succeeded:
		return true
	default:
		return false
	}
}

// MergeErrors is used to merge multiple errors into single error
func MergeErrors(errs ...error) error {
	msg := "errors: "

	foundErr := false
	i := 1

	for _, e := range errs {
		if e != nil {
			if foundErr {
				msg = fmt.Sprintf("%s, %d: ", msg, i)
			} else {
				msg = fmt.Sprintf("%s %d: ", msg, i)
			}

			msg = fmt.Sprintf("%s%v", msg, e)
			foundErr = true
			i++
		}
	}

	if foundErr {
		return fmt.Errorf("%s", msg)
	}

	return nil
}

// JobTerminated checks whether job was terminated.
func JobTerminated(job *JobInfo) bool {
	return job.PodGroup == nil && len(job.Tasks) == 0
}

// HasOnlyVolcanoSchedulingGate checks if a Pod has only the Volcano queue allocation gate
func HasOnlyVolcanoSchedulingGate(pod *v1.Pod) bool {
	return len(pod.Spec.SchedulingGates) == 1 &&
		pod.Spec.SchedulingGates[0].Name == schedulingv1beta1.QueueAllocationGateKey
}

// HasQueueAllocationGateAnnotation checks if a Pod has the queue allocation gate annotation
func HasQueueAllocationGateAnnotation(pod *v1.Pod) bool {
	return pod.Annotations != nil &&
		pod.Annotations[schedulingv1beta1.QueueAllocationGateKey] == "true"
}
