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

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

-- Unit Separator (ASCII 31)
local US = string.char(31)

------------------------------------------------------------
-- VENDOR TYPE ENUM
------------------------------------------------------------
WDIGI.Data.VENDOR_TYPE = {
    SOLD_BY           = 1,
    DROPPED_BY        = 2,
    CONTAINED_IN      = 3,
    DISENCHANTED_FROM = 4,
    GATHERED_FROM     = 5,
    CRAFTED_BY        = 6,
    LEARNED_FROM      = 7,
    QUEST_REWARD      = 8,
    UNKNOWN           = 99,
}

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

------------------------------------------------------------
-- MONEY FORMATTER
------------------------------------------------------------
local function FormatMoney(copper)
    local amt = tonumber(copper) or 0
    if amt <= 0 then return "0|cffeda55fc|r" end 

    local g = math.floor(amt / 10000)
    local s = math.floor((amt % 10000) / 100)
    local c = amt % 100

    local res = ""
    if g > 0 then res = res .. g .. "|cffffd700g|r " end
    if s > 0 then res = res .. s .. "|cffc7c7cfs|r " end
    if c > 0 or res == "" then res = res .. c .. "|cffeda55fc|r" end

    return res:trim()
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 .. "||" 

    for entry in blob:gmatch("(.-)||") do
        if entry and entry ~= "" then
            -- USE THE LOCAL 'US' DEFINED AT TOP OF FILE
            local vendorName, uiMapID, parentMapID, x, y, rawCost, vendorType = strsplit(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 vType    = tonumber(vendorType) or 0
            
            local costDisplay = ""
            if rawCost and rawCost ~= "" then
                local numCost = tonumber(rawCost)

                -- 1. Check if it already has the % symbol
                if string.find(rawCost, "%%") then
                    costDisplay = rawCost

                -- 2. Check if it's a raw float (e.g., 0.1934)
                -- Standard vendor items are rarely < 1 copper, so < 1 is a safe bet for a raw drop rate
                elseif numCost and numCost < 1 and vType == WDIGI.Data.VENDOR_TYPE.DROPPED_BY then
                    costDisplay = string.format("%.2f%%", numCost * 100)

                -- 3. Otherwise, treat it as a Vendor Price (Gold/Silver/Copper)
                else
                    costDisplay = FormatMoney(rawCost)
                end
            end

            table.insert(entries, {
                vendorName   = vendorName or "",
                mapID        = mapID,
                parentMapID  = parentID,
                tx           = tx,
                ty           = ty,
                priceCopper  = tonumber(rawCost) or 0, 
                vendorType   = vType,
                costString   = costDisplay,
            })
        end
    end

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

------------------------------------------------------------
-- PUBLIC: Best location for navigation
------------------------------------------------------------
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()
    
    -- FIX: Proper scoping for fallback entry 'e'
    if not myMapID or not px or not py then
        local e = entries[1] 
        return {
            vendorName = e.vendorName,
            zID        = e.mapID,
            tx         = e.tx,
            ty         = e.ty,
            cost       = e.costString,
            vendorType = e.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
