--[[
============================================================
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 ProfessionRows:|r constants are missing.")
    return
end

-- ============================================================
-- 🛡️ COMPATIBILITY GHOST HEADER (PERFECTLY STABLE FEED)
-- ============================================================
function dv.CreateProfessionHeader(parent, profession, y, completed, total)
    local key = "prof_" .. profession.name
    dv.collapsedHeaders = dv.collapsedHeaders or {}
    if dv.collapsedHeaders[key] == nil then
        dv.collapsedHeaders[key] = false
    end
    return false, y
end

-- ✅ Standing standalone preview engine function
function dv.UpdateProfessionPreview(profItem)
    if not profItem then
        return
    end

    dv.contentArea._isVendorPreview = false

    if dv.vendorPopup then dv.vendorPopup:Hide() end
    if dv.vendorNotes then dv.vendorNotes:Hide() end
    if dv.bossNotes then dv.bossNotes:Hide() end
    if dv.questNotes then dv.questNotes:Hide() end
    if dv.achievementPanel then dv.achievementPanel:Hide() end
    if dv.achievementWowheadWrapper then dv.achievementWowheadWrapper:Hide() end
    if dv.questWowheadWrapper then dv.questWowheadWrapper:Hide() end

    local itemName = "Preview"

    if profItem.id then
        local item = Item:CreateFromItemID(profItem.id)

        item:ContinueOnItemLoad(function()
            itemName = item:GetItemName() or "Preview"

            if dv.modelTitle then
                dv.modelTitle:SetText(itemName)
                dv.modelTitle:SetTextColor(1, 1, 1)
            end
        end)
    end

    if profItem.model3D or profItem.decorID then
        if dv.ShowPreviewModel then
            dv.ShowPreviewModel(profItem.model3D, itemName, profItem)
        end
    else
        if dv.SetPreviewWatermarkVisible then
            dv.SetPreviewWatermarkVisible(true)
        end
    end

    if not dv.profNotes then
        dv.profNotes = CreateFrame("Frame", nil, dv.itemContainer)
        dv.profNotes:SetPoint("BOTTOMLEFT", dv.itemContainer, "BOTTOMLEFT", 10, 18)
        dv.profNotes:SetPoint("BOTTOMRIGHT", dv.itemContainer, "BOTTOMRIGHT", -10, 18)
        dv.profNotes:SetHeight(44)

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

        dv.profNotes.text = dv.profNotes:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        dv.profNotes.text:SetPoint("TOPLEFT", dv.profNotes, "TOPLEFT", 10, -8)
        dv.profNotes.text:SetPoint("BOTTOMRIGHT", dv.profNotes, "BOTTOMRIGHT", -10, 8)
        dv.profNotes.text:SetFont(STANDARD_TEXT_FONT, 12, "")
        dv.profNotes.text:SetJustifyH("LEFT")
        dv.profNotes.text:SetJustifyV("TOP")
    end

    local profInfo = ""

    if profItem.skill then
        profInfo = profInfo .. "|cffFFD200Profession:|r " .. profItem.skill .. "\n"
    end

    if profItem.skillNeeded then
        profInfo = profInfo .. "|cff00fbffSkill Required:|r " .. profItem.skillNeeded
    end

    if profItem.note and profItem.note ~= "" then
        profInfo = profInfo .. "\n|cff00ff00Note:|r " .. profItem.note
    end

    if profInfo ~= "" then
        dv.profNotes.text:SetText(profInfo)
        dv.profNotes:Show()
    else
        dv.profNotes:Hide()
    end
end

-- ============================================================
-- 🛠️ PREMIUM WIDE CARD ACTIVITY FEED ROW LAYOUT
-- ============================================================
function dv.CreateProfessionLine(parent, profItem, y)
    local learned = profItem.__learned
    local collected = profItem.__collected

    if learned == nil then
        learned = profItem.spell and (IsSpellKnown(profItem.spell) or IsPlayerSpell(profItem.spell)) or false
    end

    if collected == nil then
        collected = (profItem.id and dv.IsItemCollected) and dv.IsItemCollected(profItem.id) or false
    end

    -- 🌟 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 Sleek 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)       -- Deep purple midnight theme tint
    line:SetBackdropBorderColor(0.25, 0.20, 0.35, 0.6) -- Muted border edge accents

    -- Hover Highlight Scripts
    line:SetScript("OnEnter", function(self)
        SetCursor("INSPECT_CURSOR")
        self:SetBackdropColor(0.18, 0.14, 0.26, 0.95)
        self:SetBackdropBorderColor(0.00, 0.80, 1.00, 0.8) -- Sleek cyan focus ring highlight

        -- Tooltip Callouts
        GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
        GameTooltip:ClearLines()
        local itemName = C_Item and C_Item.GetItemNameByID and C_Item.GetItemNameByID(profItem.id) or GetItemInfo(profItem.id)
        GameTooltip:AddLine(itemName or ("Item ID: " .. tostring(profItem.id)), 1, 1, 1)
        GameTooltip:AddLine(" ")
        if collected then
            GameTooltip:AddLine("|cff00ff00Owned Decor|r")
        elseif learned then
            GameTooltip:AddLine("|cffff9900Recipe Learned|r")
        else
            GameTooltip:AddLine("|cffaaaaaaNot Learned|r")
        end
        GameTooltip:AddLine("|cff00ff00<Left Click>|r View Decor & Reagents", 1, 1, 1)
        GameTooltip:Show()
    end)

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

    -- 🌟 3. LEFT EDGE: Color Accent Indicator Bars (Top = State, Bottom = Sourced)
    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")

    -- Assign color code values to the edge strips based on status values
    if collected then
        topIndicator:SetVertexColor(0.2, 1.0, 0.5, 1)    -- Bright emerald owned green
        bottomIndicator:SetVertexColor(0.2, 1.0, 0.5, 1)
    elseif learned then
        topIndicator:SetVertexColor(1.0, 0.6, 0.1, 1)    -- Learned recipe orange strip
        bottomIndicator:SetVertexColor(0.5, 0.5, 0.5, 1) -- Grey backdrop fallback
    else
        topIndicator:SetVertexColor(1.0, 0.2, 0.2, 1)    -- Missing recipe crimson alert red
        bottomIndicator:SetVertexColor(0.3, 0.3, 0.3, 1)
    end

    -- 🌟 4. Main Item Icon Texture Graphic
    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)
    icon:SetTexture("Interface\\Icons\\INV_Misc_QuestionMark")

    -- 🌟 5. Quality Glow Background Frame Rim Insets
    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)

    -- 🌟 6. Main Typography Item Label Text String Frame
    local nameText = line:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    nameText:SetFont(STANDARD_TEXT_FONT, 13, "")
    nameText:SetPoint("LEFT", icon, "RIGHT", 12, 4)
    nameText:SetWidth(180)
    nameText:SetJustifyH("LEFT")
    nameText:SetTextColor(0.92, 0.90, 0.96)
    nameText:SetText("Loading item data...")

    -- 🌟 7. Secondary Description Text Frame (Subtext sitting below name)
    local subText = line:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    subText:SetFont(STANDARD_TEXT_FONT, 11, "")
    subText:SetPoint("TOPLEFT", nameText, "BOTTOMLEFT", 0, -2)
    subText:SetWidth(180)
    subText:SetJustifyH("LEFT")
    
    if collected then
        subText:SetText("|cff30d580Collected & Ready|r")
    elseif learned then
        subText:SetText("|cffff9900Recipe Learned|r")
    else
        subText:SetText("|cffaaaaaaRecipe Unknown|r")
    end

    -- 🌟 8. Far Right Status Notification String Callout
    local statusText = line:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    statusText:SetPoint("RIGHT", line, "RIGHT", -16, 0)
    statusText:SetJustifyH("RIGHT")
    
    if collected then
        statusText:SetText("|cff30d580OWNED|r")
    elseif learned then
        statusText:SetText("|cffff9900CRAFTABLE|r")
    else
        statusText:SetText("|cffff4040LOCKED|r")
    end

    -- Call database handle update functions sequentially
    local itemObj = Item:CreateFromItemID(profItem.id)
    itemObj:ContinueOnItemLoad(function()
        if nameText then
            nameText:SetText(itemObj:GetItemName())
        end
        if icon then
            local texture = itemObj:GetItemIcon()
            if texture then icon:SetTexture(texture) end
        end
    end)

    -- Click Actions
    line:SetScript("OnClick", function(self, button)
        if button == "LeftButton" then
            dv.UpdateProfessionPreview(profItem)
            dv.ShowReagentsPopup(profItem)
        end
    end)

    table.insert(dv.activeWidgets, line)
    -- Shift the next item button downward by exactly 49px (44px row height + 5px cell padding spacing)
    return y - (rowHeight + 5)
end