local _, ns = ...

------------------------------------------------------------
-- Tier set slots (equipment slot IDs)
------------------------------------------------------------
local TIER_SLOTS = {
    { slot = 1,  name = "Head"     },
    { slot = 3,  name = "Shoulder" },
    { slot = 5,  name = "Chest"    },
    { slot = 7,  name = "Legs"     },
    { slot = 10, name = "Hands"    },
}

------------------------------------------------------------
-- Hidden tooltip for scanning set bonus text
------------------------------------------------------------
local scanTooltip = CreateFrame("GameTooltip", "YYHScanTooltip", nil, "GameTooltipTemplate")
scanTooltip:SetOwner(WorldFrame, "ANCHOR_NONE")

local function IsSetItem(unit, slotID)
    scanTooltip:ClearLines()
    local ok = pcall(scanTooltip.SetInventoryItem, scanTooltip, unit, slotID)
    if not ok then return false end

    for i = 1, scanTooltip:NumLines() do
        local line = _G["YYHScanTooltipTextLeft" .. i]
        if line then
            local text = line:GetText()
            if text and text:find("Set:") then
                return true
            end
        end
    end
    return false
end

------------------------------------------------------------
-- Raid inspection state
------------------------------------------------------------
ns.RaidInspectData = {}
local inspectQueue = {}
local inspectBusy = false
local inspectTimer = nil

local function ProcessNextInspect()
    if #inspectQueue == 0 then
        inspectBusy = false
        if ns.RefreshRaidDisplay then
            ns:RefreshRaidDisplay()
        end
        return
    end

    inspectBusy = true
    local unit = table.remove(inspectQueue, 1)

    if not UnitExists(unit) or not UnitIsConnected(unit) or not CanInspect(unit) then
        -- Skip, try next
        C_Timer.After(0.1, ProcessNextInspect)
        return
    end

    NotifyInspect(unit)

    -- Timeout fallback in case INSPECT_READY never fires
    if inspectTimer then inspectTimer:Cancel() end
    inspectTimer = C_Timer.NewTimer(2, function()
        ProcessNextInspect()
    end)
end

-- Event handler for inspect results
local inspectFrame = CreateFrame("Frame")
inspectFrame:RegisterEvent("INSPECT_READY")
inspectFrame:SetScript("OnEvent", function(self, event, guid)
    if event ~= "INSPECT_READY" then return end

    -- Find which unit matches this GUID
    local unit = nil
    local numMembers = GetNumGroupMembers()
    local prefix = IsInRaid() and "raid" or "party"

    if IsInRaid() then
        for i = 1, numMembers do
            local raidUnit = prefix .. i
            if UnitGUID(raidUnit) == guid then
                unit = raidUnit
                break
            end
        end
    else
        for i = 1, numMembers - 1 do
            local partyUnit = prefix .. i
            if UnitGUID(partyUnit) == guid then
                unit = partyUnit
                break
            end
        end
        if not unit and UnitGUID("player") == guid then
            unit = "player"
        end
    end

    if unit then
        local name = UnitName(unit)
        local _, class = UnitClass(unit)
        local tierCount = 0
        local tierSlots = {}

        for _, tierSlot in ipairs(TIER_SLOTS) do
            local hasSet = IsSetItem(unit, tierSlot.slot)
            if hasSet then
                tierCount = tierCount + 1
                table.insert(tierSlots, tierSlot.name)
            end
        end

        ns.RaidInspectData[guid] = {
            name = name,
            class = class,
            tierCount = tierCount,
            tierSlots = tierSlots,
            unit = unit,
        }

        -- Update display if visible
        if ns.RaidFrame and ns.RaidFrame:IsShown() then
            ns:RefreshRaidDisplay()
        end
    end

    if inspectTimer then inspectTimer:Cancel() end
    C_Timer.After(0.3, ProcessNextInspect)
end)

------------------------------------------------------------
-- Start a full raid scan
------------------------------------------------------------
function ns:ScanRaid()
    wipe(ns.RaidInspectData)
    wipe(inspectQueue)

    local numMembers = GetNumGroupMembers()

    -- Add player first
    local playerGUID = UnitGUID("player")
    local playerName = UnitName("player")
    local _, playerClass = UnitClass("player")
    local playerTier = 0
    local playerSlots = {}
    for _, tierSlot in ipairs(TIER_SLOTS) do
        if IsSetItem("player", tierSlot.slot) then
            playerTier = playerTier + 1
            table.insert(playerSlots, tierSlot.name)
        end
    end
    ns.RaidInspectData[playerGUID] = {
        name = playerName,
        class = playerClass,
        tierCount = playerTier,
        tierSlots = playerSlots,
        unit = "player",
    }

    -- Queue everyone else
    local prefix = IsInRaid() and "raid" or "party"
    if IsInRaid() then
        for i = 1, numMembers do
            local unit = prefix .. i
            if UnitExists(unit) and not UnitIsUnit(unit, "player") then
                table.insert(inspectQueue, unit)
            end
        end
    else
        for i = 1, numMembers - 1 do
            local unit = prefix .. i
            if UnitExists(unit) then
                table.insert(inspectQueue, unit)
            end
        end
    end

    print("|cff00ff00YippYapp Helper|r: Scanning " .. (#inspectQueue + 1) .. " players for tier pieces...")

    if ns.RaidFrame then
        ns:RefreshRaidDisplay()
        ns.RaidFrame:Show()
    end

    ProcessNextInspect()
end

------------------------------------------------------------
-- Get sorted tier data (fewest pieces first = highest loot prio)
------------------------------------------------------------
function ns:GetSortedTierData()
    local sorted = {}
    for guid, data in pairs(ns.RaidInspectData) do
        table.insert(sorted, data)
    end
    table.sort(sorted, function(a, b)
        if a.tierCount ~= b.tierCount then
            return a.tierCount > b.tierCount
        end
        return a.name < b.name
    end)
    return sorted
end

------------------------------------------------------------
-- Tier distribution summary
------------------------------------------------------------
function ns:GetTierSummary()
    local counts = { [0] = 0, [1] = 0, [2] = 0, [3] = 0, [4] = 0, [5] = 0 }
    local total = 0
    local with2set = 0
    local with4set = 0

    for _, data in pairs(ns.RaidInspectData) do
        local c = data.tierCount
        counts[c] = (counts[c] or 0) + 1
        total = total + 1
        if c >= 2 then with2set = with2set + 1 end
        if c >= 4 then with4set = with4set + 1 end
    end

    return {
        counts = counts,
        total = total,
        with2set = with2set,
        with4set = with4set,
    }
end
