package toolingplan

import (
	"context"
	"fmt"
)

type goDetector struct{}

func (goDetector) detect(_ context.Context, repoRoot string, managedTools map[string]struct{}) detectionResult {
	if alreadyManaged("go", managedTools) {
		return detectionResult{Skips: []SkipNote{{Target: "go", Reason: "already managed by repo mise declarations"}}}
	}
	goMod, ok, err := readRepoFile(repoRoot, "go.mod")
	if err != nil {
		return detectionResult{Skips: []SkipNote{{Target: "go", Reason: fmt.Sprintf("could not read go.mod: %v", err)}}}
	}
	if !ok {
		return detectionResult{Skips: []SkipNote{{Target: "go", Reason: "no go.mod"}}}
	}
	version, ok := parseGoDirective(goMod)
	if !ok {
		return detectionResult{Skips: []SkipNote{{Target: "go", Reason: "go.mod has no exact go directive"}}}
	}
	return detectionResult{Steps: []InstallStep{{Tool: "go", Version: version, Source: "go.mod", Reason: "go directive"}}}
}
