local GC = select(2, ...)

local Config = GC:NewModule("Config", "AceEvent-3.0")
GC.Config = Config

local LibStub = LibStub
local AceConfigRegistry = LibStub("AceConfigRegistry-3.0")
local AceConfigDialog = LibStub("AceConfigDialog-3.0")
local LSM = LibStub:GetLibrary("LibSharedMedia-3.0")

local tostring, tonumber = tostring, tonumber

local options = {
    type = "group",
    name = " ",
    args = {
        general = {
            type = "group",
            name = " ",
            inline = true,
            get = function(info) return GC.db.profile[info[#info]] end,
            set = function(info, value)
                GC.db.profile[info[#info]] = value;
                GC.UI.RefreshStatusText()
            end,
            args = {
                logo = {
                    order = 1,
                    type = "description",
                    name = " ",
                    image = "Interface\\AddOns\\GallywixCoins\\Media\\logo.tga",
                    imageWidth = 140,
                    imageHeight = 140,
                    width = "full",
                },
                title = {
                    order = 2,
                    type = "description",
                    name = "|cffffd100GallywixCoins|r   |cffaaaaaaVersion: " .. GC.CONST.METADATA.VERSION .. "|r\nTrack your wealth like a Trade Prince. Time is money, friend!",
                    fontSize = "large",
                    width = "full",
                },
                header_prices = {
                    order = 10,
                    type = "header",
                    name = "Valuation Settings",
                },
                source = {
                    type = "select",
                    order = 11,
                    name = "Price Source",
                    desc = "Choose which addon to use for item values calculation.",
                    width = "double",
                    values = function() return GC.availablePriceSources or {} end,
                    get = function(info) return GC.db.profile.pricesource[info[#info]] end,
                    set = function(info, value)
                        GC.db.profile.pricesource[info[#info]] = value;
                        GC:Print("Price source changed to: " .. value)
                        GC.UI.RefreshStatusText()
                    end,
                    disabled = function(info) return not Config.SettingsChangeAllowed(info[#info]) end
                },
                hint_sources = {
                    order = 12,
                    type = "description",
                    name = "|cffaaaaaaNote: You need an addon like Auctionator or TradeSkillMaster installed to provide price data.|r",
                    width = "full",
                },
                ignoreGrayItems = {
                    type = "toggle",
                    order = 13,
                    name = "Ignore Gray Items",
                    desc = "Do not track or alert for poor quality (gray) items.",
                    width = "full",
                    get = function(info) return GC.db.profile.general[info[#info]] end,
                    set = function(info, value)
                        GC.db.profile.general[info[#info]] = value
                        GC:Print("Ignore Gray Items: " .. Config.FormatBoolean(value))
                    end
                },
                header_alerts = {
                    order = 20,
                    type = "header",
                    name = "Wealth Alerts",
                },
                goldAlertThreshold = {
                    type = "input",
                    order = 21,
                    name = "Gold Alert Threshold",
                    desc = "Minimum gold value to trigger a notification.",
                    width = "double",
                    get = function(info) return tostring(GC.db.profile.notification[info[#info]] or 1000) end,
                    set = function(info, value) 
                        GC.db.profile.notification[info[#info]] = tonumber(value) or 1000 
                        GC:Print("Alert threshold set to: " .. (tonumber(value) or 1000) .. "g")
                    end,
                    pattern = "^[0-9]+$"
                },
                enableToasts = {
                    type = "toggle",
                    order = 22,
                    name = "Enable Visual Toasts",
                    desc = "Show a stylized notification when a valuable item is looted.",
                    width = "full",
                    get = function(info) return GC.db.profile.notification[info[#info]] end,
                    set = function(info, value)
                        GC.db.profile.notification[info[#info]] = value
                        GC:Print("Visual Toasts: " .. Config.FormatBoolean(value))
                    end
                },
                playSoundEnabled = {
                    type = "toggle",
                    order = 23,
                    name = "Enable Epic Sound",
                    desc = "Play a satisfying sound alert for precious loot.",
                    width = "full",
                    get = function(info) return GC.db.profile.notification[info[#info]] end,
                    set = function(info, value)
                        GC.db.profile.notification[info[#info]] = value
                        GC:Print("Sound Alerts: " .. Config.FormatBoolean(value))
                    end
                },
                soundChannel = {
                    type = "select",
                    order = 24,
                    name = "Sound Channel",
                    desc = "Choose which game sound channel to use for alerts.",
                    width = "double",
                    values = {
                        ["Master"] = "Master",
                        ["SFX"] = "Sound Effects (SFX)",
                        ["Ambience"] = "Ambience",
                        ["Music"] = "Music",
                        ["Dialog"] = "Dialog"
                    },
                    get = function(info) return GC.db.profile.notification[info[#info]] or "Master" end,
                    set = function(info, value)
                        GC.db.profile.notification[info[#info]] = value
                        GC:Print("Sound Channel set to: " .. value)
                    end
                },
                header_appearance = {
                    order = 30,
                    type = "header",
                    name = "Appearance",
                },
                fontSize = {
                    type = "range",
                    order = 31,
                    name = "Font Size",
                    desc = "Adjust the text size for the main UI and pills.",
                    min = 8, max = 24, step = 1,
                    get = function(info) return GC.db.profile.display[info[#info]] or 12 end,
                    set = function(info, value)
                        GC.db.profile.display[info[#info]] = value
                        GC.UI.ApplyFontSize()
                    end
                },
            }
        }
    }
}

function Config:OnEnable()
    GC.PriceSources.Prepare() -- Ensure sources are ready before registering options
    LSM:Register("sound", "-- No Sound", 0)
    LSM:Register("sound", "Auction Window Open", 567482)
    LSM:Register("sound", "Auction Window Close", 567499)
    LSM:Register("sound", "Auto Quest Complete", 567476)
    LSM:Register("sound", "Level Up", 567431)
    LSM:Register("sound", "Player Invite", 567451)
    LSM:Register("sound", "Raid Warning", 567397)

    -- Register with AceConfig
    AceConfigRegistry:RegisterOptionsTable(GC.CONST.METADATA.NAME, options)
    GC.settingsCategoryID = AceConfigDialog:AddToBlizOptions(GC.CONST.METADATA.NAME, GC.CONST.METADATA.NAME)
end

function Config.FormatBoolean(value)
    if value == true then return "|cff00ff00Enabled|r" end
    return "|cffff0000Disabled|r"
end

function Config.SettingsChangeAllowed(key)
    return not GC.Session.IsRunning()
end

