module IDRAC::Storage

def create_virtual_disk(controller_id, drives, name: "vssd0", raid_type: "RAID5")

Create a new virtual disk with RAID5 and FastPath optimizations
def create_virtual_disk(controller_id, drives, name: "vssd0", raid_type: "RAID5")
  # Check firmware version to determine which API to use
  firmware_version = get_firmware_version.split(".")[0,2].join.to_i
  
  # [FastPath optimization for SSDs](https://www.dell.com/support/manuals/en-us/perc-h755/perc11_ug/fastpath?guid=guid-a9e90946-a41f-48ab-88f1-9ce514b4c414&lang=en-us)
  payload = {
    "Drives": drives.map { |d| { "@odata.id": d["@odata.id"] } },
    "Name": name,
    "OptimumIOSizeBytes": 64 * 1024,
    "Oem": { "Dell": { "DellVolume": { "DiskCachePolicy": "Enabled" } } },
    "ReadCachePolicy": "Off", # "NoReadAhead"
    "WriteCachePolicy": "WriteThrough"
  }
  
  # If the firmware < 440, we need a different approach
  if firmware_version >= 440
    # For modern firmware
    if drives.size < 3 && raid_type == "RAID5"
      puts "*************************************************".red
      puts "* WARNING: Less than 3 drives. Selecting RAID0. *".red
      puts "*************************************************".red
      payload["RAIDType"] = "RAID0"
    else
      payload["RAIDType"] = raid_type
    end
  else
    # For older firmware
    payload["VolumeType"] = "StripedWithParity" if raid_type == "RAID5"
    payload["VolumeType"] = "SpannedDisks" if raid_type == "RAID0"
  end
  
  url = "Systems/System.Embedded.1/Storage/#{controller_id}/Volumes"
  response = authenticated_request(
    :post, 
    "/redfish/v1/#{url}", 
    body: payload.to_json, 
    headers: { 'Content-Type': 'application/json' }
  )
  
  if response.status.between?(200, 299)
    puts "Virtual disk creation started".green
    
    # Check if we need to wait for a job
    if response.headers["location"]
      job_id = response.headers["location"].split("/").last
      wait_for_job(job_id)
    end
    
    return true
  else
    error_message = "Failed to create virtual disk. Status code: #{response.status}"
    
    begin
      error_data = JSON.parse(response.body)
      error_message += ", Message: #{error_data['error']['message']}" if error_data['error'] && error_data['error']['message']
    rescue
      # Ignore JSON parsing errors
    end
    
    raise Error, error_message
  end
end