class IDRAC::FirmwareCatalog

def find_system_models(model_name)

def find_system_models(model_name)
  doc = parse
  models = []
  
  # Extract model code from full model name (e.g., "PowerEdge R640" -> "R640")
  model_code = nil
  if model_name.include?("PowerEdge")
    model_code = model_name.split.last
  else
    model_code = model_name
  end
  
  puts "Searching for model: #{model_name} (code: #{model_code})"
  
  # Build a mapping of model names to system IDs
  model_to_system_id = {}
  
  doc.xpath('//SupportedSystems/Brand/Model').each do |model|
    system_id = model['systemID'] || model['id']
    name = model.at_xpath('Display')&.text
    code = model.at_xpath('Code')&.text
    
    if name && system_id
      model_to_system_id[name] = {
        name: name,
        code: code,
        id: system_id
      }
      
      # Also map just the model number (R640, etc.)
      if name =~ /[RT]\d+/
        model_short = name.match(/([RT]\d+\w*)/)[1]
        model_to_system_id[model_short] = {
          name: name,
          code: code,
          id: system_id
        }
      end
    end
  end
  
  # Try exact match first
  if model_to_system_id[model_name]
    models << model_to_system_id[model_name]
  end
  
  # Try model code match
  if model_to_system_id[model_code]
    models << model_to_system_id[model_code]
  end
  
  # If we still don't have a match, try a more flexible approach
  if models.empty?
    model_to_system_id.each do |name, model_info|
      if name.include?(model_code) || model_code.include?(name)
        models << model_info
      end
    end
  end
  
  # If still no match, try matching by systemID directly
  if models.empty?
    doc.xpath('//SupportedSystems/Brand/Model').each do |model|
      system_id = model['systemID'] || model['id']
      name = model.at_xpath('Display')&.text
      code = model.at_xpath('Code')&.text
      
      if code && code.downcase == model_code.downcase
        models << {
          name: name,
          code: code,
          id: system_id
        }
      end
    end
  end
  
  models.uniq { |m| m[:id] }
end