-- Transmoggin - Copyright (c) 2026 Rooster_Roost. All Rights Reserved.
-- Proprietary software. Personal non-commercial use only; redistribution/repackaging/forks/reuse require permission.
-- Full license: Core.lua and LICENSE.txt

local T=Transmoggin

-- Offline source intelligence for details Blizzard does not expose directly.
-- Records in this table are curated from MoP Classic Wowhead source/vendor pages
-- at addon build time. WoW addons cannot make live web requests, so keeping the
-- data bundled avoids opening a browser and keeps the salon self-contained.
T.SourceIntelDB=T.SourceIntelDB or {}

local function playerFaction()
    local f=UnitFactionGroup and UnitFactionGroup("player")
    if f=="Alliance" or f=="Horde" then return f end
    return "Neutral"
end

local function slotFromName(name)
    name=(name or ""):lower()
    if name:find("cowl",1,true) or name:find("helm",1,true) or name:find("hood",1,true) or name:find("head",1,true) then return "HEAD" end
    if name:find("amice",1,true) or name:find("shoulder",1,true) or name:find("spaulder",1,true) then return "SHOULDER" end
    if name:find("raiment",1,true) or name:find("robe",1,true) or name:find("chest",1,true) then return "CHEST" end
    if name:find("handguard",1,true) or name:find("glove",1,true) or name:find("gauntlet",1,true) then return "HANDS" end
    if name:find("trouser",1,true) or name:find("legging",1,true) or name:find("legguard",1,true) then return "LEGS" end
    if name:find("boot",1,true) or name:find("tread",1,true) or name:find("foot",1,true) then return "FEET" end
    if name:find("belt",1,true) or name:find("cord",1,true) or name:find("girdle",1,true) or name:find("waist",1,true) then return "WAIST" end
    if name:find("cuff",1,true) or name:find("bracer",1,true) or name:find("wrist",1,true) then return "WRIST" end
end

-- Verified legacy Wrathful vendor/currency coverage. The exact item IDs differ
-- by class, but the vendor group is shared across the legacy Season 8 armor.
local WRATHFUL_COST={HEAD=2370,SHOULDER=1930,CHEST=2370,HANDS=1430,LEGS=2370}
local WRATHFUL_VENDORS={
    Alliance={
        {name="Sergeant Major Clate",title="Legacy Armor Quartermaster",zone="Stormwind City",faction="Alliance"},
        {name="Vixton Pinchwhistle",title="Legacy Arena Armor",zone="Gadgetzan, Tanaris",faction="Neutral"},
        {name="Krixel Pinchwhistle",title="Legacy Arena Armor",zone="Area 52, Netherstorm",faction="Neutral"},
    },
    Horde={
        {name="First Sergeant Hola'mahi",title="Legacy Armor Quartermaster",zone="Orgrimmar",faction="Horde"},
        {name="Vixton Pinchwhistle",title="Legacy Arena Armor",zone="Gadgetzan, Tanaris",faction="Neutral"},
        {name="Krixel Pinchwhistle",title="Legacy Arena Armor",zone="Area 52, Netherstorm",faction="Neutral"},
    },
    Neutral={
        {name="Vixton Pinchwhistle",title="Legacy Arena Armor",zone="Gadgetzan, Tanaris",faction="Neutral"},
        {name="Krixel Pinchwhistle",title="Legacy Arena Armor",zone="Area 52, Netherstorm",faction="Neutral"},
    },
}

-- Exact item records override name-family rules when available.
T.SourceIntelDB[51538]={currency="Honor Points",amount=2370,family="Wrathful"} -- Felweave Cowl
T.SourceIntelDB[51536]={currency="Honor Points",amount=2370,family="Wrathful"} -- Felweave Raiment
T.SourceIntelDB[51537]={currency="Honor Points",amount=1430,family="Wrathful"} -- Felweave Handguards
T.SourceIntelDB[51539]={currency="Honor Points",amount=2370,family="Wrathful"} -- Felweave Trousers
T.SourceIntelDB[51540]={currency="Honor Points",amount=1930,family="Wrathful"} -- Felweave Amice

-- Verified legacy Wrath vendor source. The local DB2 exports identify this as a
-- vendor appearance but do not contain merchant inventory rows, so bundle the
-- known vendor locations while allowing the live merchant learner to capture
-- the exact MoP Classic price whenever the player visits one.
T.SourceIntelDB[47682]={
    sourceKind="Vendor",
    vendors={
        {name="Magistrix Iruvia",title="Legacy Justice Quartermaster",zone="Icecrown",faction="Neutral"},
        {name="Magistrix Vesara",title="Legacy Justice Quartermaster",zone="Dalaran",faction="Neutral"},
    },
    note="Legacy Wrath vendor appearance. The current MoP Classic price is learned automatically when you visit the merchant."
}


-- Mirror curated records into the fast Source DB format.
if T.SourceDB then
    local function wrathful(id,amount)
        T.SourceDB[id]={kind="Vendor",currency="Honor Points",amount=amount,family="Wrathful"}
    end
    wrathful(51538,2370); wrathful(51536,2370); wrathful(51537,1430); wrathful(51539,2370); wrathful(51540,1930)
end


-- In-game vendor directions for source records whose exact merchant inventory is
-- not part of the static client DB. These hints are bundled so players never
-- need to alt-tab just to learn where to start looking.
local function contains(hay,needle)
    return (hay or ""):lower():find((needle or ""):lower(),1,true)~=nil
end

