/*
Copyright 2024 The KubeSphere Authors.

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 task

import (
	"context"
	"reflect"

	"github.com/cockroachdb/errors"
	kkcorev1alpha1 "github.com/kubesphere/kubekey/api/core/v1alpha1"
	"k8s.io/apimachinery/pkg/fields"
	"k8s.io/apimachinery/pkg/labels"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/apimachinery/pkg/util/validation/field"
	apigeneric "k8s.io/apiserver/pkg/registry/generic"
	apistorage "k8s.io/apiserver/pkg/storage"
	apinames "k8s.io/apiserver/pkg/storage/names"
	"sigs.k8s.io/structured-merge-diff/v4/fieldpath"

	_const "github.com/kubesphere/kubekey/v4/pkg/const"
)

const playbookKind = "Playbook"

// taskStrategy implements behavior for Pods
type taskStrategy struct {
	runtime.ObjectTyper
	apinames.NameGenerator
}

// Strategy is the default logic that applies when creating and updating Pod
// objects via the REST API.
var Strategy = taskStrategy{_const.Scheme, apinames.SimpleNameGenerator}

// ===CreateStrategy===

// NamespaceScoped always true
func (t taskStrategy) NamespaceScoped() bool {
	return true
}

// PrepareForCreate set tasks status to pending
func (t taskStrategy) PrepareForCreate(_ context.Context, obj runtime.Object) {
	// init status when create
	if task, ok := obj.(*kkcorev1alpha1.Task); ok {
		task.Status = kkcorev1alpha1.TaskStatus{
			Phase: kkcorev1alpha1.TaskPhasePending,
		}
	}
}

// Validate always pass
func (t taskStrategy) Validate(context.Context, runtime.Object) field.ErrorList {
	return nil
}

// WarningsOnCreate do no-thing
func (t taskStrategy) WarningsOnCreate(context.Context, runtime.Object) []string {
	return nil
}

// Canonicalize do no-thing
func (t taskStrategy) Canonicalize(runtime.Object) {}

// ===UpdateStrategy===

// AllowCreateOnUpdate always false
func (t taskStrategy) AllowCreateOnUpdate() bool {
	return false
}

// PrepareForUpdate do no-thing
func (t taskStrategy) PrepareForUpdate(context.Context, runtime.Object, runtime.Object) {}

// ValidateUpdate spec is immutable
func (t taskStrategy) ValidateUpdate(_ context.Context, obj, old runtime.Object) field.ErrorList {
	// only support update status
	task, ok := obj.(*kkcorev1alpha1.Task)
	if !ok {
		return field.ErrorList{field.InternalError(field.NewPath("spec"), errors.New("the object is not Task"))}
	}
	oldTask, ok := old.(*kkcorev1alpha1.Task)
	if !ok {
		return field.ErrorList{field.InternalError(field.NewPath("spec"), errors.New("the object is not Task"))}
	}
	if !reflect.DeepEqual(task.Spec, oldTask.Spec) {
		return field.ErrorList{field.Forbidden(field.NewPath("spec"), "spec is immutable")}
	}

	return nil
}

// WarningsOnUpdate always nil
func (t taskStrategy) WarningsOnUpdate(context.Context, runtime.Object, runtime.Object) []string {
	return nil
}

// AllowUnconditionalUpdate always true
func (t taskStrategy) AllowUnconditionalUpdate() bool {
	return true
}

// ===ResetFieldsStrategy===

// GetResetFields always nil
func (t taskStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
	return nil
}

// OwnerPlaybookIndexFunc return value ownerReference.object is playbook.
func OwnerPlaybookIndexFunc(obj any) ([]string, error) {
	task, ok := obj.(*kkcorev1alpha1.Task)
	if !ok {
		return nil, errors.New("not Task")
	}

	var index string
	for _, reference := range task.OwnerReferences {
		if reference.Kind == playbookKind {
			index = types.NamespacedName{
				Namespace: task.Namespace,
				Name:      reference.Name,
			}.String()

			break
		}
	}
	if index == "" {
		return nil, errors.New("task has no ownerReference.playbook")
	}

	return []string{index}, nil
}

// MatchTask returns a generic matcher for a given label and field selector.
func MatchTask(label labels.Selector, fd fields.Selector) apistorage.SelectionPredicate {
	return apistorage.SelectionPredicate{
		Label:       label,
		Field:       fd,
		GetAttrs:    GetAttrs,
		IndexFields: []string{"metadata.name"},
	}
}

// GetAttrs returns labels and fields of a given object for filtering purposes.
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
	task, ok := obj.(*kkcorev1alpha1.Task)
	if !ok {
		return nil, nil, errors.New("not task")
	}

	return task.ObjectMeta.Labels, ToSelectableFields(task), nil
}

// ToSelectableFields returns a field set that represents the object
func ToSelectableFields(task *kkcorev1alpha1.Task) fields.Set {
	objectMetaFieldsSet := apigeneric.ObjectMetaFieldsSet(&task.ObjectMeta, true)
	taskSpecificFieldsSet := fields.Set{
		"spec.name": task.Spec.Name,
	}
	for _, reference := range task.OwnerReferences {
		if reference.Kind == playbookKind {
			taskSpecificFieldsSet["playbook.name"] = reference.Name
			taskSpecificFieldsSet["playbook.uid"] = string(reference.UID)

			break
		}
	}

	return apigeneric.MergeFieldsSets(taskSpecificFieldsSet, objectMetaFieldsSet)
}

// NameTriggerFunc returns value metadata.namespace of given object.
func NameTriggerFunc(obj runtime.Object) string {
	task, ok := obj.(*kkcorev1alpha1.Task)
	if ok {
		return task.ObjectMeta.Name
	}

	return ""
}
