package redfishprofile

func hgxProfile() Profile {
	return staticProfile{
		name:            "hgx-topology",
		priority:        30,
		safeForFallback: true,
		matchFn: func(s MatchSignals) int {
			score := 0
			if containsFold(s.SystemModel, "hgx") || containsFold(s.ChassisModel, "hgx") {
				score += 70
			}
			for _, hint := range s.ResourceHints {
				if containsFold(hint, "hgx_") || containsFold(hint, "gpu_sxm") {
					score += 20
					break
				}
			}
			return min(score, 100)
		},
		extendAcquisition: func(plan *AcquisitionPlan, _ MatchSignals) {
			ensureSnapshotMaxDocuments(plan, 180000)
			ensureSnapshotWorkers(plan, 4)
			ensurePrefetchWorkers(plan, 4)
			ensureNVMePostProbeEnabled(plan, false)
			ensureRecoveryPolicy(plan, AcquisitionRecoveryPolicy{
				EnableProfilePlanB: true,
			})
			ensureETABaseline(plan, AcquisitionETABaseline{
				DiscoverySeconds:     20,
				SnapshotSeconds:      300,
				PrefetchSeconds:      50,
				CriticalPlanBSeconds: 90,
				ProfilePlanBSeconds:  40,
			})
			ensureRatePolicy(plan, AcquisitionRatePolicy{
				TargetP95LatencyMS:      1500,
				ThrottleP95LatencyMS:    3000,
				MinSnapshotWorkers:      1,
				MinPrefetchWorkers:      1,
				DisablePrefetchOnErrors: true,
			})
			addPlanNote(plan, "hgx topology acquisition extensions enabled")
		},
		refineAcquisition: func(resolved *ResolvedAcquisitionPlan, discovered DiscoveredResources, _ MatchSignals) {
			for _, systemPath := range discovered.SystemPaths {
				if !containsFold(systemPath, "hgx_baseboard_") {
					continue
				}
				addPlanPaths(&resolved.SeedPaths, systemPath, joinPath(systemPath, "/Processors"))
				addPlanPaths(&resolved.Plan.SeedPaths, systemPath, joinPath(systemPath, "/Processors"))
				addPlanPaths(&resolved.CriticalPaths, systemPath, joinPath(systemPath, "/Processors"))
				addPlanPaths(&resolved.Plan.CriticalPaths, systemPath, joinPath(systemPath, "/Processors"))
				addPlanPaths(&resolved.Plan.PlanBPaths, systemPath, joinPath(systemPath, "/Processors"))
			}
		},
		applyAnalysisDirectives: func(d *AnalysisDirectives, _ MatchSignals) {
			d.EnableGenericGraphicsControllerDedup = true
			d.EnableStorageEnclosureRecovery = true
		},
		refineAnalysis: func(plan *ResolvedAnalysisPlan, snapshot map[string]interface{}, discovered DiscoveredResources, _ MatchSignals) {
			if snapshotHasGPUProcessor(snapshot, discovered.SystemPaths) && (snapshotHasPathContaining(snapshot, "gpu_sxm") || snapshotHasPathContaining(snapshot, "hgx_")) {
				plan.Directives.EnableProcessorGPUFallback = true
				plan.Directives.EnableProcessorGPUChassisAlias = true
				addAnalysisLookupMode(plan, "hgx-alias")
				addAnalysisNote(plan, "hgx analysis enables processor-gpu alias fallback from snapshot topology")
			}
			if snapshotHasStorageControllerHint(snapshot, "/storage/intelvroc", "/storage/ha-raid", "/storage/mrvl.ha-raid") {
				plan.Directives.EnableKnownStorageControllerRecovery = true
				addAnalysisStorageDriveCollections(plan,
					"/Storage/IntelVROC/Drives",
					"/Storage/IntelVROC/Controllers/1/Drives",
				)
				addAnalysisStorageVolumeCollections(plan,
					"/Storage/IntelVROC/Volumes",
					"/Storage/HA-RAID/Volumes",
					"/Storage/MRVL.HA-RAID/Volumes",
				)
			}
			if snapshotHasPathContaining(snapshot, "/chassis/nvmessd.") && snapshotHasPathContaining(snapshot, ".storagebackplane") {
				plan.Directives.EnableSupermicroNVMeBackplane = true
			}
		},
	}
}
