--[[
============================================================
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

function dv.CreateProfessionHeader(parent, profession, y, completed, total)
    completed = tonumber(completed) or 0
    total     = tonumber(total) or 0

    dv.collapsedHeaders = dv.collapsedHeaders or {}

    local key = "prof_" .. profession.name

    if dv.collapsedHeaders[key] == nil then
        dv.collapsedHeaders[key] = true
    end

    local collapsed = dv.collapsedHeaders[key]

    if dv.filtersJustChanged then
        local profSel = dv.filters.professions.professions

        if not profSel[profession.name] then
            dv.collapsedHeaders[key] = true
            collapsed = true
        end
    end

    local header = CreateFrame("Button", nil, parent)
    header:SetPoint("TOPLEFT", parent, "TOPLEFT", 0, y)
    header:SetPoint("RIGHT", parent, "RIGHT", -6, 0)
    header:SetHeight(C.VENDOR_HEADER_HEIGHT)

    local bg = header:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints()
    bg:SetTexture("Interface\\Buttons\\WHITE8x8")
    bg:SetGradient("HORIZONTAL",
        CreateColor(0.15, 0.10, 0.25, 0.9),
        CreateColor(0.05, 0.05, 0.15, 0.9)
    )

    header.icon = header:CreateFontString(nil, "OVERLAY")
    header.icon:SetFont(STANDARD_TEXT_FONT, 13,"OUTLINE")
    header.icon:SetPoint("LEFT", 10, 0)
    header.icon:SetText(collapsed and "+" or "-")

    header.text = header:CreateFontString(nil, "OVERLAY")
    header.text:SetFont(STANDARD_TEXT_FONT, 12,"OUTLINE")
    header.text:SetPoint("LEFT", 20, 0)
    header.text:SetText(string.format(profession.name or "Unknown", completed, total))

    header.progress = header:CreateFontString(nil, "OVERLAY")
    header.progress:SetFont(STANDARD_TEXT_FONT, 12,"OUTLINE")
    header.progress:SetPoint("RIGHT", -8, 0)
    header.progress:SetText(string.format("%d/%d learned", completed, total))

    local color
    if total > 0 and completed == total then
        color = CreateColor(0.2, 1, 0.2, 1)
    elseif completed >= total / 2 then
        color = CreateColor(1, 0.82, 0, 1)
    else
        color = CreateColor(0.9, 0.9, 0.9, 1)
    end
    header.progress:SetTextColor(color:GetRGBA())

header:SetScript("OnClick", function()
    dv.ToggleAccordionHeader("professions", key)
end)

    table.insert(dv.activeWidgets, header)
    return collapsed, y - 26
end

-- ✅ Moved outside to be a proper standalone function
function dv.UpdateProfessionPreview(profItem)
    if not profItem then
        return
    end

    dv.contentArea._isVendorPreview = false

    -------------------------------------------------
    -- Hide other preview-only panels
    -------------------------------------------------

    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

    -------------------------------------------------
    -- Get item name first
    -------------------------------------------------

    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

    -------------------------------------------------
    -- Gallery-style decor model preview
    -- IMPORTANT:
    -- Pass profItem as itemData so decorID / uiModelSceneID can be used.
    -------------------------------------------------

    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

    -------------------------------------------------
    -- Profession info card
    -------------------------------------------------

    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

function dv.CreateProfessionLine(parent, profItem, y)
    -- 🔥 determine states (prefer precomputed flags)
    local learned = profItem.__learned
    local collected = profItem.__collected

    -- fallback (safety if flags weren’t set)
    if learned == nil then
        if profItem.spell then
            learned = IsSpellKnown(profItem.spell) or IsPlayerSpell(profItem.spell)
        else
            learned = false
        end
    end

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

    local line = CreateFrame("Button", nil, parent)
    line:SetPoint("TOPLEFT", parent, "TOPLEFT", 10, y)
    line:SetPoint("RIGHT", parent, "RIGHT", -6, 0)
    line:SetHeight(C.LINE_HEIGHT)
    line:RegisterForClicks("AnyUp")
	
	-- 🔥 STATUS ICON
	local icon = line:CreateTexture(nil, "OVERLAY")
	icon:SetSize(14, 14)
	icon:SetPoint("LEFT", 0, 0)

    local nameText = line:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    nameText:SetFont(STANDARD_TEXT_FONT, 13, "OUTLINE")
    nameText:SetPoint("LEFT", icon, "RIGHT", 4, -2)
    nameText:SetJustifyH("LEFT")
    nameText:SetText("• Loading item...")

	-- 🔥 ICON + COLOR LOGIC
	if collected then
    -- 🟢 OWNED
    icon:SetTexture("Interface\\Buttons\\UI-CheckBox-Check")
    icon:SetVertexColor(0.2, 1, 0.2)
    icon:SetAlpha(0.9)

    nameText:SetTextColor(0.2, 1, 0.2)

	elseif learned then
    -- 🟠 LEARNED ONLY
    icon:SetTexture("Interface\\Icons\\INV_Scroll_03")
    icon:SetVertexColor(1, 0.6, 0.1)
    icon:SetAlpha(0.9)

    nameText:SetTextColor(1, 0.5, 0)

	else
    -- 🔴 NOT LEARNED
    icon:SetTexture("Interface\\Buttons\\UI-GroupLoot-Pass-Up")
    icon:SetVertexColor(1, 0.2, 0.2)
    icon:SetAlpha(0.75)

    nameText:SetTextColor(0.95, 0.95, 0.95)
	end

    local itemObj = Item:CreateFromItemID(profItem.id)
    itemObj:ContinueOnItemLoad(function()
        if nameText then
            nameText:SetText(itemObj:GetItemName())
        end
    end)

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

    -- tooltip
line:SetScript("OnEnter", function(self)
    SetCursor("INSPECT_CURSOR")

    GameTooltip:SetOwner(self, "ANCHOR_LEFT")
    GameTooltip:ClearLines()

    local itemName

    if C_Item and C_Item.GetItemNameByID then
        itemName = C_Item.GetItemNameByID(profItem.id)
    end

    if not itemName and GetItemInfo then
        itemName = GetItemInfo(profItem.id)
    end

    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()
        ResetCursor()
        GameTooltip:Hide()
    end)

    table.insert(dv.activeWidgets, line)
    return y - 22
end
