package quality

import (
	"regexp"
	"strings"

	"github.com/tidbcloud/AgentTeam/kits/agentteam-insight/internal/graphbuilder/schema"
)

var deterministicFileSummary = regexp.MustCompile(`^[^\s]+ (?:code|docs|config|infra|script) file$`)

type Report struct {
	PlaceholderSummaries  int
	AnalyzerArtifactNodes int
}

// Measure checks the two known graph defects that prevent grounded domain extraction.
func Measure(graph schema.KnowledgeGraph) Report {
	var report Report
	for _, node := range graph.Nodes {
		summary := strings.TrimSpace(node.Summary)
		if summary == "Function "+node.Name || summary == "Type "+node.Name || deterministicFileSummary.MatchString(summary) {
			report.PlaceholderSummaries++
		}
		path := strings.TrimPrefix(strings.TrimSpace(strings.ReplaceAll(node.FilePath, `\`, "/")), "./")
		if path == ".ua" || strings.HasPrefix(path, ".ua/") || path == ".agentteam-insight" || strings.HasPrefix(path, ".agentteam-insight/") {
			report.AnalyzerArtifactNodes++
		}
	}
	return report
}

func (report Report) Grounded() bool {
	return report.PlaceholderSummaries == 0 && report.AnalyzerArtifactNodes == 0
}
