package validate

import (
	"context"
	"fmt"

	"github.com/databricks/cli/bundle"
	"github.com/databricks/cli/libs/diag"
)

func JobClusterKeyDefined() bundle.ReadOnlyMutator {
	return &jobClusterKeyDefined{}
}

type jobClusterKeyDefined struct {
}

func (v *jobClusterKeyDefined) Name() string {
	return "validate:job_cluster_key_defined"
}

func (v *jobClusterKeyDefined) Apply(ctx context.Context, rb bundle.ReadOnlyBundle) diag.Diagnostics {
	diags := diag.Diagnostics{}

	for k, job := range rb.Config().Resources.Jobs {
		jobClusterKeys := make(map[string]bool)
		for _, cluster := range job.JobClusters {
			if cluster.JobClusterKey != "" {
				jobClusterKeys[cluster.JobClusterKey] = true
			}
		}

		for index, task := range job.Tasks {
			if task.JobClusterKey != "" {
				if _, ok := jobClusterKeys[task.JobClusterKey]; !ok {
					loc := location{
						path: fmt.Sprintf("resources.jobs.%s.tasks[%d].job_cluster_key", k, index),
						rb:   rb,
					}

					diags = diags.Append(diag.Diagnostic{
						Severity: diag.Warning,
						Summary:  fmt.Sprintf("job_cluster_key %s is not defined", task.JobClusterKey),
						Location: loc.Location(),
						Path:     loc.Path(),
					})
				}
			}
		}
	}

	return diags
}