-- maputils.lua
-- Map utilities for Retail UiMapIDs with match-level logic

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

------------------------------------------------------------
-- GetCurrentPlayerData
-- Returns: mapID, playerX, playerY
------------------------------------------------------------
function WDIGI.MapUtils.GetCurrentPlayerData()
    local mapID = C_Map.GetBestMapForUnit("player")
    if not mapID then
        return nil, nil, nil
    end

    local pos = C_Map.GetPlayerMapPosition(mapID, "player")
    if not pos then
        return mapID, nil, nil
    end

    return mapID, pos.x, pos.y
end

------------------------------------------------------------
-- IsSameZone (strict)
------------------------------------------------------------
function WDIGI.MapUtils.IsSameZone(a, b)
    if not a or not b or a <= 0 or b <= 0 then
        return false
    end

    if a == b then
        return true
    end

    local aInfo = C_Map.GetMapInfo(a)
    local bInfo = C_Map.GetMapInfo(b)
    if not aInfo or not bInfo then
        return false
    end

    -- Same direct parent
    if aInfo.parentMapID ~= 0 and aInfo.parentMapID == bInfo.parentMapID then
        return true
    end

    -- Same grandparent
    local aParent = aInfo.parentMapID ~= 0 and C_Map.GetMapInfo(aInfo.parentMapID)
    local bParent = bInfo.parentMapID ~= 0 and C_Map.GetMapInfo(bInfo.parentMapID)

    if aParent and bParent and aParent.parentMapID ~= 0 and aParent.parentMapID == bParent.parentMapID then
        return true
    end

    return false
end

------------------------------------------------------------
-- GetZoneMatchLevel
-- 0 = same zone
-- 1 = same parent zone (Uses pre-baked vendorParentID)
-- 2 = same continent
-- 3 = everything else
------------------------------------------------------------
function WDIGI.MapUtils.GetZoneMatchLevel(playerMapID, vendorMapID, vendorParentID)
    if not playerMapID or not vendorMapID then
        return 3
    end

    if playerMapID == vendorMapID then
        return 0
    end

    local p = C_Map.GetMapInfo(playerMapID)
    if not p then return 3 end

    -- 1. Check against our pre-baked Parent ID (The Normalization Advantage)
    if vendorParentID and vendorParentID > 0 and p.parentMapID == vendorParentID then
        return 1
    end

    -- 2. Fallback to API check for parent zone
    local v = C_Map.GetMapInfo(vendorMapID)
    if v and p.parentMapID ~= 0 and p.parentMapID == v.parentMapID then
        return 1
    end

    -- 3. Same continent (grandparent)
    local pParent = p.parentMapID ~= 0 and C_Map.GetMapInfo(p.parentMapID)
    local vParent = (v and v.parentMapID ~= 0) and C_Map.GetMapInfo(v.parentMapID)

    if pParent and vParent and pParent.parentMapID ~= 0 and pParent.parentMapID == vParent.parentMapID then
        return 2
    end

    return 3
end

------------------------------------------------------------
-- GetZoneMultiplier
-- Scaling factor for distance calculations
------------------------------------------------------------
function WDIGI.MapUtils.GetZoneMultiplier(specificMapID)
    local mapID = specificMapID or C_Map.GetBestMapForUnit("player")
    if not mapID then return 1000 end

    local multiplier = 1000.0 -- Base Default
    local mapInfo = C_Map.GetMapInfo(mapID) -- FIXED: Variable defined

    -- 1. Scale by Map Type
    if mapInfo then
        if mapInfo.mapType == 3 then -- City
            multiplier = 1625.0 
        elseif mapInfo.mapType == 4 then -- Dungeon
            multiplier = 500.0 
        end
    end

    -- 2. Specific Map Overrides
    if mapID == 84 then -- Stormwind
        multiplier = 1650.0
    elseif mapID == 1453 then -- Orgrimmar
        multiplier = 1500.0
    end

    return multiplier
end