-- data.lua
-- Compact vendor format: vendorName␟uiMapID␟parentMapID␟x␟y␟priceCopper␟vendorType

local ADDON_NAME, WDIGI = ...
WDIGI.Data = WDIGI.Data or {}

local US = string.char(31)

------------------------------------------------------------
-- VENDOR TYPE ENUM (must match exporter)
------------------------------------------------------------
WDIGI.Data.VENDOR_TYPE = {
    SOLD_BY           = 1,
    DROPPED_BY        = 2,
    CONTAINED_IN      = 3,
    DISENCHANTED_FROM = 4,
    GATHERED_FROM     = 5,
    UNKNOWN           = 6, -- catch-all
}

------------------------------------------------------------
-- CACHE: Parsed vendor entries per itemID
------------------------------------------------------------
WDIGI.Data.Parsed = {}

------------------------------------------------------------
-- MONEY FORMATTER
------------------------------------------------------------
local function FormatMoney(copper)
    copper = tonumber(copper) or 0
    local g = math.floor(copper / 10000)
    local s = math.floor((copper % 10000) / 100)
    local c = copper % 100

    local parts = {}
    if g > 0 then table.insert(parts, g .. "|cffffd700g|r") end
    if s > 0 then table.insert(parts, s .. "|cffc7c7cfs|r") end
    if c > 0 or (#parts == 0) then table.insert(parts, c .. "|cffeda55fc|r") end

    return table.concat(parts, " ")
end

------------------------------------------------------------
-- INTERNAL: Parse raw ITEMLOCATIONSLIST entry into structured rows
------------------------------------------------------------
local function ParseItemData(itemID)
    if WDIGI.Data.Parsed[itemID] then
        return WDIGI.Data.Parsed[itemID]
    end

    local raw = ITEMLOCATIONSLIST and ITEMLOCATIONSLIST[itemID]
    if type(raw) ~= "string" or raw == "" then
        WDIGI.Data.Parsed[itemID] = {}
        return WDIGI.Data.Parsed[itemID]
    end

    local entries = {}
    local blob = raw .. "||"  -- ensure last entry is captured

    for entry in blob:gmatch("(.-)||") do
        if entry and entry ~= "" then
            local vendorName, uiMapID, parentMapID, x, y, priceCopper, vendorType = strsplit(WDIGI.Constants.US, entry)

            local mapID       = tonumber(uiMapID) or 0
            local parentID    = tonumber(parentMapID) or 0
            local tx          = tonumber(x) or 0
            local ty          = tonumber(y) or 0
            local price       = tonumber(priceCopper) or 0
            local vType       = tonumber(vendorType) or 0

            local vType = tonumber(vendorType) or 0

            -- ADD THIS TRACE
            WDIGI.Utils.Trace(string.format("Parsing Entry: %s | TypeID: %s",
                                            tostring(vendorName), tostring(vType)))

            table.insert(entries, {
                vendorName   = vendorName or "",
                mapID        = mapID,
                parentMapID  = parentID,
                tx           = tx,
                ty           = ty,
                priceCopper  = price,
                vendorType   = vType,
                costString   = FormatMoney(price),
            })
        end
    end

    WDIGI.Data.Parsed[itemID] = entries
    return entries
end

------------------------------------------------------------
-- PUBLIC: Best location for navigation (closest in same/related zone)
------------------------------------------------------------
function WDIGI.Data.GetBestLocation(itemID)
    local entries = ParseItemData(itemID)
    if not entries or #entries == 0 then
        return nil
    end

    local myMapID, px, py = WDIGI.MapUtils.GetCurrentPlayerData()
    if not myMapID or not px or not py then
        local e = entries[1]
        return {
            vendorName = chosen.vendorName,
            zID        = chosen.mapID,  -- Changed from zoneID
            tx         = chosen.tx,     -- Changed from x
            ty         = chosen.ty,     -- Changed from y
            cost       = chosen.costString,
            vendorType = chosen.vendorType,
        }
    end

    local bestEntry, bestDist

    for _, e in ipairs(entries) do
        if e.mapID > 0 and e.tx > 0 then
            local onMap = WDIGI.MapUtils.IsSameZone(myMapID, e.mapID)

            if onMap then
                local dx = (e.tx / 100) - px
                local dy = (e.ty / 100) - py
                local dist = math.sqrt(dx * dx + dy * dy) *
                             (WDIGI.MapUtils.GetZoneMultiplier() or 1000)

                if not bestDist or dist < bestDist then
                    bestDist  = dist
                    bestEntry = e
                end
            end
        end
    end

    local chosen = bestEntry or entries[1]
    return {
        vendorName = chosen.vendorName,
        zoneID     = chosen.mapID,
        x          = chosen.tx,
        y          = chosen.ty,
        cost       = chosen.costString,
        vendorType = chosen.vendorType,
    }
end

------------------------------------------------------------
-- PUBLIC EXPORT
------------------------------------------------------------
WDIGI.Data.ParseItemData = ParseItemData
WDIGI.Data.FormatMoney   = FormatMoney
