--[[
============================================================
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 = ...
-------------------------------------------------
-- 🔹 Helpers
-------------------------------------------------

function dv.ItemPassesRequirements(itemID)
    local data = dv.decorItem and dv.decorItem[itemID]
    if not data then return true end

    if selectedExpansions and next(selectedExpansions) then
        if data.expansion and not selectedExpansions[data.expansion] then
            return false
        end
    end
    if selectedExpansionz and next(selectedExpansionz) then
        if data.expansion and not selectedExpansionz[data.expansion] then
            return false
        end
    end
    if selectedFactions and next(selectedFactions) then
        if data.faction and not selectedFactions[data.faction] then
            return false
        end
    end

    return true
end

dv.collapsedFilters = dv.collapsedFilters or {
    expansions = false,
    faction = false,
    categories = false,
    requirements = false,
    status = false,
}
-------------------------------------------------
-- 🔹 Filters
-------------------------------------------------
function dv.BuildVendorFilters()
	local parent = dv.sidebarFilters
	local filters = dv.filters.vendors
	local selectedExpansions = filters.expansions
	local selectedFactions   = filters.factions
	local selectedCategories = filters.categories

    -- wipe old
    for _, child in ipairs({ parent:GetChildren() }) do
        child:Hide()
        child:SetParent(nil)
    end

    local y = -6
    local HEADER_X = 12
    local CHECKBOX_X = 20
    local SPACING = 20

    local function Header(text)
        local fs = parent:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        fs:SetPoint("TOPLEFT", HEADER_X, y)
		fs:SetFont(STANDARD_TEXT_FONT, 15, "OUTLINE")
        fs:SetTextColor(1, 0.82, 0)
        fs:SetText(text)
        y = y - 20
    end

    local function Checkbox(label, tbl, key)
        tbl[key] = tbl[key] or false
        local cb = CreateFrame("CheckButton", nil, parent, "UICheckButtonTemplate")
		cb.text:SetFont(STANDARD_TEXT_FONT, 12, "OUTLINE")
		cb.text:SetTextColor(1, 1, 1, 1)
        cb:SetPoint("TOPLEFT", CHECKBOX_X, y)
        cb.Text:SetText(label)
		
        cb:SetChecked(tbl[key])
        cb:SetScript("OnClick", function(self)
            tbl[key] = self:GetChecked()
			dv.filtersJustChanged = true
            BuildVendorUI()
        end)
        y = y - SPACING
    end

-- EXPANSIONS
Header("Expansions")
local seen = {}

-- discover which expansions are actually used
for _, vendor in ipairs(dv.npcs or {}) do
    local exp = vendor.expansion
    if exp then
        seen[exp] = true
    end
end

-- render in defined expansion order
for _, exp in ipairs(dv.EXPANSION_ORDER) do
    if seen[exp] then
        Checkbox(exp, selectedExpansions, exp)
    end
end

    y = y - 10

    -- FACTION
    Header("Faction")

    for _, f in ipairs({ "alliance", "horde", "neutral" }) do
        Checkbox(f, selectedFactions, f)
    end
	
-- =========================
-- CATEGORIES (NEW)
-- =========================
y = y - 10

Header("Requirements")

local categoryList = {
    -- 🔥 progression types (NEW but still treated as "categories")
    { key = "none", label = "No Requirement" },
    { key = "reputation", label = "Reputation Vendors" },
    { key = "renown", label = "Renown Vendors" },
    { key = "subfaction", label = "Subfaction Vendors" },
    { key = "friendship", label = "Friendship Vendors" },

    -- 🔥 your existing ones
    { key = "race", label = "Race Locked" },
    { key = "neighborhood", label = "Neighborhood / Housing" },
    { key = "Promo", label = "Promo Vendors" },
}


for _, cat in ipairs(categoryList) do
    Checkbox(cat.label, selectedCategories, cat.key)
end	

-- =========================
-- STATUS KEY (NEW)
-- =========================
y = y - 20

Header("Status Key")

local keyText = parent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
keyText:SetPoint("TOPLEFT", CHECKBOX_X, y)
keyText:SetJustifyH("LEFT")
keyText:SetWordWrap(true)
keyText:SetFont(STANDARD_TEXT_FONT, 12, "")
keyText:SetText(
    "|TInterface\\Buttons\\UI-CheckBox-Check:16|t  |cff9d9d9dFound Vendor|r\n" ..
	"|TInterface\\Buttons\\UI-GroupLoot-Pass-Up:16|t |cffffffffNot found|r\n" ..
    "|TInterface\\AddOns\\DecorVendor\\Assets\\red:16|t  |cffff2020Horde|r\n" ..
    "|TInterface\\AddOns\\DecorVendor\\Assets\\blue:16|t  |cff4faaffAlliance|r\n" ..
    "|TInterface\\AddOns\\DecorVendor\\Assets\\neutral:16|t  |cff00ff00Neutral|r\n\n" ..

    "|cffaaaaaaBased on character discovery data|r"
)

-- auto spacing
local width = parent:GetWidth() - 30
keyText:SetWidth(width)
y = y - keyText:GetStringHeight() - 10
end

function dv.BuildQuestFilters()
    local parent = dv.sidebarFilters
	local filters = dv.filters.quests
	local selectedExpansionz = filters.expansions
	local selectedFactionz   = filters.factions
	local selectedCategories = filters.categories

    for _, child in ipairs({ parent:GetChildren() }) do
        child:Hide()
        child:SetParent(nil)
    end

    local y = -6
    local HEADER_X = 12
    local CHECKBOX_X = 20
    local SPACING = 20

    local function Header(text)
        local fs = parent:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        fs:SetPoint("TOPLEFT", HEADER_X, y)
		fs:SetFont(STANDARD_TEXT_FONT, 15, "OUTLINE")
        fs:SetTextColor(1, 0.82, 0)
        fs:SetText(text)
        y = y - 20
    end

    local function Checkbox(label, tbl, key)
        tbl[key] = tbl[key] or false
        local cb = CreateFrame("CheckButton", nil, parent, "UICheckButtonTemplate")
		cb.text:SetFont(STANDARD_TEXT_FONT, 12, "OUTLINE")
		cb.text:SetTextColor(1, 1, 1, 1)
        cb:SetPoint("TOPLEFT", CHECKBOX_X, y)
        cb.Text:SetText(label)
        cb:SetChecked(tbl[key])
        cb:SetScript("OnClick", function(self)
            tbl[key] = self:GetChecked()
			dv.filtersJustChanged = true
            BuildVendorUI()
        end)
        y = y - SPACING
    end
	
-- EXPANSIONS
Header("Expansions")
local seen = {}

-- discover which expansions are actually used (NEW STRUCTURE)
for _, group in ipairs(dv.quests or {}) do
    for _, quest in ipairs(group.quests or {}) do
        if quest.expansion then
            seen[quest.expansion] = true
        end
    end
end

-- render in defined expansion order
for _, exp in ipairs(dv.EXPANSION_ORDER or {}) do
    if seen[exp] then
        Checkbox(exp, selectedExpansionz, exp)
    end
end
	

    y = y - 10

    -- FACTIONS USED IN QUESTS
    Header("Faction")
    selectedFactionz = selectedFactionz or {}
    local found = {}

for _, group in ipairs(dv.quests or {}) do
    for _, quest in ipairs(group.quests or {}) do
        if quest.faction then
            found[string.lower(quest.faction)] = true
        end
    end
end


    for f,_ in pairs(found) do
        Checkbox(f, selectedFactionz, f)
    end

-- =========================
-- CATEGORIES (NEW)
-- =========================
y = y - 10

Header("Categories")

local categoryList = {
    { key = "race", label = "Race Locked" },
    { key = "spec", label = "Spec Locked" },
}

for _, cat in ipairs(categoryList) do
    Checkbox(cat.label, selectedCategories, cat.key)
end
	
-- =========================
-- STATUS KEY (NEW)
-- =========================
y = y - 20

Header("Status Key")

local keyText = parent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
keyText:SetPoint("TOPLEFT", CHECKBOX_X, y)
keyText:SetJustifyH("LEFT")
keyText:SetWordWrap(true)
keyText:SetFont(STANDARD_TEXT_FONT, 12, "")

keyText:SetText(
    "|TInterface\\Buttons\\UI-CheckBox-Check:16|t  |cff9d9d9dCompleted|r\n" ..
    "|TInterface\\Buttons\\UI-GroupLoot-Pass-Up:16|t  |cffffffffNot Completed|r\n" ..
    "|TInterface\\AddOns\\DecorVendor\\Assets\\red:16|t  |cffff2020Horde|r\n" ..
    "|TInterface\\AddOns\\DecorVendor\\Assets\\blue:16|t  |cff4faaffAlliance|r\n" ..
    "|TInterface\\AddOns\\DecorVendor\\Assets\\neutral:16|t  |cff00ff00Neutral|r\n\n" ..
    "|cffaaaaaaBased on Warband-wide collection data|r"
)

-- auto spacing
local width = parent:GetWidth() - 30
keyText:SetWidth(width)
y = y - keyText:GetStringHeight() - 10	
end

function dv.BuildProfessionFilters()
    local parent = dv.sidebarFilters
	local filters = dv.filters.professions
	local selectedProfessions = filters.professions
	local selectedProfessionExpansions  = filters.expansions


    for _, child in ipairs({ parent:GetChildren() }) do
        child:Hide()
        child:SetParent(nil)
    end

    local y = -6
    local HEADER_X = 12
    local CHECKBOX_X = 20
    local SPACING = 20

    local function Header(text)
        local fs = parent:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        fs:SetPoint("TOPLEFT", HEADER_X, y)
		fs:SetFont(STANDARD_TEXT_FONT, 15, "OUTLINE")
        fs:SetTextColor(1, 0.82, 0)
        fs:SetText(text)
        y = y - 20
    end

    local function Checkbox(label, tbl, key)
        tbl[key] = tbl[key] or false
        local cb = CreateFrame("CheckButton", nil, parent, "UICheckButtonTemplate")
		cb.text:SetFont(STANDARD_TEXT_FONT, 12, "OUTLINE")
		cb.text:SetTextColor(1, 1, 1, 1)
        cb:SetPoint("TOPLEFT", CHECKBOX_X, y)
        cb.Text:SetText(label)
        cb:SetChecked(tbl[key])
        cb:SetScript("OnClick", function(self)
            tbl[key] = self:GetChecked()
			dv.filtersJustChanged = true
            BuildVendorUI()
        end)
        y = y - SPACING
    end
	
    y = y - 10
	
-- =========================
-- EXPANSIONS (NEW)
-- =========================
    Header("Expansions")

    local seen = {}

    -- 🔥 collect expansions from items
    for _, group in ipairs(dv.professions or {}) do
        for _, item in ipairs(group.items or {}) do
            if item.expansion then
                seen[item.expansion] = true
            end
        end
    end

    -- 🔥 render in your global expansion order
    for _, exp in ipairs(dv.EXPANSION_ORDER or {}) do
        if seen[exp] then
            Checkbox(exp, selectedProfessionExpansions, exp)
        end
    end	

    y = y - 10
	
    -- CATEGORIES
    Header("Professions")

local seenCategories = {}

for _, group in ipairs(dv.professions or {}) do
    for _, item in ipairs(group.items or {}) do
        if item.category then
            seenCategories[item.category] = true
        end
    end
end

for cat, _ in pairs(seenCategories) do
    Checkbox(cat, selectedProfessions, cat)
end

-- =========================
-- STATUS KEY (NEW)
-- =========================
y = y - 20

Header("Status Key")

local keyText = parent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
keyText:SetPoint("TOPLEFT", CHECKBOX_X, y)
keyText:SetJustifyH("LEFT")
keyText:SetWordWrap(true)
keyText:SetFont(STANDARD_TEXT_FONT, 12, "")

keyText:SetText(
    "|TInterface\\Buttons\\UI-GroupLoot-Pass-Up:16|t |cffffffffNo Progress|r\n" ..
	"|TInterface\\Icons\\INV_Scroll_03:16|t |cffff9900Learned (Recipe)|r\n" ..
	"|TInterface\\Buttons\\UI-CheckBox-Check:16|t |cff00ff00Collected|r\n\n" ..
    "|cffaaaaaa• Learned recipes = Current character\n• Collected decor = Warband-wide|r"
)

-- auto spacing
local width = parent:GetWidth() - 30
keyText:SetWidth(width)
y = y - keyText:GetStringHeight() - 10

end

function dv.BuildAchievementFilters()
    local parent = dv.sidebarFilters
	local filters = dv.filters.achievements
	local selectedAchievements = filters.groups
	local selectedFactionz   = filters.factions



    for _, child in ipairs({ parent:GetChildren() }) do
        child:Hide()
        child:SetParent(nil)
    end

    local y = -6
    local HEADER_X = 12
    local CHECKBOX_X = 20
    local SPACING = 20

    local function Header(text)
        local fs = parent:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        fs:SetPoint("TOPLEFT", HEADER_X, y)
		fs:SetFont(STANDARD_TEXT_FONT, 15, "OUTLINE")
        fs:SetTextColor(1, 0.82, 0)
        fs:SetText(text)
        y = y - 20
    end

    local function Checkbox(label, tbl, key)
        tbl[key] = tbl[key] or false
        local cb = CreateFrame("CheckButton", nil, parent, "UICheckButtonTemplate")
		cb.text:SetFont(STANDARD_TEXT_FONT, 12, "OUTLINE")
		cb.text:SetTextColor(1, 1, 1, 1)		
        cb:SetPoint("TOPLEFT", CHECKBOX_X, y)
        cb.Text:SetText(label)
        cb:SetChecked(tbl[key])
        cb:SetScript("OnClick", function(self)
            tbl[key] = self:GetChecked()
			dv.filtersJustChanged = true
            BuildVendorUI()
        end)
        y = y - SPACING
    end

    -- CATEGORIES
    Header("Categories")

    for _, group in ipairs(dv.achievements or {}) do
        Checkbox(group.name, selectedAchievements, group.name)
    end

    y = y - 10

    -- FACTION
    Header("Faction")
    local found = {}

for _, group in ipairs(dv.achievements or {}) do
    for _, achieve in ipairs(group.achievements or {}) do
        if achieve.faction then
            found[string.lower(achieve.faction)] = true
        end
    end
end


    for f,_ in pairs(found) do
        Checkbox(f, selectedFactionz, f)
    end
	

-- =========================
-- STATUS KEY (NEW)
-- =========================
y = y - 20

Header("Status Key")

local keyText = parent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
keyText:SetPoint("TOPLEFT", CHECKBOX_X, y)
keyText:SetJustifyH("LEFT")
keyText:SetWordWrap(true)
keyText:SetFont(STANDARD_TEXT_FONT, 12, "")

keyText:SetText(
    "|TInterface\\Buttons\\UI-CheckBox-Check:16|t  |cff9d9d9dCompleted|r\n" ..
    "|TInterface\\Buttons\\UI-GroupLoot-Pass-Up:16|t  |cffffffffNot Completed|r\n" ..
    "|TInterface\\AddOns\\DecorVendor\\Assets\\red:16|t  |cffff2020Horde|r\n" ..
    "|TInterface\\AddOns\\DecorVendor\\Assets\\blue:16|t  |cff4faaffAlliance|r\n" ..
    "|TInterface\\AddOns\\DecorVendor\\Assets\\neutral:16|t  |cff00ff00Neutral|r\n\n" ..

    "|cffaaaaaaBased on Warband-wide collection data|r"
)

-- auto spacing
local width = parent:GetWidth() - 30
keyText:SetWidth(width)
y = y - keyText:GetStringHeight() - 10
end

function dv.BuildBossDropFilters()
    local parent = dv.sidebarFilters
	local filters = dv.filters.bossdrops
	local selectedBossExpansions = filters.expansions
	local selectedCategories = filters.categories


    -- wipe sidebar
    for _, child in ipairs({ parent:GetChildren() }) do
        child:Hide()
        child:SetParent(nil)
    end

    local y = -6
    local HEADER_X = 12
    local CHECKBOX_X = 20
    local SPACING = 20

    -- Header builder
    local function Header(text)
        local fs = parent:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        fs:SetPoint("TOPLEFT", HEADER_X, y)
		fs:SetFont(STANDARD_TEXT_FONT, 15, "OUTLINE")
        fs:SetTextColor(1, 0.82, 0)
        fs:SetText(text)
        y = y - 20
    end

    -- Checkbox builder
    local function Checkbox(label, tbl, key)
        tbl[key] = tbl[key] or false
        local cb = CreateFrame("CheckButton", nil, parent, "UICheckButtonTemplate")
		cb.text:SetFont(STANDARD_TEXT_FONT, 12, "OUTLINE")
		cb.text:SetTextColor(1, 1, 1, 1)
        cb:SetPoint("TOPLEFT", CHECKBOX_X, y)
        cb.Text:SetText(label)
        cb:SetChecked(tbl[key])

        cb:SetScript("OnClick", function(self)
            tbl[key] = self:GetChecked()
            dv.filtersJustChanged = true
            BuildVendorUI() -- refresh
        end)

        y = y - SPACING
    end

    ---------------------------------------------------------
    -- EXPANSIONS (ONLY FILTER WE USE)
    ---------------------------------------------------------
Header("Expansions")

local seenExp = {}

-- discover which expansions are used by boss drops
for _, group in ipairs(dv.bossdrops or {}) do
    if group.expansion then
        seenExp[group.expansion] = true
    end
end

-- render in defined expansion order
for _, exp in ipairs(dv.EXPANSION_ORDER or {}) do
    if seenExp[exp] then
        Checkbox(exp, selectedBossExpansions, exp)
    end
end

-- =========================
-- CATEGORIES (NEW)
-- =========================
y = y - 10

Header("Categories")

local categoryList = {
    { key = "rare", label = "Rares" },
    { key = "dungeon", label = "Dungeons" },
	{ key = "delve", label = "Delves" },
	{ key = "renown", label = "Renown" },
	{ key = "event", label = "Events" },
	{ key = "raid", label = "Raids" },
	{ key = "daily", label = "Dailies" },
	{ key = "decor", label = "Decor Specialties" },
}

for _, cat in ipairs(categoryList) do
    Checkbox(cat.label, selectedCategories, cat.key)
end
	-- =========================
-- STATUS KEY (NEW)
-- =========================
y = y - 20

Header("Status Key")

local keyText = parent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
keyText:SetPoint("TOPLEFT", CHECKBOX_X, y)
keyText:SetJustifyH("LEFT")
keyText:SetWordWrap(true)
keyText:SetFont(STANDARD_TEXT_FONT, 12, "")
keyText:SetText(
    "|TInterface\\Buttons\\UI-CheckBox-Check:14|t |cff00ff00Collected|r\n" ..
    "|TInterface\\Buttons\\UI-GroupLoot-Pass-Up:14|t |cffff4040Not Collected|r\n\n" ..
    "|cffaaaaaaBased on Warband-wide collection data|r"
)

-- auto spacing
local width = parent:GetWidth() - 30
keyText:SetWidth(width)
y = y - keyText:GetStringHeight() - 10
end

function dv.ResetSidebarFilters()
    if dv.sidebarFilters then
        dv.sidebarFilters:Hide()
        dv.sidebarFilters:SetParent(nil)
    end

    -- Create a brand-new container
    dv.sidebarFilters = CreateFrame("Frame", "DV_sidebarFilters", dv.sidebar)
    dv.sidebarFilters:SetPoint("TOPLEFT", dv.sidebar, "TOPLEFT", 0, 0)
	dv.sidebarFilters:SetPoint("BOTTOMRIGHT", dv.sidebar, "BOTTOMRIGHT", 0, 40)
end