--[[
============================================================
Decor Vendor Addon
© 2026 MidniteDestiny. All Rights Reserved.
============================================================

This file is part of the Decor Vendor addon.

All code, structure, and design are the intellectual
property of MidniteDestiny unless otherwise stated.

You may NOT:
• Copy, reproduce, or redistribute this code
• Modify and redistribute this code
• Use this code in other addons or projects

without explicit permission from the author.

This addon is distributed for personal use only.

============================================================
]]

local addonName, dv = ...

local C = dv.CONSTANTS

if not C then
    print("|cffff4040DecorVendor BossDropRows:|r constants are missing.")
    return
end

-- ============================================================
-- 🛡️ COMPATIBILITY GHOST HEADER (REMOVES ACCORDION SECTIONS SAFELY)
-- ============================================================
function dv.CreateBossDropHeader(parent, group, collected, total, y)
    local key = "boss_" .. (group.name or "Unknown")
    dv.collapsedHeaders = dv.collapsedHeaders or {}
    if dv.collapsedHeaders[key] == nil then
        dv.collapsedHeaders[key] = false -- Keep rows streaming out uncollapsed
    end

    -- Return false and y - 1 to make the section dividers completely invisible
    -- while keeping ListBuilders.lua tracking perfectly happy!
    return false, y - 1
end

-- ============================================================
-- 🖼️ SAFE PREVIEW INSPECTOR ENGINE
-- ============================================================
function dv.UpdateBossPreview(itemData)
    if not itemData then return end

    local ms = dv.modelScene
    local actor = dv.previewActor
    local texture = dv.texture

    if not ms or not actor then return end

    dv.contentArea._isVendorPreview = false

    -- Title
    local itemObj = Item:CreateFromItemID(itemData.id)
    itemObj:ContinueOnItemLoad(function()
        if dv.modelTitle then
            dv.modelTitle:SetText(itemObj:GetItemName() or "Preview")
            dv.modelTitle:SetTextColor(1, 1, 1)
        end
    end)

    -- Model Loading
    if itemData.model3D then
        if texture then texture:Hide() end
        ms:Show()

        dv:ShowModel(ms, itemData.model3D)

        C_Timer.After(0.01, function()
            local camera = ms:GetActiveCamera()
            if not camera then return end

            local cam = dv.DecorCameraOverrides and dv.DecorCameraOverrides[itemData.model3D]

            if camera.TargetActor then
                camera:TargetActor(actor, true)
            end

            if camera.SetPitch then
                camera:SetPitch(0.2)
            end

            if cam and cam.zoom and camera.SetZoomDistance then
                camera:SetZoomDistance(cam.zoom)
            end
        end)
    else
        ms:Hide()
    end

    -- Boss Drop Notes Wrapper
    if not dv.bossNotes then
        dv.bossNotes = CreateFrame("Frame", nil, dv.itemContainer)
        dv.bossNotes:SetFrameLevel((dv.itemContainer:GetFrameLevel() or 1) + 8)

        dv.bossNotes.bg = dv.bossNotes:CreateTexture(nil, "BACKGROUND")
        dv.bossNotes.bg:SetAllPoints()
        dv.bossNotes.bg:SetColorTexture(0.08, 0.08, 0.08, 0.75)

        dv.bossNotes.text = dv.bossNotes:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        dv.bossNotes.text:SetPoint("TOPLEFT", dv.bossNotes, "TOPLEFT", 10, -10)
        dv.bossNotes.text:SetPoint("BOTTOMRIGHT", dv.bossNotes, "BOTTOMRIGHT", -10, 10)
        dv.bossNotes.text:SetJustifyH("LEFT")
        dv.bossNotes.text:SetJustifyV("TOP")
        dv.bossNotes.text:SetWordWrap(true)
        dv.bossNotes.text:SetNonSpaceWrap(false)
        dv.bossNotes.text:SetFont(STANDARD_TEXT_FONT, 11, "")
    end

    local lines = {}

    if itemData.bossencounter then
        local encounterName = EJ_GetEncounterInfo(itemData.bossencounter)
        table.insert(lines, "|cffFFD200Boss:|r " .. (encounterName or "Unknown Boss"))
    end

    if itemData.instance or itemData.bossevent then
        local loc = itemData.instance or itemData.bossevent
        table.insert(lines, "|cff00fbffEvent:|r " .. tostring(loc))
    end

    if itemData.zone then
        table.insert(lines, "|cff00fbffLocation:|r " .. tostring(itemData.zone))
    end

    if itemData.note and itemData.note ~= "" then
        table.insert(lines, "|cff00ff00Note:|r " .. tostring(itemData.note))
    end

    if #lines == 0 then
        dv.bossNotes:Hide()
        return
    end

    local fullBossInfo = table.concat(lines, "\n")

    local plainText = fullBossInfo
        :gsub("|c%x%x%x%x%x%x%x%x", "")
        :gsub("|r", "")
        :gsub("|T.-|t", "")

    local estimatedLines = 0
    local charsPerLine = 36

    for line in string.gmatch(plainText .. "\n", "(.-)\n") do
        if line == "" then
            estimatedLines = estimatedLines + 1
        else
            estimatedLines = estimatedLines + math.max(1, math.ceil(string.len(line) / charsPerLine))
        end
    end

    local noteHeight = math.max(88, estimatedLines * 14 + 24)
    noteHeight = math.min(noteHeight, 210)

    dv.bossNotes:ClearAllPoints()
    dv.bossNotes:SetPoint("TOPLEFT", dv.modelTitle, "BOTTOMLEFT", 0, -8)
    dv.bossNotes:SetPoint("TOPRIGHT", dv.modelTitle, "BOTTOMRIGHT", 0, -8)
    dv.bossNotes:SetHeight(noteHeight)

    dv.bossNotes.text:SetText(fullBossInfo)
    dv.bossNotes:Show()
