-- Functional smoke for acq.selected.recipes: load the REAL DecorDB + StaticData,
-- select a vendor by npcID, assert the recipe rows resolve (names, cost, known-state)
-- and that a 2-vendor recipe appears under both vendors (the 41-vs-31 split).
dofile("wow_mock.lua")
dofile("../Core/HDGR_SchemeConstants.lua")
dofile("../Core/HDGR_Constants.lua")
dofile("../Core/HDGR_Format.lua")
dofile("../Core/HDGR_Paths.lua")
dofile("../Core/HDGR_Log.lua")
dofile("../UI/HDGR_Selectors.lua")
dofile("../Modules/HDGR_StaticData.lua")
dofile("../UI/HDGR_Selectors_Acquisition.lua")
dofile("../data/HDGR_DecorDB.lua")   -- REAL data (not the empty stub)

local function call(npcID, recipes)
    -- In-game a vendor switch dispatches SET_TRANSIENT, which clears the memo.
    -- Hand-built state doesn't dispatch, so wipe caches to force fresh evaluation.
    HDG.Selectors:InvalidateMemos("*")
    local state = {
        session = { ui = { acquisition = { selectedNpcID = npcID } } },
        account = { recipes = recipes or {} },
    }
    return HDG.Selectors:Call("acq.selected.recipes", state, {})
end

local fails = 0
local function check(cond, msg)
    if not cond then fails = fails + 1; io.stderr:write("  FAIL: " .. msg .. "\n") end
end
-- harness mocks print(); use io.stderr for diagnostics
io.stderr:write(("counts -> Lelorian:%d Lyrendal:%d Construct:%d Naynar:%d\n"):format(
    #call(243555), #call(243286), #call(252956), #call(240407)))

-- Lelorian (243555) sells 4 recipes; mark Homely Sin'dorei Shelf (262594) known.
local lel = call(243555, { [262594] = { selfKnown = true } })
check(#lel == 4, "Lelorian: expected 4 recipes, got " .. #lel)
for _, r in ipairs(lel) do
    check(r.name:match("^Recipe: "), "row name should start 'Recipe: ' -> " .. tostring(r.name))
    check(type(r.costText) == "string" and #r.costText > 0, "costText non-empty -> " .. tostring(r.costText))
    if r.itemID == 262594 then check(r.isKnown == true, "Homely Sin'dorei Shelf should read known") end
end

-- 2-vendor recipe also appears under Lyrendal (243286, alt vendor) -> 11 recipes.
local lyr = call(243286)
check(#lyr == 11, "Lyrendal: expected 11 recipes, got " .. #lyr)
local found = false
for _, r in ipairs(lyr) do if r.itemID == 262594 then found = true end end
check(found, "Homely Sin'dorei Shelf should also appear under Lyrendal")

-- Recipe-only quartermaster Construct V'anore (252956) -> 2 recipes.
local cva = call(252956)
check(#cva == 2, "Construct V'anore: expected 2 recipes, got " .. #cva)

-- A rep-gated recipe carries factionID/minRep (Naynar 240407, Hara'ti).
local nay = call(240407)
check(#nay > 0, "Naynar: expected recipes")
local gated = false
for _, r in ipairs(nay) do if r.factionID == 2704 and r.minRep == 13 then gated = true end end
check(gated, "Naynar recipe should carry factionID=2704 minRep=13")

-- Empty selection -> {}.
check(#call(nil) == 0, "nil selectedNpcID -> empty")

if fails == 0 then
    io.stderr:write("test_acquire_recipes_smoke.lua passed (sample: " .. lel[1].name .. "  cost=" .. lel[1].costText .. ")\n")
else
    error(fails .. " check(s) failed")
end
