module IDRAC::Boot

def import_system_configuration(scp, target: "ALL", reboot: false)

Import System Configuration Profile for advanced configurations
def import_system_configuration(scp, target: "ALL", reboot: false)
  params = {
    "ImportBuffer": JSON.pretty_generate(scp),
    "ShareParameters": {
      "Target": target
    }
  }
  
  # Configure shutdown behavior
  params["ShutdownType"] = "Forced"
  params["HostPowerState"] = reboot ? "On" : "Off"
  
  response = authenticated_request(
    :post, 
    "/redfish/v1/Managers/iDRAC.Embedded.1/Actions/Oem/EID_674_Manager.ImportSystemConfiguration",
    body: params.to_json,
    headers: { 'Content-Type': 'application/json' }
  )
  
  if response.status.between?(200, 299)
    # Check if we need to wait for a job
    if response.headers["location"]
      job_id = response.headers["location"].split("/").last
      
      job = wait_for_job(job_id)
      
      # Check for task completion status
      if job["TaskState"] == "Completed" && job["TaskStatus"] == "OK"
        puts "System configuration imported successfully".green
        return true
      else
        # If there's an error message with a line number, surface it
        error_message = "Failed to import system configuration"
        
        if job["Messages"]
          job["Messages"].each do |m|
            puts "#{m["Message"]} (#{m["Severity"]})".red
            
            # Check for line number in error message
            if m["Message"] =~ /line (\d+)/
              line_num = $1.to_i
              lines = JSON.pretty_generate(scp).split("\n")
              puts "Error near line #{line_num}:".red
              ((line_num-3)..(line_num+1)).each do |ln|
                puts "#{ln}: #{lines[ln-1]}" if ln > 0 && ln <= lines.length
              end
            end
          end
        end
        
        raise Error, error_message
      end
    else
      puts "System configuration import started, but no job ID was returned".yellow
      return true
    end
  else
    error_message = "Failed to import system configuration. Status code: #{response.status}"
    
    begin
      error_data = JSON.parse(response.body)
      if error_data["error"] && error_data["error"]["@Message.ExtendedInfo"]
        error_info = error_data["error"]["@Message.ExtendedInfo"].first
        error_message += ", Message: #{error_info['Message']}"
      end
    rescue
      # Ignore JSON parsing errors
    end
    
    raise Error, error_message
  end
end