package redfishprofile

import "strings"

func msiProfile() Profile {
	return staticProfile{
		name:            "msi",
		priority:        20,
		safeForFallback: true,
		matchFn: func(s MatchSignals) int {
			score := 0
			if containsFold(s.SystemManufacturer, "micro-star") || containsFold(s.ChassisManufacturer, "micro-star") {
				score += 80
			}
			if containsFold(s.SystemManufacturer, "msi") || containsFold(s.ChassisManufacturer, "msi") {
				score += 40
			}
			for _, hint := range s.ResourceHints {
				if strings.HasPrefix(hint, "/redfish/v1/Chassis/GPU") {
					score += 10
					break
				}
			}
			return min(score, 100)
		},
		extendAcquisition: func(plan *AcquisitionPlan, _ MatchSignals) {
			ensureSnapshotWorkers(plan, 6)
			ensurePrefetchWorkers(plan, 8)
			ensureETABaseline(plan, AcquisitionETABaseline{
				DiscoverySeconds:     12,
				SnapshotSeconds:      120,
				PrefetchSeconds:      25,
				CriticalPlanBSeconds: 35,
				ProfilePlanBSeconds:  25,
			})
			ensureRatePolicy(plan, AcquisitionRatePolicy{
				TargetP95LatencyMS:      1000,
				ThrottleP95LatencyMS:    2200,
				MinSnapshotWorkers:      2,
				MinPrefetchWorkers:      2,
				DisablePrefetchOnErrors: true,
			})
			ensureRecoveryPolicy(plan, AcquisitionRecoveryPolicy{
				EnableProfilePlanB: true,
			})
			addPlanNote(plan, "msi gpu chassis probes enabled")
		},
		refineAcquisition: func(resolved *ResolvedAcquisitionPlan, discovered DiscoveredResources, _ MatchSignals) {
			for _, chassisPath := range discovered.ChassisPaths {
				if !strings.HasPrefix(chassisPath, "/redfish/v1/Chassis/GPU") {
					continue
				}
				addPlanPaths(&resolved.SeedPaths, chassisPath)
				addPlanPaths(&resolved.Plan.SeedPaths, chassisPath)
				addPlanPaths(&resolved.CriticalPaths, joinPath(chassisPath, "/Sensors"))
				addPlanPaths(&resolved.Plan.CriticalPaths, joinPath(chassisPath, "/Sensors"))
				addPlanPaths(&resolved.Plan.PlanBPaths, joinPath(chassisPath, "/Sensors"))
			}
		},
		applyAnalysisDirectives: func(d *AnalysisDirectives, _ MatchSignals) {
			d.EnableGenericGraphicsControllerDedup = true
		},
		refineAnalysis: func(plan *ResolvedAnalysisPlan, snapshot map[string]interface{}, discovered DiscoveredResources, _ MatchSignals) {
			if snapshotHasGPUProcessor(snapshot, discovered.SystemPaths) && snapshotHasPathPrefix(snapshot, "/redfish/v1/Chassis/GPU") {
				plan.Directives.EnableProcessorGPUFallback = true
				plan.Directives.EnableMSIProcessorGPUChassisLookup = true
				plan.Directives.EnableMSIGhostGPUFilter = true
				addAnalysisLookupMode(plan, "msi-index")
				addAnalysisNote(plan, "msi analysis enables processor-gpu fallback from discovered GPU chassis")
				addAnalysisNote(plan, "msi ghost-gpu filter enabled: GPUs with temperature=0 on powered-on host are excluded")
			}
		},
	}
}
