---@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()

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

-- Case 1: fresh install (no VDS_DB)
VDS_DB = nil
VDS.Store:LoadFromSavedVariables()
local s = VDS.Store:GetState()
if not s.account.warehouse then error("fresh install missing warehouse") end
assertEq(s.account.schemaVersion, 2, "fresh install schemaVersion = 2")
assertEq(s.account.warehouse.fillMode, "buy", "fresh install fillMode = buy")
assertEq(s.account.warehouse.targetGroups.Red, 1, "fresh install Red default 1")
print("  [OK] case 1: fresh install")

-- Case 2: v1 install (account exists, no warehouse, no schemaVersion)
VDS_DB = {
    account = {
        config = { scheme = "DefaultDark", debug = false, ownedOnly = false, replaceBlizzard = false },
        customThemes = {},
        canvas = { loadouts = {}, autoSave = false, previewDetached = false },
        live = { recentDyes = {} },
        dyeableItems = {},
        minimap = { hide = false },
        panelPosition = nil,
        migratedFrom = { vds = true, vdsTS = 0 },
        -- no warehouse, no schemaVersion
    },
}
VDS.Store:LoadFromSavedVariables()
local s2 = VDS.Store:GetState()
if not s2.account.warehouse then error("v1 upgrade missing warehouse") end
assertEq(s2.account.schemaVersion, 2, "v1 upgrade schemaVersion = 2")
assertEq(s2.account.warehouse.targetGroups.Purple, 1, "v1 upgrade Purple default 1")
print("  [OK] case 2: v1 upgrade")

-- Case 3: partial schema (warehouse exists but missing sub-field)
VDS_DB = {
    account = {
        config = { scheme = "DefaultDark", debug = false, ownedOnly = false, replaceBlizzard = false },
        customThemes = {},
        canvas = { loadouts = {}, autoSave = false, previewDetached = false },
        live = { recentDyes = {} },
        dyeableItems = {},
        minimap = { hide = false },
        panelPosition = nil,
        migratedFrom = { vds = true, vdsTS = 0 },
        warehouse = { targetGroups = { Red = 9 } },  -- partial
        schemaVersion = 2,
    },
}
VDS.Store:LoadFromSavedVariables()
local s3 = VDS.Store:GetState()
assertEq(s3.account.warehouse.targetGroups.Red, 9, "partial Red survives backfill")
assertEq(s3.account.warehouse.fillMode, "buy", "partial fillMode backfilled to buy")
if s3.account.warehouse.targetDyes == nil then error("partial targetDyes backfill") end
print("  [OK] case 3: partial schema backfill")

-- Case 4: by-reference link survives
if VDS_DB.account ~= s3.account then
    error("by-reference broken: VDS_DB.account must alias state.account")
end
if VDS_DB.account.warehouse ~= s3.account.warehouse then
    error("by-reference broken at warehouse level")
end
print("  [OK] case 4: by-reference preserved")

print("test_warehouse_migration.lua passed")
