--guid,loadoutid,quanity required.
--don't forget to add the comma afte new entries unless it's the very last in the list(even then it's fine).
local airbasesToFill = {
{name='SUA_1', guid='4FH7PU-0HMNDPRJL928I',loadoutid=26461, quantity=50,mydesc="f22 aim 260 loadout"}, 
{name='SUA_1', guid='4FH7PU-0HMNDPRJL928I',loadoutid=29716, quantity=50,mydesc="f18 aim 120D loadout"}, 
{name='SUA_2', guid='4FH7PU-0HMNDPRJL9A20',loadoutid=26461, quantity=50,mydesc="f22 aim 260 loadout"}, 
{name='SUA_2', guid='4FH7PU-0HMNDPRJL9A20',loadoutid=29716, quantity=50,mydesc="f18 aim 120D loadout"}
}

---Function to fill a list of airbases from table data.
---@param tbl table @ A table of entries each containing at least a guid,loadoutid, and quantity.
local function FillAirBasesInitially(tbl)
    local fn = "FillAirBasesInitially(): "
    if (tbl ==nil) or type(tbl) ~= "table" then print(fn.. 'tbl parameter is missing or not a table. aborting'); return false; end
    local counter,scounter = 0,0;
    for k,v in pairs(tbl) do -- k= index, v = the list entry
        if v.guid == nil or v.loadoutid == nil or v.quantity == nil then print(fn.. "Table entry is invalid. a guid,loadoutid or quantity is missing."); return false; end
        local retval,unit = pcall(SE_GetUnit,{guid=v.guid}); --get unit, will fail when does not exist.
        if (retval == true) and unit ~=nil then -- unit exists so let's proceed. 
            local retval,response = pcall(ScenEdit_FillMagsForLoadout,{guid=v.guid, loadoutid=v.loadoutid, quantity=v.quantity});
            if ((retval == true) and type(response) =='table') and #response > 0 then
                print(fn .. "successes and failures for filling mags for loadout on " .. tostring(v.name) .. ":");
                print(response);
                scounter = scounter +1
            else
                print(fn.. "A call to ScenEdit_FillMagsForLoadout seriously failed for entry " .. tostring(v.name));
                if type(response) == "table" then print(response); else print(tostring(response)); end
            end
            retval,unit = false,nil; --explicitly release unit
        else
            print(fn.."The unit " ..tostring(v.name) .. ' no longer exists or its guid changed and will be skipped.');
        end
        counter = counter + 1
    end
    print("A total of " .. tostring(counter) .. " units were processed, and " .. tostring(scounter) .. " processed successfully.");
end


--- now call the function with the table from up top ---
FillAirBasesInitially(airbasesToFill);


