// Package runall implements the logic for running commands across all units in a stack.
package runall

import (
	"context"
	"os"
	"path/filepath"

	"github.com/gruntwork-io/terragrunt/internal/runner"
	"github.com/gruntwork-io/terragrunt/internal/runner/common"
	"github.com/gruntwork-io/terragrunt/internal/stacks/clean"
	"github.com/gruntwork-io/terragrunt/internal/stacks/generate"
	"github.com/gruntwork-io/terragrunt/internal/worktrees"
	"github.com/gruntwork-io/terragrunt/pkg/config"

	"github.com/gruntwork-io/terragrunt/internal/errors"
	"github.com/gruntwork-io/terragrunt/internal/iacargs"
	"github.com/gruntwork-io/terragrunt/internal/os/stdout"
	"github.com/gruntwork-io/terragrunt/internal/report"
	"github.com/gruntwork-io/terragrunt/internal/shell"
	"github.com/gruntwork-io/terragrunt/internal/telemetry"
	"github.com/gruntwork-io/terragrunt/internal/tf"
	"github.com/gruntwork-io/terragrunt/pkg/log"
	"github.com/gruntwork-io/terragrunt/pkg/options"
)

// Known terraform commands that are explicitly not supported in run --all due to the nature of the command. This is
// tracked as a map that maps the terraform command to the reasoning behind disallowing the command in run --all.
var runAllDisabledCommands = map[string]string{
	tf.CommandNameImport:      "terraform import should only be run against a single state representation to avoid injecting the wrong object in the wrong state representation.",
	tf.CommandNameTaint:       "terraform taint should only be run against a single state representation to avoid using the wrong state address.",
	tf.CommandNameUntaint:     "terraform untaint should only be run against a single state representation to avoid using the wrong state address.",
	tf.CommandNameConsole:     "terraform console requires stdin, which is shared across all instances of run --all when multiple modules run concurrently.",
	tf.CommandNameForceUnlock: "lock IDs are unique per state representation and thus should not be run with run --all.",
}

func Run(ctx context.Context, l log.Logger, opts *options.TerragruntOptions) error {
	if opts.TerraformCommand == "" {
		return errors.New(MissingCommand{})
	}

	reason, isDisabled := runAllDisabledCommands[opts.TerraformCommand]
	if isDisabled {
		return RunAllDisabledErr{
			command: opts.TerraformCommand,
			reason:  reason,
		}
	}

	runnerOpts := []common.Option{}

	r := report.NewReport().WithWorkingDir(opts.WorkingDir)

	if l.Formatter().DisabledColors() || stdout.IsRedirected() {
		r.WithDisableColor()
	}

	if opts.ReportFormat != "" {
		r.WithFormat(opts.ReportFormat)
	}

	if opts.SummaryPerUnit {
		r.WithShowUnitLevelSummary()
	}

	if opts.ReportSchemaFile != "" {
		defer r.WriteSchemaToFile(opts.ReportSchemaFile) //nolint:errcheck
	}

	if opts.ReportFile != "" {
		defer r.WriteToFile(opts.ReportFile) //nolint:errcheck
	}

	// Skip summary for programmatic interactions:
	// - When JSON output is requested (--json or report format is JSON)
	// - When running 'output' command (typically for programmatic consumption)
	if !opts.SummaryDisable && !shouldSkipSummary(opts) {
		defer func() {
			if err := r.WriteSummary(opts.Writers.Writer); err != nil {
				l.Warnf("Failed to write summary: %v", err)
			}
		}()
	}

	gitFilters := opts.Filters.UniqueGitFilters()

	// Only create worktrees when git filter expressions are present
	var (
		wts *worktrees.Worktrees
		err error
	)
	if len(gitFilters) > 0 {
		wts, err = worktrees.NewWorktrees(ctx, l, opts.WorkingDir, gitFilters)
		if err != nil {
			return errors.Errorf("failed to create worktrees: %w", err)
		}

		defer func() {
			cleanupErr := wts.Cleanup(ctx, l)
			if cleanupErr != nil {
				l.Errorf("failed to cleanup worktrees: %v", cleanupErr)
			}
		}()
	}

	if !opts.NoStackGenerate {
		// Set the stack config path to the default location in the working directory
		opts.TerragruntStackConfigPath = filepath.Join(opts.WorkingDir, config.DefaultStackFile)

		// Clean stack folders before calling `generate` when the `--source-update` flag is passed
		if opts.SourceUpdate {
			errClean := telemetry.TelemeterFromContext(ctx).Collect(ctx, "stack_clean", map[string]any{
				"stack_config_path": opts.TerragruntStackConfigPath,
				"working_dir":       opts.WorkingDir,
			}, func(ctx context.Context) error {
				l.Debugf("Running stack clean for %s, as part of generate command", opts.WorkingDir)
				return clean.CleanStacks(l, opts)
			})
			if errClean != nil {
				return errors.Errorf("failed to clean stack directories under %q: %w", opts.WorkingDir, errClean)
			}
		}

		// Generate the stack configuration with telemetry tracking
		err = telemetry.TelemeterFromContext(ctx).Collect(ctx, "stack_generate", map[string]any{
			"stack_config_path": opts.TerragruntStackConfigPath,
			"working_dir":       opts.WorkingDir,
		}, func(ctx context.Context) error {
			return generate.GenerateStacks(ctx, l, opts, wts)
		})

		// Handle any errors during stack generation
		if err != nil {
			return errors.Errorf("failed to generate stack file: %w", err)
		}
	} else {
		l.Debugf("Skipping stack generation in %s", opts.WorkingDir)
	}

	// Pass worktrees to runner for git filter expressions
	if wts != nil && len(wts.WorktreePairs) > 0 {
		runnerOpts = append(runnerOpts, common.WithWorktrees(wts))
	}

	rnr, err := runner.NewStackRunner(ctx, l, opts, runnerOpts...)
	if err != nil {
		return err
	}

	return RunAllOnStack(ctx, l, opts, rnr, r)
}

