---@diagnostic disable: undefined-global
dofile("wow_mock.lua")
dofile("../Core/VDS_Constants.lua")
dofile("../Core/VDS_SchemeConstants.lua")
dofile("../Core/VDS_Paths.lua")
dofile("../Core/VDS_Log.lua")
dofile("../Core/VDS_Store.lua")
dofile("../Core/VDS_ActionMeta.lua")

-- Stub modules used by the selector. SD.GetAuctionPrice is normally a
-- live Auctionator wrap; stub it to a constant for deterministic tests.
VDS.StaticData = VDS.StaticData or {}
dofile("../Modules/VDS_StaticData.lua")
VDS.StaticData.GetAuctionPrice = function(_) return 10 end
VDS_DyeCraftingDB = {
    dyeRecipes = { [100] = "p_red", [101] = "p_red" },
    pigments   = { p_red = { herbQty = 2, herbs = { 999 } } },
}

dofile("../UI/VDS_Selectors.lua")
dofile("../UI/VDS_Selectors_Warehouse.lua")
VDS.Store:ValidateActionMetaExhaustiveness()
VDS_DB = {}
VDS.Store:LoadFromSavedVariables()

local A = VDS.Constants.ACTIONS

VDS.Store:Dispatch({
    type = A.DYE_DATA_INIT,
    payload = {
        dyes = { 10, 11 },
        categories = {}, itemIDToDye = {}, categoryNames = {},
        infos = {
            [10] = { name="Test Red 1", nameLower="test red 1", colorGroup="Red", itemID=100, swatchRGB={r=1,g=0,b=0} },
            [11] = { name="Test Red 2", nameLower="test red 2", colorGroup="Red", itemID=101, swatchRGB={r=1,g=0,b=0} },
        },
    },
})
VDS.Store:Dispatch({ type = A.STOCK_TICK, payload = { perItem = { [100]=0, [101]=0, [999]=0 } } })
VDS.Store:Dispatch({ type = A.STOCK_SET_GROUP_TARGET, payload = { group="Red", value=3 } })
-- Expand all groups so dye rows are visible in the selector (default state is all-collapsed).
VDS.Store:Dispatch({ type = A.STOCK_EXPAND_ALL_GROUPS })

local rows = VDS.Selectors:Call("stock.row.array", VDS.Store:GetState(), {})
local dyeRows, totalNeed = 0, 0
for _, r in ipairs(rows) do
    if r.kind == "warehouseRow" then dyeRows = dyeRows + 1; totalNeed = totalNeed + r.need end
end
if dyeRows ~= 2 then error("expected 2 dye rows, got " .. dyeRows) end
if totalNeed ~= 6 then error("expected total need 6, got " .. totalNeed) end
print("  [OK] stock.row.array composes deficits")

local summary = VDS.Selectors:Call("stock.summary", VDS.Store:GetState(), {})
if summary.deficit ~= 6 then error("summary deficit wrong: " .. summary.deficit) end
print("  [OK] stock.summary aggregates")

-- Craft mode -> herb list compiles
VDS.Store:Dispatch({ type = A.STOCK_SET_FILL_MODE, payload = { mode="craft" } })
local herbs = VDS.Selectors:Call("stock.herbs.list", VDS.Store:GetState(), {})
if #herbs ~= 1 then error("expected 1 herb in list, got " .. #herbs) end
if herbs[1].qty ~= 12 then error("expected herb qty 12 (6 dyes * 2 herb/craft), got " .. herbs[1].qty) end
print("  [OK] stock.herbs.list aggregates")

print("test_warehouse_restock.lua passed")
