-- test_vendor_augment.lua
-- Verifies HDG.StaticData.VendorAugment accessors + derived indexes.
-- Lattice ADR-018: shipped data goes through StaticData facade.

dofile("wow_mock.lua")
dofile("../data/HDGR_VendorAugment.lua")
dofile("../Core/HDGR_Constants.lua")
dofile("../Core/HDGR_Format.lua")
dofile("../Modules/HDGR_StaticData.lua")

-- StaticData.OnInitialize builds derived indexes
HDG.StaticData:OnInitialize()

local function assertEq(actual, expected, msg)
    if actual ~= expected then
        error(string.format("FAIL [%s]: expected %s got %s", msg or "?",
            tostring(expected), tostring(actual)))
    end
end

-- Pick a known vendor from the generated file. Replace with an actual
-- npcID from HDGR_VendorAugment after the file is generated. Use
-- `next(HDGR_VendorAugment)` to discover a valid entry at test time.
local sampleNpcID, sampleEntry = next(HDGR_VendorAugment)
assert(sampleNpcID, "VendorAugment should not be empty")

-- :Get(npcID) returns the entry
local got = HDG.StaticData.VendorAugment:Get(sampleNpcID)
assertEq(got.name, sampleEntry.name, "Get(npcID).name")
assertEq(got.mapID, sampleEntry.mapID, "Get(npcID).mapID")

-- :Get(unknown) returns nil
assertEq(HDG.StaticData.VendorAugment:Get(99999999), nil, "Get(unknown) is nil")

-- :ResolveName(name) returns the npcID
local resolved = HDG.StaticData.VendorAugment:ResolveName(sampleEntry.name, sampleEntry.zone)
assertEq(resolved, sampleNpcID, "ResolveName(name, zone) -> npcID")

-- :ResolveName(name) without zone also resolves for unique names
local resolvedNoZone = HDG.StaticData.VendorAugment:ResolveName(sampleEntry.name)
assert(resolvedNoZone == sampleNpcID or resolvedNoZone == nil,
       "ResolveName(name) -> npcID OR nil if name is ambiguous")

-- :GetVendorsByMap returns a list including sampleNpcID
local mapVendors = HDG.StaticData.VendorAugment:GetVendorsByMap(sampleEntry.mapID)
assert(mapVendors and #mapVendors > 0, "GetVendorsByMap returns list")
local found = false
for _, npcID in ipairs(mapVendors) do
    if npcID == sampleNpcID then found = true; break end
end
assert(found, "GetVendorsByMap result includes sampleNpcID")

print("test_vendor_augment: PASS")
