package iace

// Machine-type overrides for legacy patterns that lacked MachineTypes
// filtering at authoring time. Applied as a post-load pass in
// collectAllPatterns() so we do not need to touch the large pattern
// source files (which would push them past the 500-LOC cap).
//
// Adding an entry here causes the listed pattern IDs to fire ONLY for
// projects whose machine_type is in the value list. This eliminates
// drift like "Punktschweisselektroden" firing for a Kistenhubgeraet
// project just because tags incidentally aligned.

var legacyMachineTypeOverrides = map[string][]string{
	// Walzen / Roller hazards — printing, paper, metalworking only.
	"HP1000": {"printing", "paper", "textile", "metalworking", "rolling_mill", "food_processing"},
	// HP306 + HP1530 already carry MachineTypes; skip.

	// Welding-specific patterns.
	"HP539": {"welding", "spot_welding"},

	// Glass-handling tilters.
	"HP545": {"glass", "glass_processing"},
	"HP782": {"glass", "glass_processing"},

	// Escalator-specific.
	"HP756": {"escalator"},
	"HP757": {"escalator"},
	"HP760": {"escalator"},

	// CNC machine tools (these fired on Kistenhubgeraet because they
	// share crush_point + moving_part tags but are bench-mounted tools).
	"HP1400": {"cnc", "metalworking", "lathe", "milling"},
	"HP1401": {"cnc", "metalworking", "lathe", "milling"},
	"HP1402": {"cnc", "metalworking", "lathe", "milling"},

	// Press-specific (Pressenteile/Pressraum/Werkzeugraum).
	"HP045": {"press", "hydraulic_press", "mechanical_press", "stamping_press"},
	"HP049": {"press", "hydraulic_press", "mechanical_press", "stamping_press"},

	// Conveyor-belt-specific drift.
	"HP420": {"conveyor", "packaging", "food_processing"},
	"HP421": {"conveyor", "packaging", "food_processing"},
	"HP422": {"conveyor", "packaging", "food_processing"},
}

// applyMachineTypeOverrides mutates the passed slice in place, setting
// MachineTypes on any pattern whose ID is in the override map. Patterns
// that already have MachineTypes set are NOT overwritten — the override
// only fills the gap.
func applyMachineTypeOverrides(patterns []HazardPattern) []HazardPattern {
	for i := range patterns {
		if len(patterns[i].MachineTypes) > 0 {
			continue
		}
		if mt, ok := legacyMachineTypeOverrides[patterns[i].ID]; ok {
			patterns[i].MachineTypes = mt
		}
	}
	return patterns
}