function T:GetVendorGuide(rec)
    if self.clientMode and self.clientMode~="mop-classic" then return nil end
    if not rec then return nil end
    local name=rec.itemName or rec.name or ""
    local faction=playerFaction()
    local guide={}

    -- Siege of Orgrimmar paladin tier token exchange.
    if contains(name,"Winged Triumph") then
        guide.title="Siege of Orgrimmar Tier Vendor"
        guide.note="Bring the matching Siege of Orgrimmar tier token for the item variant you want."
        if faction=="Alliance" then
            guide.vendors={{name="Clarice Chapmann",title="Raid Vendor",zone="Shrine of Seven Stars",where="The Golden Lantern / raid-vendor area"}}
        elseif faction=="Horde" then
            guide.vendors={{name="Ki'agnuu",title="Raid Vendor",zone="Shrine of Two Moons",where="The Imperial Mercantile / raid-vendor area"}}
        else
            guide.vendors={
                {name="Clarice Chapmann",title="Raid Vendor",zone="Shrine of Seven Stars"},
                {name="Ki'agnuu",title="Raid Vendor",zone="Shrine of Two Moons"},
            }
        end
        guide.whereToGo=(faction=="Horde") and "Go to Shrine of Two Moons and find the raid vendors." or "Go to Shrine of Seven Stars and find the raid vendors."
        return guide
    end

    -- MoP PvP seasons. Blizzard placed copies of these vendors on the second
    -- floors of the faction shrines in MoP Classic, which is the most reliable
    -- current destination even when the Serpent's Spine vendors phase badly.
    if contains(name,"Dreadful Gladiator") or contains(name,"Malevolent Gladiator") or
       contains(name,"Tyrannical Gladiator") or contains(name,"Grievous Gladiator") or
       contains(name,"Prideful Gladiator") then
        guide.title="Mists of Pandaria PvP Vendor"
        if faction=="Alliance" then
            guide.whereToGo="Go to the 2nd floor of Shrine of Seven Stars. Look for the Alliance PvP vendor marker on the map."
            guide.vendors={{name="MoP PvP vendors",zone="Shrine of Seven Stars",where="2nd floor / Alliance PvP marker",faction="Alliance"}}
        elseif faction=="Horde" then
            guide.whereToGo="Go to the 2nd floor of Shrine of Two Moons. Look for the Horde PvP vendor marker on the map."
            guide.vendors={{name="MoP PvP vendors",zone="Shrine of Two Moons",where="2nd floor / Horde PvP marker",faction="Horde"}}
        else
            guide.whereToGo="Visit your faction shrine's 2nd-floor PvP vendors."
        end
        guide.note="Legacy Serpent's Spine vendors may also exist, but the shrine copies are the most reliable MoP Classic location."
        return guide
    end

    -- Burning Crusade arena gear. Area 52 is the simplest neutral legacy hub.
    if contains(name,"Gladiator's") and (contains(name,"Merciless") or contains(name,"Vengeful") or contains(name,"Brutal") or contains(name,"Gladiator's")) then
        guide.title="Burning Crusade Arena Vendor"
        guide.whereToGo="Start at Area 52 in Netherstorm. Find the legacy arena gear vendors."
        guide.vendors={{name="Kezzik the Striker",title="Gladiator, Merciless, & Vengeful Gear",zone="Area 52, Netherstorm",faction="Neutral"}}
        guide.note="This gear can also appear on legacy arena vendors in Shattrath City and other Burning Crusade PvP hubs. Area 52 is the easiest first stop."
        return guide
    end

    return nil
end

function T:GetSourceIntel(rec)
    if self.clientMode and self.clientMode~="mop-classic" then return nil end
    if not rec then return nil end
    local exact=rec.itemID and self.SourceIntelDB[rec.itemID] or nil
    local name=rec.itemName or rec.name or ""
    local lower=name:lower()
    local faction=playerFaction()
    local out={faction=faction,itemName=name}

    if exact then
        for k,v in pairs(exact) do out[k]=v end
    end

    if out.family=="Wrathful" or lower:find("wrathful gladiator's",1,true) then
        out.family="Wrathful"
        out.sourceKind="Vendor"
        out.vendors=WRATHFUL_VENDORS[faction] or WRATHFUL_VENDORS.Neutral
        if not out.amount then
            local slot=slotFromName(name)
            out.amount=slot and WRATHFUL_COST[slot] or nil
        end
        out.currency=out.currency or "Honor Points"
        out.note="Legacy Season 8 PvP armor. Faction-specific capital vendor plus neutral legacy arena vendors."
        return out
    end

    return exact and out or nil
end

function T:FormatSourceIntel(rec)
    local intel=self:GetSourceIntel(rec)
    if not intel then return nil end
    local lines={}
    local function add(s) if s and s~="" then lines[#lines+1]=s end end
    add("HOW TO OBTAIN")
    add("Item: "..(intel.itemName or rec.itemName or rec.name or "Unknown item"))
    if intel.sourceKind then add("Source: "..intel.sourceKind) end
    if intel.faction and intel.faction~="Neutral" then add("Your faction: "..intel.faction) end
    if intel.vendors and #intel.vendors>0 then
        add("Vendors:")
        for _,v in ipairs(intel.vendors) do
            local tag=(v.faction and v.faction~="Neutral") and (" ["..v.faction.."]") or " [Neutral]"
            local title=(v.title and v.title~="") and (" <"..v.title..">") or ""
            add("  "..v.name..title..tag)
            if v.zone then add("    "..v.zone) end
        end
    end
    if intel.amount and intel.currency then
        add("Cost: "..tostring(intel.amount).." "..intel.currency)
    elseif intel.currency then
        add("Currency: "..intel.currency)
    end
    if intel.note then add(intel.note) end
    add("Data: bundled Wowhead source data")
    return table.concat(lines,"\n")
end
