---@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")
VDS.Store:ValidateActionMetaExhaustiveness()
VDS_DB = {}
VDS.Store:LoadFromSavedVariables()

local A = VDS.Constants.ACTIONS
local function assertEq(actual, expected, msg)
    if actual ~= expected then
        error(string.format("%s: expected %s, got %s",
            msg or "assertEq", tostring(expected), tostring(actual)), 2)
    end
end

local s = VDS.Store:GetState()
assertEq(s.account.warehouse.targetGroups.Red, 1, "default Red target")
assertEq(s.account.warehouse.targetGroups.Purple, 1, "default Purple target")
assertEq(s.account.warehouse.fillMode, "buy", "default fillMode")
assertEq(s.account.schemaVersion, 2, "schemaVersion = 2 on fresh install")
print("  [OK] default warehouse shape")

VDS.Store:Dispatch({ type = A.STOCK_SET_GROUP_TARGET, payload = { group = "Red", value = 5 } })
assertEq(VDS.Store:GetState().account.warehouse.targetGroups.Red, 5, "Red bumped to 5")
print("  [OK] group target bump")

VDS.Store:Dispatch({ type = A.STOCK_SET_DYE_TARGET, payload = { dyeID = 100, group = "Red", value = 3 } })
assertEq(VDS.Store:GetState().account.warehouse.targetDyes[100], 3, "override stored")
print("  [OK] override stored")

VDS.Store:Dispatch({ type = A.STOCK_SET_DYE_TARGET, payload = { dyeID = 100, group = "Red", value = 5 } })
assertEq(VDS.Store:GetState().account.warehouse.targetDyes[100], nil, "override == default auto-cleared")
print("  [OK] auto-clear at default")

VDS.Store:Dispatch({ type = A.STOCK_SET_DYE_TARGET, payload = { dyeID = 100, group = "Red", value = 3 } })
VDS.Store:Dispatch({ type = A.STOCK_RESET_DYE_OVERRIDE, payload = { dyeID = 100 } })
assertEq(VDS.Store:GetState().account.warehouse.targetDyes[100], nil, "reset clears override")
print("  [OK] explicit reset")

VDS.Store:Dispatch({ type = A.STOCK_SET_FILL_MODE, payload = { mode = "craft" } })
assertEq(VDS.Store:GetState().account.warehouse.fillMode, "craft", "fillMode flipped to craft")
print("  [OK] fillMode flip")

local before = VDS.Store:GetState().session.stock.tick
VDS.Store:Dispatch({ type = A.STOCK_TICK, payload = { perItem = { [1] = 5, [2] = 0 } } })
assertEq(VDS.Store:GetState().session.stock.perItem[1], 5, "stock 5 for item 1")
assertEq(VDS.Store:GetState().session.stock.tick, before + 1, "tick incremented")
print("  [OK] STOCK_TICK applied")

dofile("../Modules/VDS_StaticData.lua")
VDS_DyeCraftingDB = {
    dyeRecipes = { [99001] = "pig_red_1" },
    pigments   = {
        pig_red_1 = { name = "Red Pigment", category = "Red",
                      herbQty = 2, herbs = { 2447, 765 } },
    },
}
local recipe = VDS.StaticData.GetDyeRecipe(99001)
assertEq(recipe.pigmentID, "pig_red_1", "recipe pigmentID")
assertEq(recipe.qtyPerCraft, 2, "recipe qtyPerCraft")
assertEq(#recipe.herbItemIDs, 2, "two herb choices")
print("  [OK] recipe lookup")

-- Default warehouse has all groups collapsed
s = VDS.Store:GetState()
if not s.account.warehouse.collapsedGroups.Red then
    error("expected Red collapsed by default")
end
print("  [OK] default: all groups collapsed")

-- TOGGLE_GROUP flips one
VDS.Store:Dispatch({ type = A.STOCK_TOGGLE_GROUP, payload = { group = "Red" } })
if VDS.Store:GetState().account.warehouse.collapsedGroups.Red ~= nil then
    error("Red should be nil (expanded) after toggle")
end
print("  [OK] STOCK_TOGGLE_GROUP expands a collapsed group")
VDS.Store:Dispatch({ type = A.STOCK_TOGGLE_GROUP, payload = { group = "Red" } })
if not VDS.Store:GetState().account.warehouse.collapsedGroups.Red then
    error("Red should be true after second toggle")
end
print("  [OK] STOCK_TOGGLE_GROUP collapses an expanded group")

-- STOCK_BUMP_ALL_GROUPS: applies delta to every group (clamped to >= 0).
local redBefore = VDS.Store:GetState().account.warehouse.targetGroups.Red
VDS.Store:Dispatch({ type = A.STOCK_BUMP_ALL_GROUPS, payload = { delta = 2 } })
assertEq(VDS.Store:GetState().account.warehouse.targetGroups.Red, redBefore + 2, "Red += 2")
VDS.Store:Dispatch({ type = A.STOCK_BUMP_ALL_GROUPS, payload = { delta = -1000 } })
assertEq(VDS.Store:GetState().account.warehouse.targetGroups.Red, 0, "Red clamped to 0")
print("  [OK] STOCK_BUMP_ALL_GROUPS")

-- STOCK_RESET_ALL_TARGETS: every group -> 1, all overrides cleared.
VDS.Store:Dispatch({ type = A.STOCK_SET_DYE_TARGET, payload = { dyeID = 200, group = "Red", value = 7 } })
VDS.Store:Dispatch({ type = A.STOCK_RESET_ALL_TARGETS })
assertEq(VDS.Store:GetState().account.warehouse.targetGroups.Red, 1, "Red reset to 1")
assertEq(VDS.Store:GetState().account.warehouse.targetGroups.Blue, 1, "Blue reset to 1")
assertEq(VDS.Store:GetState().account.warehouse.targetDyes[200], nil, "dye overrides cleared")
print("  [OK] STOCK_RESET_ALL_TARGETS")

-- EXPAND_ALL clears the map
VDS.Store:Dispatch({ type = A.STOCK_EXPAND_ALL_GROUPS })
for k, v in pairs(VDS.Store:GetState().account.warehouse.collapsedGroups) do
    if v then error("expected all groups expanded, found " .. k .. " still collapsed") end
end
print("  [OK] STOCK_EXPAND_ALL_GROUPS empties the map")

-- COLLAPSE_ALL sets every group
VDS.Store:Dispatch({ type = A.STOCK_COLLAPSE_ALL_GROUPS })
if not VDS.Store:GetState().account.warehouse.collapsedGroups.Red then
    error("Red should be collapsed after COLLAPSE_ALL")
end
if not VDS.Store:GetState().account.warehouse.collapsedGroups.Purple then
    error("Purple should be collapsed after COLLAPSE_ALL")
end
print("  [OK] STOCK_COLLAPSE_ALL_GROUPS sets every group")

print("test_warehouse_targets.lua passed")
