/*
Copyright 2024.

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 integrationtask

import (
	"github.com/tidbcloud/observability/pkg/common/constants"
	"github.com/tidbcloud/observability/pkg/common/deploy"
	"github.com/tidbcloud/observability/pkg/resourceworker/controllers/integrationtask/v2/task"
	"github.com/tidbcloud/observability/pkg/resourceworker/controllers/integrationtask/v2/task/legacy"
	"github.com/tidbcloud/observability/pkg/resourceworker/controllers/integrationtask/v2/task/vector"
)

// GetIntegrationTasks returns the task chain for IntegrationTask reconciliation.
// Internal tasks (LabelInternal="true"): Guardian -> ServiceAccount -> S3Bucket -> ConfigMap -> Deployment
// ServiceAccount creates Kubernetes ServiceAccount with IRSA annotation for AWS access.
// External tasks (legacy): Guardian -> S3Bucket -> IAMRole -> ConfigMap -> Deployment
// Guardian handles phase transitions, S3Bucket provisions a per-tenant destination
// bucket for exclusive bucket policy, IAMRole creates assume role for S3/OSS access,
// ConfigMap creates job manager config referencing clusters, and Deployment creates
// job manager deployment using ConfigMap and assuming IAM role.
// When LabelInternal is set to "true", uses vector task implementations (ServiceAccount, ConfigMap and Deployment).
// Otherwise, uses legacy implementations (backward compatible).
func GetIntegrationTasks(r *IntegrationTaskReconciler, c *task.Context) []deploy.Task {
	if v, ok := c.Task.Labels[constants.LabelInternal]; ok && v == "true" {
		return []deploy.Task{
			task.NewGuardian(c),
			vector.NewServiceAccount(c),
			vector.NewS3Bucket(c),
			vector.NewConfigMap(c),
			vector.NewDeployment(c),
		}
	}
	return []deploy.Task{
		task.NewGuardian(c),
		legacy.NewS3Bucket(c),
		legacy.NewIAMRole(c),
		legacy.NewConfigMap(c),
		legacy.NewDeployment(c),
	}
}
