package toolingplan

import (
	"context"
	"fmt"
	"strings"
)

type pythonDetector struct{}

func (pythonDetector) detect(_ context.Context, repoRoot string, managedTools map[string]struct{}) detectionResult {
	if alreadyManaged("python", managedTools) {
		return detectionResult{Skips: []SkipNote{{Target: "python", Reason: "already managed by repo mise declarations"}}}
	}
	value, ok, err := readRepoFile(repoRoot, ".python-version")
	if err != nil {
		return detectionResult{Skips: []SkipNote{{Target: "python", Reason: fmt.Sprintf("could not read .python-version: %v", err)}}}
	}
	if !ok {
		return detectionResult{Skips: []SkipNote{{Target: "python", Reason: "no .python-version"}}}
	}
	version, ok := normalizeExactVersion(strings.TrimSpace(value))
	if !ok {
		return detectionResult{Skips: []SkipNote{{Target: "python", Reason: ".python-version does not pin an exact version"}}}
	}
	return detectionResult{Steps: []InstallStep{{Tool: "python", Version: version, Source: ".python-version", Reason: "exact runtime version"}}}
}