func RunAllOnStack(ctx context.Context, l log.Logger, opts *options.TerragruntOptions, rnr common.StackRunner, r *report.Report) error {
	l.Debugf("%s", rnr.GetStack().String())

	isDestroy := opts.TerraformCliArgs.IsDestroyCommand(opts.TerraformCommand)
	if err := rnr.LogUnitDeployOrder(l, opts.TerraformCommand, isDestroy, opts.Writers.LogShowAbsPaths); err != nil {
		return err
	}

	var prompt string

	switch opts.TerraformCommand {
	case tf.CommandNameApply:
		prompt = "Are you sure you want to run 'terragrunt apply' in each unit of the run queue displayed above?"
	case tf.CommandNameDestroy:
		prompt = "WARNING: Are you sure you want to run `terragrunt destroy` in each unit of the run queue displayed above? There is no undo!"
	case tf.CommandNameState:
		prompt = "Are you sure you want to manipulate the state with `terragrunt state` in each unit of the run queue displayed above? Note that absolute paths are shared, while relative paths will be relative to each working directory."
	}

	if prompt != "" {
		shouldRunAll, err := shell.PromptUserForYesNo(ctx, l, prompt, opts.NonInteractive, opts.Writers.ErrWriter)
		if err != nil {
			return err
		}

		if !shouldRunAll {
			// We explicitly exit here to avoid running any defers that might be registered, like from the run summary.
			os.Exit(0)
		}
	}

	var runErr error

	telemetryErr := telemetry.TelemeterFromContext(ctx).Collect(ctx, "run_all_on_stack", map[string]any{
		"terraform_command": opts.TerraformCommand,
		"working_dir":       opts.WorkingDir,
	}, func(ctx context.Context) error {
		err := rnr.Run(ctx, l, opts, r)
		if err != nil {
			// At this stage, we can't handle the error any further, so we just log it and return nil.
			// After this point, we'll need to report on what happened, and we want that to happen
			// after the error summary.
			l.Errorf("Run failed: %v", err)

			// Save error to potentially return after telemetry completes
			runErr = err

			// Return nil to allow telemetry and reporting to complete
			return nil
		}

		return nil
	})

	// log telemetry error and continue execution
	if telemetryErr != nil {
		l.Warnf("Telemetry collection failed: %v", telemetryErr)
	}

	return runErr
}

// shouldSkipSummary determines if summary output should be skipped for programmatic interactions.
// Summary is skipped when:
// - The command is 'output' (typically used for programmatic consumption)
// - JSON output is requested via terraform CLI args (-json flag)
// - JSON report format is specified (--report-format=json)
func shouldSkipSummary(opts *options.TerragruntOptions) bool {
	// Skip summary for 'output' command as it's typically used programmatically
	if opts.TerraformCommand == tf.CommandNameOutput {
		return true
	}

	// Skip summary when JSON output is requested via -json flag
	if opts.TerraformCliArgs.Normalize(iacargs.SingleDashFlag).Contains(tf.FlagNameJSON) {
		return true
	}

	return false
}
