-- ============================================================================
-- Vamoose's Endeavors - Leaderboard Tab
-- Shows neighborhood contribution rankings
-- ============================================================================

VE = VE or {}
VE.UI = VE.UI or {}
VE.UI.Tabs = VE.UI.Tabs or {}

-- Helper to get current theme colors
local function GetColors()
    return VE.Constants:GetThemeColors()
end

-- My characters tracking (highlights all player's alts in leaderboard)
local function GetMyCharacters()
    VE_DB = VE_DB or {}
    VE_DB.myCharacters = VE_DB.myCharacters or {}
    return VE_DB.myCharacters
end

local function RegisterCurrentCharacter()
    local charName = UnitName("player")
    if charName then
        local myChars = GetMyCharacters()
        myChars[charName] = true
    end
end

local function IsMyCharacter(name)
    local myChars = GetMyCharacters()
    return myChars[name] == true
end

function VE.UI.Tabs:CreateLeaderboard(parent)
    local UI = VE.Constants.UI

    -- Register current character for multi-char highlighting
    RegisterCurrentCharacter()

    local container = CreateFrame("Frame", nil, parent)
    container:SetAllPoints()

    local padding = 0  -- Container edge padding (0 for full-bleed atlas backgrounds)

    -- ========================================================================
    -- LEADERBOARD HEADER (with contribution pip icon)
    -- ========================================================================

    local header = VE.UI:CreateSectionHeader(container, "Leaderboard")
    header:SetPoint("TOPLEFT", 0, UI.sectionHeaderYOffset)
    header:SetPoint("TOPRIGHT", 0, UI.sectionHeaderYOffset)

    -- ========================================================================
    -- LEADERBOARD LIST (Scrollable)
    -- ========================================================================

    local listContainer = CreateFrame("Frame", nil, container, "BackdropTemplate")
    listContainer:SetPoint("TOPLEFT", header, "BOTTOMLEFT", 0, 0)
    listContainer:SetPoint("BOTTOMRIGHT", -padding, padding)
    listContainer:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8x8",
        edgeFile = nil,
    })
    container.listContainer = listContainer

    -- Atlas background support
    local ApplyListContainerColors = VE.UI:AddAtlasBackground(listContainer)
    ApplyListContainerColors()

    local _, scrollContent = VE.UI:CreateScrollFrame(listContainer)
    container.scrollContent = scrollContent

    -- Pool of leaderboard rows
    container.rows = {}

    -- ========================================================================
    -- SUMMARY ROW (Total for all player characters)
    -- ========================================================================

    local summaryRow = CreateFrame("Frame", nil, scrollContent, "BackdropTemplate")
    summaryRow:SetHeight(26)
    summaryRow:SetPoint("TOPLEFT", 0, 0)
    summaryRow:SetPoint("TOPRIGHT", 0, 0)
    summaryRow:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 1,
    })
    container.summaryRow = summaryRow

    local summaryC = GetColors()
    summaryRow:SetBackdropColor(summaryC.accent.r, summaryC.accent.g, summaryC.accent.b, 0.2)
    summaryRow:SetBackdropBorderColor(summaryC.accent.r, summaryC.accent.g, summaryC.accent.b, 0.4)

    -- Sum icon
    local summaryIcon = summaryRow:CreateTexture(nil, "ARTWORK")
    summaryIcon:SetSize(16, 16)
    summaryIcon:SetPoint("LEFT", 6, 0)
    summaryIcon:SetAtlas("housefinder_neighborhood-friends-icon")
    summaryRow.icon = summaryIcon

    -- Sum label
    local summaryLabel = summaryRow:CreateFontString(nil, "OVERLAY")
    summaryLabel:SetPoint("LEFT", summaryIcon, "RIGHT", 4, 0)
    VE.Theme.ApplyFont(summaryLabel, summaryC)
    summaryLabel:SetText("My Total")
    summaryLabel:SetTextColor(summaryC.accent.r, summaryC.accent.g, summaryC.accent.b)
    summaryRow.label = summaryLabel

    -- Character count
    local summaryCount = summaryRow:CreateFontString(nil, "OVERLAY")
    summaryCount:SetPoint("LEFT", summaryLabel, "RIGHT", 6, 0)
    VE.Theme.ApplyFont(summaryCount, summaryC, "small")
    summaryCount:SetTextColor(summaryC.text_dim.r, summaryC.text_dim.g, summaryC.text_dim.b)
    summaryRow.charCount = summaryCount

    -- Total amount
    local summaryAmount = summaryRow:CreateFontString(nil, "OVERLAY")
    summaryAmount:SetPoint("RIGHT", -8, 0)
    summaryAmount:SetJustifyH("RIGHT")
    VE.Theme.ApplyFont(summaryAmount, summaryC)
    summaryAmount:SetTextColor(summaryC.endeavor.r, summaryC.endeavor.g, summaryC.endeavor.b)
    summaryRow.amount = summaryAmount

    summaryRow:Hide()

    function container:UpdateSummaryRow(contributions)
        local myChars = GetMyCharacters()
        local totalContrib = 0
        local charCount = 0

        for name, _ in pairs(myChars) do
            if contributions[name] then
                totalContrib = totalContrib + contributions[name]
                charCount = charCount + 1
            end
        end

        if charCount > 0 then
            local colors = GetColors()
            self.summaryRow:SetBackdropColor(colors.accent.r, colors.accent.g, colors.accent.b, 0.2)
            self.summaryRow:SetBackdropBorderColor(colors.accent.r, colors.accent.g, colors.accent.b, 0.4)
            self.summaryRow.label:SetTextColor(colors.accent.r, colors.accent.g, colors.accent.b)
            VE.Theme.ApplyFont(self.summaryRow.label, colors)
            self.summaryRow.charCount:SetText("(" .. charCount .. " char" .. (charCount > 1 and "s" or "") .. ")")
            self.summaryRow.charCount:SetTextColor(colors.text_dim.r, colors.text_dim.g, colors.text_dim.b)
            VE.Theme.ApplyFont(self.summaryRow.charCount, colors, "small")
            self.summaryRow.amount:SetText(string.format("%.1f", totalContrib))
            self.summaryRow.amount:SetTextColor(colors.endeavor.r, colors.endeavor.g, colors.endeavor.b)
            VE.Theme.ApplyFont(self.summaryRow.amount, colors)
            self.summaryRow:Show()
            return true
        else
            self.summaryRow:Hide()
            return false
        end
    end

    -- ========================================================================
    -- CREATE LEADERBOARD ROW
    -- ========================================================================

    local function CreateLeaderboardRow(parentFrame)
        local C = GetColors()
        local row = CreateFrame("Frame", nil, parentFrame, "BackdropTemplate")
        row:SetHeight(24)
        row:SetBackdrop({
            bgFile = "Interface\\Buttons\\WHITE8x8",
            edgeFile = nil,
        })
        row:SetBackdropColor(C.panel.r, C.panel.g, C.panel.b, C.panel.a * 0.5)

        -- Rank number
        local rank = row:CreateFontString(nil, "OVERLAY")
        rank:SetPoint("LEFT", 8, 0)
        rank:SetWidth(32)
        rank:SetJustifyH("CENTER")
        rank:SetTextColor(C.gold.r, C.gold.g, C.gold.b)
        VE.Theme.ApplyFont(rank, C)
        row.rank = rank

        -- Player name
        local name = row:CreateFontString(nil, "OVERLAY")
        name:SetPoint("LEFT", rank, "RIGHT", 8, 0)
        name:SetPoint("RIGHT", -80, 0)
        name:SetJustifyH("LEFT")
        name:SetTextColor(C.text.r, C.text.g, C.text.b)
        VE.Theme.ApplyFont(name, C)
        row.name = name

        -- Contribution amount
        local amount = row:CreateFontString(nil, "OVERLAY")
        amount:SetPoint("RIGHT", -8, 0)
        amount:SetJustifyH("RIGHT")
        amount:SetTextColor(C.endeavor.r, C.endeavor.g, C.endeavor.b)
        VE.Theme.ApplyFont(amount, C)
        row.amount = amount

        -- Track if this is the current player for hover states
        row.isCurrentPlayer = false

        -- Hover effect (uses fresh colors)
        row:EnableMouse(true)
        row:SetScript("OnEnter", function(self)
            local colors = GetColors()
            if self.isCurrentPlayer then
                self:SetBackdropColor(colors.accent.r, colors.accent.g, colors.accent.b, colors.accent.a * 0.25)
            else
                self:SetBackdropColor(colors.text_dim.r, colors.text_dim.g, colors.text_dim.b, colors.text_dim.a * 0.3)
            end
        end)
        row:SetScript("OnLeave", function(self)
            local colors = GetColors()
            if self.isCurrentPlayer then
                self:SetBackdropColor(colors.accent.r, colors.accent.g, colors.accent.b, colors.accent.a * 0.15)
            else
                self:SetBackdropColor(colors.panel.r, colors.panel.g, colors.panel.b, colors.panel.a * 0.5)
            end
        end)

        function row:SetData(rankNum, playerName, contribution)
            local colors = GetColors()
            self.rank:SetText("#" .. rankNum)
            self.name:SetText(playerName)
            self.amount:SetText(string.format("%.1f", contribution))

            -- Gold/Silver/Bronze colors for top 3, otherwise use text_dim
            if rankNum == 1 then
                self.rank:SetTextColor(colors.gold.r, colors.gold.g, colors.gold.b)
            elseif rankNum == 2 then
                self.rank:SetTextColor(colors.silver.r, colors.silver.g, colors.silver.b)
            elseif rankNum == 3 then
                self.rank:SetTextColor(colors.bronze.r, colors.bronze.g, colors.bronze.b)
            else
                self.rank:SetTextColor(colors.text_dim.r, colors.text_dim.g, colors.text_dim.b)
            end
            VE.Theme.ApplyFont(self.rank, colors)

            self.amount:SetTextColor(colors.endeavor.r, colors.endeavor.g, colors.endeavor.b)
            VE.Theme.ApplyFont(self.amount, colors)

            -- Highlight player's characters (current + alts)
            self.isCurrentPlayer = IsMyCharacter(playerName)
            if self.isCurrentPlayer then
                self.name:SetTextColor(colors.accent.r, colors.accent.g, colors.accent.b, colors.accent.a)
                self:SetBackdropColor(colors.accent.r, colors.accent.g, colors.accent.b, colors.accent.a * 0.15)
            else
                self.name:SetTextColor(colors.text.r, colors.text.g, colors.text.b, colors.text.a)
                self:SetBackdropColor(colors.panel.r, colors.panel.g, colors.panel.b, colors.panel.a * 0.5)
            end
            VE.Theme.ApplyFont(self.name, colors)
        end

        return row
    end

    -- ========================================================================
    -- UPDATE FUNCTION
    -- ========================================================================

    -- Loading text
    local loadingColors = GetColors()
    container.loadingText = container.scrollContent:CreateFontString(nil, "OVERLAY")
    container.loadingText:SetPoint("CENTER", container.scrollContent, "CENTER", 0, 0)
    VE.Theme.ApplyFont(container.loadingText, loadingColors)
    container.loadingText:SetText("Loading activity data...")
    container.loadingText:SetTextColor(loadingColors.text_dim.r, loadingColors.text_dim.g, loadingColors.text_dim.b)
    container.loadingText:Hide()

    function container:Update(forceUpdate)
        -- Skip rebuild if data hasn't changed (optimization)
        local currentTimestamp = VE.EndeavorTracker and VE.EndeavorTracker.activityLogLastUpdated
        if not forceUpdate and self.lastActivityUpdate and self.lastActivityUpdate == currentTimestamp then
            return
        end
        self.lastActivityUpdate = currentTimestamp

        -- Hide all existing rows
        for _, row in ipairs(self.rows) do
            row:Hide()
        end

        -- Get activity log data
        local activityData = VE.EndeavorTracker:GetActivityLogData()
        if not activityData or not activityData.taskActivity then
            -- Show loading or empty state
            if not self.emptyText then
                self.emptyText = self.scrollContent:CreateFontString(nil, "OVERLAY")
                self.emptyText:SetPoint("CENTER", self.scrollContent, "CENTER", 0, 0)
            end

            -- Apply theme color and font to empty text
            local colors = GetColors()
            VE.Theme.ApplyFont(self.emptyText, colors)
            self.emptyText:SetTextColor(colors.text_dim.r, colors.text_dim.g, colors.text_dim.b, colors.text_dim.a)

            -- Check fetch status to show appropriate message
            local fetchStatus = VE.EndeavorTracker and VE.EndeavorTracker.fetchStatus
            local isFetching = fetchStatus and (fetchStatus.state == "fetching" or fetchStatus.state == "retrying" or fetchStatus.state == "pending")

            if isFetching then
                self.emptyText:SetText("Loading activity data...")
                if self.setActiveButton then self.setActiveButton:Hide() end
            else
                self.emptyText:SetText("No activity data available.\nThis house is not set as your active endeavor.")
                -- Create button via factory (once)
                if not self.setActiveButton then
                    self.setActiveButton = VE.UI:CreateSetAsActiveButton(self.scrollContent, self.emptyText)
                end
                self.setActiveButton:Show()
            end
            self.emptyText:Show()
            self.scrollContent:SetHeight(100)
            return
        end

        if self.emptyText then
            self.emptyText:Hide()
        end
        if self.setActiveButton then
            self.setActiveButton:Hide()
        end

        -- Aggregate contributions by player
        local contributions = {}
        for _, entry in ipairs(activityData.taskActivity) do
            local playerName = entry.playerName or "Unknown"
            local amt = entry.amount or 0
            contributions[playerName] = (contributions[playerName] or 0) + amt
        end

        -- Sort by contribution (highest first)
        local sorted = {}
        for playerName, amt in pairs(contributions) do
            table.insert(sorted, { name = playerName, amount = amt })
        end
        table.sort(sorted, function(a, b) return a.amount > b.amount end)

        -- Update summary row (shows total for all player characters)
        local hasSummary = self:UpdateSummaryRow(contributions)

        -- Display rows
        local yOffset = hasSummary and 30 or 0  -- Leave room for summary row
        local rowHeight = 24
        local rowSpacing = 2

        for i, data in ipairs(sorted) do
            local row = self.rows[i]
            if not row then
                row = CreateLeaderboardRow(self.scrollContent)
                self.rows[i] = row
            end

            row:SetPoint("TOPLEFT", 0, -yOffset)
            row:SetPoint("TOPRIGHT", 0, -yOffset)
            row:SetData(i, data.name, data.amount)
            row:Show()

            yOffset = yOffset + rowHeight + rowSpacing
        end

        self.scrollContent:SetHeight(yOffset + 10)
    end

    -- Initial update when shown
    container:SetScript("OnShow", function(self)
        -- Request fresh data
        if C_NeighborhoodInitiative and C_NeighborhoodInitiative.RequestInitiativeActivityLog then
            C_NeighborhoodInitiative.RequestInitiativeActivityLog()
        end
        -- Show loading state immediately
        self:Update()
    end)

    -- Listen for activity log updates
    VE.EventBus:Register("VE_ACTIVITY_LOG_UPDATED", function()
        if container:IsShown() then
            container:Update()
        end
    end)

    -- Listen for theme updates to refresh colors
    VE.EventBus:Register("VE_THEME_UPDATE", function()
        ApplyListContainerColors()
        if container:IsShown() then
            container:Update()
        end
    end)

    return container
end