end

-- ============================================================
-- ============================================================
-- ⚔️ PREMIUM MIDNIGHT WIDE CARD BOSS DROP ROW LAYOUT (FIXED DELVES)
-- ============================================================
function dv.CreateBossDropLine(parent, itemData, y)
    local id = itemData.id
    local isCollected = dv.IsItemCollected(id)

    -- 🌟 1. Main Row Card Container Button
    local rowHeight = 44
    local line = CreateFrame("Button", nil, parent, "BackdropTemplate")
    line:SetPoint("TOPLEFT", parent, "TOPLEFT", 8, y)
    line:SetPoint("RIGHT", parent, "RIGHT", -12, 0)
    line:SetHeight(rowHeight)
    line:RegisterForClicks("AnyUp")

    -- 🌟 2. Custom Premium Midnight Backdrop 
    line:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        tile = false,
        tileSize = 0,
        edgeSize = 1,
        insets = { left = 0, right = 0, top = 0, bottom = 0 }
    })
    line:SetBackdropColor(0.12, 0.08, 0.18, 0.85)       
    line:SetBackdropBorderColor(0.25, 0.20, 0.35, 0.6) 

    -- 🌟 3. LEFT EDGE: Split Color Status Strip Indicators
    local topIndicator = line:CreateTexture(nil, "OVERLAY")
    topIndicator:SetWidth(4)
    topIndicator:SetPoint("TOPLEFT", line, "TOPLEFT", 0, 0)
    topIndicator:SetPoint("BOTTOMLEFT", line, "LEFT", 0, 0)
    topIndicator:SetTexture("Interface\\Buttons\\WHITE8x8")

    local bottomIndicator = line:CreateTexture(nil, "OVERLAY")
    bottomIndicator:SetWidth(4)
    bottomIndicator:SetPoint("TOPLEFT", line, "LEFT", 0, 0)
    bottomIndicator:SetPoint("BOTTOMLEFT", line, "BOTTOMLEFT", 0, 0)
    bottomIndicator:SetTexture("Interface\\Buttons\\WHITE8x8")

    if isCollected then
        topIndicator:SetVertexColor(0.2, 1.0, 0.5, 1)
        bottomIndicator:SetVertexColor(0.2, 1.0, 0.5, 1)
    else
        topIndicator:SetVertexColor(1.0, 0.2, 0.2, 1)    
        bottomIndicator:SetVertexColor(0.4, 0.4, 0.4, 1) 
    end

    -- 🌟 4. True Reward Decor Icon Artwork Loader
    local icon = line:CreateTexture(nil, "ARTWORK")
    icon:SetSize(32, 32)
    icon:SetPoint("LEFT", line, "LEFT", 12, 0)
    icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)
    
    local itemTexture = "Interface\\Icons\\INV_Misc_Armband_01"
    if id then
        itemTexture = C_Item and C_Item.GetItemIconByID and C_Item.GetItemIconByID(id) or GetItemIcon(id) or itemTexture
    end
    icon:SetTexture(itemTexture)

    -- Quality Rim Inset Border Box Frame
    local iconBorder = CreateFrame("Frame", nil, line, "BackdropTemplate")
    iconBorder:SetSize(36, 36)
    iconBorder:SetPoint("CENTER", icon, "CENTER", 0, 0)
    local lineLevel = line:GetFrameLevel() or 1
    iconBorder:SetFrameLevel(math.max(0, lineLevel - 1))
    iconBorder:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        tile = false,
        edgeSize = 1,
        insets = { left = 1, right = 1, top = 1, bottom = 1 },
    })
    iconBorder:SetBackdropColor(0.05, 0.05, 0.05, 0.8)
    iconBorder:SetBackdropBorderColor(0.35, 0.30, 0.50, 0.6)

    -- 🌟 5. LEFT SIDE TEXTS: Primary Decor Item Name Label
    local nameFS = line:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    nameFS:SetFont(STANDARD_TEXT_FONT, 11, "")
    nameFS:SetPoint("LEFT", icon, "RIGHT", 12, 4)
    nameFS:SetWidth(185)
    nameFS:SetJustifyH("LEFT")
    nameFS:SetText("Loading loot properties...")

    -- Left Subtext: Sourced description status layout
    local subText = line:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    subText:SetFont(STANDARD_TEXT_FONT, 11, "")
    subText:SetPoint("TOPLEFT", nameFS, "BOTTOMLEFT", 0, -2)
    subText:SetWidth(185)
    subText:SetJustifyH("LEFT")
    
    if isCollected then
        nameFS:SetTextColor(0.2, 1.0, 0.5)
        subText:SetTextColor(0.2, 1.0, 0.5)
        subText:SetText("Collected & Owned")
    else
        nameFS:SetTextColor(0.92, 0.90, 0.96)
        subText:SetTextColor(0.70, 0.68, 0.78)
        subText:SetText("Loot Uncollected")
    end

