--[[
================================================================================
DragonUI Options Panel - Enhancements Tab
================================================================================
Dark Mode, Range Indicator, Item Quality Borders, Enhanced Tooltips.
================================================================================
]]

local addon = DragonUI
if not addon then return end

local L = addon.L
local LO = addon.LO
local C = addon.PanelControls
local Panel = addon.OptionsPanel

-- ============================================================================
-- HELPERS
-- ============================================================================

local function EnsureModuleTable(moduleName)
    return C:EnsureModuleTable(moduleName)
end

local function GetModuleField(moduleName, field)
    local m = addon.db.profile.modules
    return m and m[moduleName] and m[moduleName][field]
end

local function IsEnabled(moduleName)
    return GetModuleField(moduleName, "enabled") == true
end

-- ============================================================================
-- TAB BUILDER
-- ============================================================================

local function BuildEnhancementsTab(scroll)
    C:AddLabel(scroll, "|cffFFD700" .. LO["Enhancements"] .. "|r", { color = C.Theme.textGold })
    C:AddDescription(scroll, LO["Visual enhancements that add Dragonflight-style polish to the UI. These are optional \226\128\148 disable any you don't want."])
    C:AddSpacer(scroll)

    -- ====================================================================
    -- DARK MODE
    -- ====================================================================
    local darkSection = C:AddSection(scroll, LO["Dark Mode"])

    C:AddDescription(darkSection, LO["Darkens UI borders and chrome only: action bar borders, unit frame borders, minimap border, bag slot borders, micro menu, castbar borders, and decorative elements. Icons, portraits, and abilities are never affected."])

    C:AddToggle(darkSection, {
        label = LO["Enable Dark Mode"],
        desc = LO["Apply darker tinted textures to all UI elements."],
        getFunc = function() return IsEnabled("darkmode") end,
        setFunc = function(val)
            EnsureModuleTable("darkmode").enabled = val
            if val then
                if addon.ApplyDarkMode then addon.ApplyDarkMode() end
            else
                if addon.RestoreDarkMode then addon.RestoreDarkMode() end
            end
            -- Rebuild tab so the intensity dropdown updates its disabled state
            Panel:SelectTab("enhancements")
        end,
        requiresReload = true,
    })

    C:AddDropdown(darkSection, {
        label = LO["Intensity"],
        values = {
            [1] = LO["Light (subtle)"],
            [2] = LO["Medium (balanced)"],
            [3] = LO["Dark (maximum)"],
        },
        getFunc = function()
            return GetModuleField("darkmode", "intensity_preset") or 3
        end,
        setFunc = function(val)
            EnsureModuleTable("darkmode").intensity_preset = val
        end,
        callback = function()
            if addon.RefreshDarkMode then addon.RefreshDarkMode() end
        end,
        disabled = function() return not IsEnabled("darkmode") or GetModuleField("darkmode", "use_custom_color") == true end,
        width = 200,
    })

    C:AddToggle(darkSection, {
        label = LO["Custom Color"],
        desc = LO["Override presets with a custom tint color."],
        getFunc = function() return GetModuleField("darkmode", "use_custom_color") == true end,
        setFunc = function(val)
            EnsureModuleTable("darkmode").use_custom_color = val
            if addon.RefreshDarkMode then addon.RefreshDarkMode() end
            Panel:SelectTab("enhancements")
        end,
        disabled = function() return not IsEnabled("darkmode") end,
        requiresReload = false,
    })

    C:AddColorPicker(darkSection, {
        label = LO["Tint Color"],
        getFunc = function()
            local c = GetModuleField("darkmode", "custom_color")
            if c then return c.r or 0.15, c.g or 0.15, c.b or 0.15 end
            return 0.15, 0.15, 0.15
        end,
        setFunc = function(r, g, b)
            EnsureModuleTable("darkmode").custom_color = { r = r, g = g, b = b }
        end,
        callback = function()
            if addon.RefreshDarkMode then addon.RefreshDarkMode() end
        end,
        hasAlpha = false,
    })

    -- ====================================================================
    -- RANGE INDICATOR
    -- ====================================================================
    C:AddSpacer(scroll)
    local rangeSection = C:AddSection(scroll, LO["Range Indicator"])

    C:AddDescription(rangeSection, LO["Tints action button icons based on range and usability: red = out of range, blue = not enough mana, gray = unusable."])

    C:AddToggle(rangeSection, {
        label = LO["Enable Range Indicator"],
        desc = LO["Color action button icons when target is out of range or ability is unusable."],
        getFunc = function() return IsEnabled("rage_indicator") end,
        setFunc = function(val)
            EnsureModuleTable("rage_indicator").enabled = val
        end,
        callback = function()
            if addon.RefreshRageIndicatorSystem then addon.RefreshRageIndicatorSystem() end
        end,
        requiresReload = false,
    })

    -- ====================================================================
    -- ITEM QUALITY BORDERS
    -- ====================================================================
    C:AddSpacer(scroll)
    local iqSection = C:AddSection(scroll, LO["Item Quality Borders"])

    C:AddDescription(iqSection, LO["Adds quality-colored glow borders to items in your bags, character panel, bank, merchant, and inspect frames: green = uncommon, blue = rare, purple = epic, orange = legendary."])

    C:AddToggle(iqSection, {
        label = LO["Enable Item Quality Borders"],
        desc = LO["Show quality-colored borders on items in bags, character panel, bank, merchant, and inspect frames."],
        getFunc = function() return IsEnabled("itemquality") end,
        setFunc = function(val)
            EnsureModuleTable("itemquality").enabled = val
            if val then
                if addon.ApplyItemQualitySystem then addon.ApplyItemQualitySystem() end
            else
                if addon.RestoreItemQualitySystem then addon.RestoreItemQualitySystem() end
            end
            -- Rebuild tab so the min quality dropdown updates its disabled state
            Panel:SelectTab("enhancements")
        end,
        requiresReload = false,
    })

    C:AddDropdown(iqSection, {
        label = LO["Minimum Quality"],
        values = {
            [0] = "|cff9d9d9d" .. LO["Poor"] .. "|r",
            [1] = "|cffffffff" .. LO["Common"] .. "|r",
            [2] = "|cff1eff00" .. LO["Uncommon"] .. "|r",
            [3] = "|cff0070dd" .. LO["Rare"] .. "|r",
            [4] = "|cffa335ee" .. LO["Epic"] .. "|r",
            [5] = "|cffff8000" .. LO["Legendary"] .. "|r",
        },
        getFunc = function()
            return GetModuleField("itemquality", "min_quality") or 2
        end,
        setFunc = function(val)
            EnsureModuleTable("itemquality").min_quality = val
        end,
        callback = function()
            if addon.UpdateAllQualityBorders then addon.UpdateAllQualityBorders() end
        end,
        disabled = function() return not IsEnabled("itemquality") end,
        width = 200,
    })

    C:AddToggle(iqSection, {
        label = LO["Show Durability Text"],
        desc = LO["Display durability text on equipment slots in the character window."],
        getFunc = function()
            local val = GetModuleField("itemquality", "show_durability")
            return val ~= false
        end,
        setFunc = function(val)
            EnsureModuleTable("itemquality").show_durability = val
        end,
        callback = function()
            if addon.UpdateAllQualityBorders then addon.UpdateAllQualityBorders() end
        end,
        disabled = function() return not IsEnabled("itemquality") end,
        requiresReload = false,
    })

    C:AddToggle(iqSection, {
        label = LO["Use Percentage Text"],
        desc = LO["Show durability as a percentage instead of current/max values."],
        getFunc = function()
            local val = GetModuleField("itemquality", "durability_percent")
            return val ~= false
        end,
        setFunc = function(val)
            EnsureModuleTable("itemquality").durability_percent = val
        end,
        callback = function()
            if addon.UpdateAllQualityBorders then addon.UpdateAllQualityBorders() end
        end,
        disabled = function()
            return not IsEnabled("itemquality") or GetModuleField("itemquality", "show_durability") == false
        end,
        requiresReload = false,
    })

    C:AddToggle(iqSection, {
        label = LO["Show At Full Durability"],
        desc = LO["Keep durability text visible even when an item is fully repaired."],
        getFunc = function()
            return GetModuleField("itemquality", "show_full_durability") == true
        end,
        setFunc = function(val)
            EnsureModuleTable("itemquality").show_full_durability = val
        end,
        callback = function()
            if addon.UpdateAllQualityBorders then addon.UpdateAllQualityBorders() end
        end,
        disabled = function()
            return not IsEnabled("itemquality") or GetModuleField("itemquality", "show_durability") == false
        end,
        requiresReload = false,
    })

    C:AddToggle(iqSection, {
        label = LO["Show Repair Cost"],
        desc = LO["Display total repair cost in the character window."],
        getFunc = function()
            local val = GetModuleField("itemquality", "show_repair_cost")
            return val ~= false
        end,
        setFunc = function(val)
            EnsureModuleTable("itemquality").show_repair_cost = val
        end,
        callback = function()
            if addon.UpdateAllQualityBorders then addon.UpdateAllQualityBorders() end
        end,
        disabled = function() return not IsEnabled("itemquality") end,
        requiresReload = false,
    })

    C:AddDescription(iqSection, LO["Show durability text on equipped gear in the character window and display total repair cost."])

    -- ====================================================================
    -- MERCHANT ENHANCEMENTS
    -- ====================================================================
    C:AddSpacer(scroll)
    local merchantSection = C:AddSection(scroll, LO["Merchant Enhancements"])

    C:AddDescription(merchantSection, LO["Improves the merchant window with automatic repairs and a larger item list layout."])

    C:AddToggle(merchantSection, {
        label = LO["Enable Merchant Enhancements"],
        desc = LO["Expand the merchant item list and enable related quality-of-life improvements."],
        getFunc = function() return IsEnabled("merchantplus") end,
        setFunc = function(val)
            EnsureModuleTable("merchantplus").enabled = val
            if val then
                if addon.RefreshMerchantPlusSystem then addon.RefreshMerchantPlusSystem() end
            else
                if addon.RestoreMerchantPlusSystem then addon.RestoreMerchantPlusSystem() end
            end
            Panel:SelectTab("enhancements")
        end,
        requiresReload = false,
    })

    C:AddToggle(merchantSection, {
        label = LO["Automatic Repairs"],
        desc = LO["Automatically repair gear when visiting a merchant who can repair."],
        getFunc = function()
            local val = GetModuleField("merchantplus", "auto_repair")
            return val ~= false
        end,
        setFunc = function(val)
            EnsureModuleTable("merchantplus").auto_repair = val
        end,
        disabled = function() return not IsEnabled("merchantplus") end,
        requiresReload = false,
    })

    C:AddToggle(merchantSection, {
        label = LO["Prefer Guild Bank Repairs"],
        desc = LO["Use guild bank repair funds first when the merchant allows it."],
        getFunc = function()
            local val = GetModuleField("merchantplus", "use_guild_bank")
            return val ~= false
        end,
        setFunc = function(val)
            EnsureModuleTable("merchantplus").use_guild_bank = val
        end,
        disabled = function()
            return not IsEnabled("merchantplus") or GetModuleField("merchantplus", "auto_repair") == false
        end,
        requiresReload = false,
    })

    C:AddToggle(merchantSection, {
        label = LO["Expanded Merchant Layout"],
        desc = LO["Show up to 20 merchant items per page in a wider multi-column layout."],
        getFunc = function()
            local val = GetModuleField("merchantplus", "expanded_layout")
            return val ~= false
        end,
        setFunc = function(val)
            EnsureModuleTable("merchantplus").expanded_layout = val
        end,
        callback = function()
            if addon.RefreshMerchantPlusSystem then addon.RefreshMerchantPlusSystem() end
        end,
        disabled = function() return not IsEnabled("merchantplus") end,
        requiresReload = false,
    })

    -- ====================================================================
    -- QUICK LOOT
    -- ====================================================================
    C:AddSpacer(scroll)
    local quickLootSection = C:AddSection(scroll, LO["Quick Loot"])

    C:AddDescription(quickLootSection, LO["Move the loot window to your cursor and optionally hide it when there is nothing to loot."])

    C:AddToggle(quickLootSection, {
        label = LO["Enable Quick Loot"],
        desc = LO["Reposition the loot window at your cursor whenever loot opens or updates."],
        getFunc = function() return IsEnabled("quickloot") end,
        setFunc = function(val)
            EnsureModuleTable("quickloot").enabled = val
            if addon.RefreshQuickLootSystem then addon.RefreshQuickLootSystem() end
            Panel:SelectTab("enhancements")
        end,
        requiresReload = false,
    })

    C:AddToggle(quickLootSection, {
        label = LO["Auto-Hide Empty Loot"],
        desc = LO["Hide the loot window automatically if nothing remains to be looted."],
        getFunc = function()
            return GetModuleField("quickloot", "auto_hide") == true
        end,
        setFunc = function(val)
            EnsureModuleTable("quickloot").auto_hide = val
        end,
        disabled = function() return not IsEnabled("quickloot") end,
        requiresReload = false,
    })

    -- ====================================================================
    -- UNIT FRAME LAYERS
    -- ====================================================================
    C:AddSpacer(scroll)
    local uflSection = C:AddSection(scroll, LO["Unit Frame Layers"])

    C:AddDescription(uflSection, LO["Heal prediction bars, absorb shields, and animated health loss overlays on unit frames."])

    C:AddToggle(uflSection, {
        label = LO["Enable Unit Frame Layers"],
        desc = LO["Show heal prediction, absorb shields, and animated health loss on all unit frames."],
        getFunc = function() return IsEnabled("unitframe_layers") end,
        setFunc = function(val)
            EnsureModuleTable("unitframe_layers").enabled = val
        end,
        callback = function()
            Panel:SelectTab("enhancements")
        end,
        requiresReload = true,
    })

    C:AddToggle(uflSection, {
        label = LO["Animated Health Loss"],
        desc = LO["Show animated red health loss bar on player frame when taking damage."],
        getFunc = function()
            local m = addon.db.profile.modules and addon.db.profile.modules.unitframe_layers
            if not m then return true end
            return m.animated_loss ~= false
        end,
        setFunc = function(val)
            if not addon.db.profile.modules.unitframe_layers then
                addon.db.profile.modules.unitframe_layers = {}
            end
            addon.db.profile.modules.unitframe_layers.animated_loss = val
        end,
        disabled = function() return not IsEnabled("unitframe_layers") end,
        requiresReload = true,
    })

    C:AddToggle(uflSection, {
        label = LO["Missing Health Text"],
        desc = LO["Show the health deficit (missing health) as red text on health bars. Useful for healers."],
        getFunc = function()
            local m = addon.db.profile.modules and addon.db.profile.modules.unitframe_layers
            if not m then return false end
            return m.missing_health == true
        end,
        setFunc = function(val)
            if not addon.db.profile.modules.unitframe_layers then
                addon.db.profile.modules.unitframe_layers = {}
            end
            addon.db.profile.modules.unitframe_layers.missing_health = val
            if addon.RefreshUnitFrameLayers then
                addon.RefreshUnitFrameLayers()
            end
        end,
        disabled = function() return not IsEnabled("unitframe_layers") end,
        requiresReload = false,
    })

    -- ====================================================================
    -- ENHANCED TOOLTIPS
    -- ====================================================================
    C:AddSpacer(scroll)
    local ttSection = C:AddSection(scroll, LO["Enhanced Tooltips"])

    C:AddDescription(ttSection, LO["Improves GameTooltip with class-colored borders, class-colored names, target-of-target info, and styled health bars."])

    C:AddToggle(ttSection, {
        label = LO["Enable Enhanced Tooltips"],
        desc = LO["Activate all tooltip improvements. Sub-options below control individual features."],
        getFunc = function() return IsEnabled("tooltip") end,
        setFunc = function(val)
            EnsureModuleTable("tooltip").enabled = val
            -- Rebuild tab so sub-toggles update their disabled state
            Panel:SelectTab("enhancements")
        end,
            requiresReload = true,
    })

    C:AddToggle(ttSection, {
        label = LO["Class-Colored Border"],
        desc = LO["Color the tooltip border by the unit's class (players) or reaction (NPCs)."],
        getFunc = function()
            return GetModuleField("tooltip", "class_colored_border") ~= false
        end,
        setFunc = function(val)
            EnsureModuleTable("tooltip").class_colored_border = val
        end,
        disabled = function() return not IsEnabled("tooltip") end,
        requiresReload = false,
    })

    C:AddToggle(ttSection, {
        label = LO["Class-Colored Name"],
        desc = LO["Color the unit name text in the tooltip by class color (players only)."],
        getFunc = function()
            return GetModuleField("tooltip", "class_colored_name") ~= false
        end,
        setFunc = function(val)
            EnsureModuleTable("tooltip").class_colored_name = val
        end,
        disabled = function() return not IsEnabled("tooltip") end,
        requiresReload = false,
    })

    C:AddToggle(ttSection, {
        label = LO["Target of Target"],
        desc = LO["Add a 'Targeting: <name>' line showing who the unit is targeting."],
        getFunc = function()
            return GetModuleField("tooltip", "target_of_target") ~= false
        end,
        setFunc = function(val)
            EnsureModuleTable("tooltip").target_of_target = val
        end,
        disabled = function() return not IsEnabled("tooltip") end,
        requiresReload = false,
    })

    C:AddToggle(ttSection, {
        label = LO["Styled Health Bar"],
        desc = LO["Restyle the tooltip health bar with class/reaction colors and slimmer look."],
        getFunc = function()
            return GetModuleField("tooltip", "health_bar") ~= false
        end,
        setFunc = function(val)
            EnsureModuleTable("tooltip").health_bar = val
        end,
        disabled = function() return not IsEnabled("tooltip") end,
        requiresReload = false,
    })

    C:AddToggle(ttSection, {
        label = LO["Anchor to Cursor"],
        desc = LO["Make the tooltip follow the cursor position instead of the default anchor."],
        getFunc = function()
            return GetModuleField("tooltip", "anchor_cursor") == true
        end,
        setFunc = function(val)
            EnsureModuleTable("tooltip").anchor_cursor = val
        end,
        disabled = function() return not IsEnabled("tooltip") end,
        requiresReload = false,
    })

    C:AddToggle(ttSection, {
        label = LO["Show Spell IDs"],
        desc = LO["Show spell IDs on spell tooltips, aura tooltips, and linked spell references."],
        getFunc = function()
            return GetModuleField("tooltip", "spell_id") ~= false
        end,
        setFunc = function(val)
            EnsureModuleTable("tooltip").spell_id = val
        end,
        disabled = function() return not IsEnabled("tooltip") end,
        requiresReload = false,
    })
end

-- Register the tab (order 11 = after Quest Tracker, before Profiles)
Panel:RegisterTab("enhancements", LO["Enhancements"], BuildEnhancementsTab, 11)
