-- =========================================
-- DecorVendor - Vendor Dump Tool
-- Dev-only utility for collecting vendor data
-- =========================================

local addonName, dv = ...

-- 🔥 DEV TOGGLE (turn on when needed)
local ENABLE_DUMP = false
if not ENABLE_DUMP then return end

-- 🔥 SavedVariables safety
DV_DumpLog = DV_DumpLog or {}
local CLEAR_ON_RELOAD = true

if CLEAR_ON_RELOAD then
    DV_DumpLog = {}
else
    DV_DumpLog = DV_DumpLog or {}
end
-- =========================================
-- Helper: Get NPC ID
-- =========================================
local function GetNPCID()
    local guid = UnitGUID("npc")
    if not guid then return 0 end

    local npcID = tonumber(guid:match("-(%d+)-%x+$"))
    return npcID or 0
end

-- =========================================
-- Helper: Normalize vendor items table
-- =========================================
local function GetVendorItems(vendorID)
    if not dv or not dv.vendorGoodies then return nil end

    local data = dv.vendorGoodies[vendorID]
    if not data then return nil end

    return data.items or data
end

-- =========================================
-- Dump Logic
-- =========================================
local function DumpVendor()
    if not MerchantFrame or not MerchantFrame:IsShown() then return end
    if not dv or not dv.ActiveItems then return end

    local npcID = GetNPCID()
    local npcName = UnitName("npc") or "Unknown"

    local numItems = GetMerchantNumItems()
    if numItems == 0 then return end

    local vendorEntry = {
        npcID = npcID,
        name = npcName,
        entries = {},
    }

    local newCount = 0

    for i = 1, numItems do
        local link = GetMerchantItemLink(i)
        if link then
            local itemID = tonumber(link:match("item:(%d+)"))

            if itemID then
                -- 🔥 Skip items already in your database
                if not (dv.ActiveItems and dv.ActiveItems[itemID]) then

                    -- 🔥 Ready-to-paste database format
                    local entry = string.format(
                        "[%d] = { decorID = ?, model3D = ?, soldBy = {%d} },",
                        itemID,
                        npcID
                    )

                    table.insert(vendorEntry.entries, entry)
                    newCount = newCount + 1
                end
            end
        end
    end

    -- 🔥 Only save if new items found
    if newCount > 0 then
        table.insert(DV_DumpLog, vendorEntry)

        print("|cff00ff00[DecorVendor]|r Dumped", newCount, "new items from", npcName)
    else
        print("|cffaaaaaa[DecorVendor]|r No new items for", npcName)
    end
end

-- =========================================
-- Event Hook
-- =========================================
local f = CreateFrame("Frame")
f:RegisterEvent("MERCHANT_SHOW")

f:SetScript("OnEvent", function()
    C_Timer.After(0.2, function()
        if not dv or not dv.ActiveItems then
            print("Dump failed: dv not ready")
            return
        end

        DumpVendor()
    end)
end)

SLASH_DVCLEAR1 = "/dvclear"

SlashCmdList["DVCLEAR"] = function()
    DV_DumpLog = {}
    print("|cff00ff00[DecorVendor]|r Dump log cleared.")
end

