package cli

import (
	"fmt"

	"github.com/redscaresu/infrafactory/internal/scenario"
)

func unsupportedCriteriaResult(sc scenario.Scenario, sandboxEnabled bool) ([]StageSummary, []FailureSummary, error) {
	specs, err := sc.ExecutableChecks()
	if err != nil {
		return nil, nil, fmt.Errorf("parse acceptance criteria: %w", err)
	}

	autoPassCount := 0
	stages := make([]StageSummary, 0, 1)
	failures := make([]FailureSummary, 0)
	for idx, spec := range specs {
		reason, supported, autoPass := criteriaSupportReason(spec.Type, sandboxEnabled)
		if supported {
			continue
		}
		if autoPass {
			autoPassCount++
			continue
		}

		failures = append(failures, FailureSummary{
			Layer:   "criteria",
			Stage:   "support_matrix",
			Check:   spec.Type,
			Command: "criteria support matrix",
			Detail:  fmt.Sprintf("criterion %d (%s): %s", idx+1, spec.Type, reason),
		})
	}

	if autoPassCount > 0 {
		stages = append(stages, StageSummary{
			Layer:  "criteria",
			Stage:  "support_matrix",
			Status: StageStatusSkip,
			Detail: fmt.Sprintf("%d criteria auto-passed because Layer 3 is disabled", autoPassCount),
		})
	}

	if len(failures) > 0 {
		stages = append(stages, StageSummary{
			Layer:  "criteria",
			Stage:  "support_matrix",
			Status: StageStatusSkip,
			Detail: fmt.Sprintf("%d unsupported criteria", len(failures)),
		})
	}

	if len(stages) == 0 && len(failures) == 0 {
		return nil, nil, nil
	}

	return stages, failures, nil
}

func criteriaSupportReason(criterionType string, sandboxEnabled bool) (reason string, supported bool, autoPass bool) {
	switch criterionType {
	case "policy", "destruction", "connectivity", "http_probe":
		return "", true, false
	case "dns_resolution":
		if sandboxEnabled {
			return "", true, false
		}
		return dnsResolutionAutoPassMessage(), false, true
	default:
		return "is not supported by the current criteria support matrix", false, false
	}
}

func dnsResolutionAutoPassMessage() string {
	return "currently automatically passes because Layer 3 real probes are disabled"
}