-- Asynchronous asset data loading loops
    local item = Item:CreateFromItemID(id)
    item:ContinueOnItemLoad(function()
        if nameFS then
            -- 🚀 THE FORCE-NAME FIX:
            -- If we manually typed a clean custom name in our database table, use it!
            -- Otherwise, fall back to the raw retail server name.
            local cleanDisplayName = itemData.name or item:GetItemName() or "Unknown Loot"
            nameFS:SetText(cleanDisplayName)
        end
    end)

    -- ============================================================
    -- 🚀 THE SMART SEPARATION MATRIX (FIXES DELVES AND BOSSES)
    -- ============================================================
    local primarySource = "Rare Spawn"
    local secondaryLocation = "World Drop"

    if itemData.category == "delve" then
        -- If it's explicitly a delve, set the Delve Name as the bold header
        primarySource = itemData.bossevent or "Delve Run"
        secondaryLocation = "Delve Reward"
    elseif itemData.bossencounter then
        -- If it's a Dungeon Journal Boss, fetch their live name string
        local encounterName = EJ_GetEncounterInfo(itemData.bossencounter)
        primarySource = encounterName or "Encounter Boss"
        secondaryLocation = itemData.instance or "Dungeon Boss"
    else
        -- Fall back for normal open-world rares
        primarySource = itemData.bossevent or itemData.instance or "Rare Spawn"
        secondaryLocation = "World Drop"
    end

    -- Overwrite the secondary location string if raw data specifically includes an area name
    if itemData.zone and itemData.zone ~= "" then
        -- 🧹 CLEANSE THE DATA ON THE FLY:
        -- Automatically strip out any line breaks, bullet marks, and extra blank margins!
        local cleanZone = itemData.zone:gsub("\n", " "):gsub("•", ""):gsub("%s+", " ")
        -- Strip any trailing or leading empty spaces left over
        cleanZone = cleanZone:match("^%s*(.-)%s*$")
        secondaryLocation = cleanZone
    end

    -- 🌟 6. RIGHT SIDE TEXTS: Sourced Instance / Event (Bolds on top)
    local sourceText = line:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    sourceText:SetFont(STANDARD_TEXT_FONT, 12, "")
    sourceText:SetPoint("TOPRIGHT", line, "TOPRIGHT", -16, -6) 
    sourceText:SetJustifyH("RIGHT")
    sourceText:SetTextColor(0.85, 0.75, 0.45) -- Gold accent header text
    sourceText:SetText(tostring(primarySource))

    -- 🌟 7. RIGHT SIDE TEXTS: Single-Line Cleansed Location Details (Below name)
    local zoneText = line:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    zoneText:SetFont(STANDARD_TEXT_FONT, 10, "")
    zoneText:SetPoint("TOPRIGHT", sourceText, "BOTTOMRIGHT", 0, -2)
    zoneText:SetJustifyH("RIGHT")
    zoneText:SetTextColor(0.50, 0.48, 0.58) -- Muted layout text color
    zoneText:SetText(tostring(secondaryLocation))

    -- 🌟 8. Interaction Clicking Handlers
    line:SetScript("OnClick", function(_, button)
        if button == "LeftButton" then
            dv.UpdateBossPreview(itemData)

        elseif button == "RightButton" then
            if InCombatLockdown() then return end

            local rares = dv.rareEvents and dv.rareEvents[itemData.bossevent]

            if rares and #rares > 0 then
                if TomTom then
                    for _, rare in ipairs(rares) do
                        if rare.x and rare.y then
                            TomTom:AddWaypoint(
                                itemData.mapID,
                                rare.x / 100,
                                rare.y / 100,
                                { title = rare.name }
                            )
                        end
                    end
                else
                    local first = rares[1]
                    if first and first.x and first.y then
                        local waypoint = UiMapPoint.CreateFromCoordinates(
                            itemData.mapID,
                            first.x / 100,
                            first.y / 100
                        )
                        C_Map.SetUserWaypoint(waypoint)
                        C_SuperTrack.SetSuperTrackedUserWaypoint(true)
                    end
                end

                if itemData.mapID then
                    C_Map.OpenWorldMap(itemData.mapID)
                end

                return
            end

            if itemData.mapID then
                C_Map.OpenWorldMap(itemData.mapID)
            end
        end
    end)

    -- Mouse Hover Tooltip Scripts
    line:SetScript("OnEnter", function(self)
        self:SetBackdropColor(0.18, 0.14, 0.26, 0.95)
        self:SetBackdropBorderColor(0.00, 0.80, 1.00, 0.8) 

        GameTooltip:SetOwner(self, "ANCHOR_LEFT")
        GameTooltip:SetHyperlink("item:" .. id)
        GameTooltip:AddLine(" ")
        GameTooltip:AddLine("|cff00ff00<Right Click>|r Set Map Waypoint Pins", 1, 1, 1)
        GameTooltip:Show()
    end)

    line:SetScript("OnLeave", function(self)
        self:SetBackdropColor(0.12, 0.08, 0.18, 0.85)
        self:SetBackdropBorderColor(0.25, 0.20, 0.35, 0.6)
        GameTooltip:Hide()
    end)

    table.insert(dv.activeWidgets, line)
    return y - (rowHeight + 5)
end