local MythicMateNS = _G.MythicMate
if not MythicMateNS then return end

MythicMateNS.Settings = MythicMateNS.Settings or {}
local Settings = MythicMateNS.Settings

local aceDBInitialized = (MythicMateDB and MythicMateDB.profile) and true or false

local function buildDefaults()
    return {
        profile = {
            showPlayerHoverTooltip = true,
            showRaiderIOLockoutDialog = true,
            premadeCustomFilterEnabled = true,
            useElvUISkinDefaults = true,
            activeTab = "DUNGEONS",
            hideLockedByTab = {},
            typeFilterByTab = {},
            useInstanceAbbreviations = false,
            shareChannel = "PARTY",
            whisperTarget = nil,
            selectedRemotePlayer = nil,
            whisperAutoReplyEnabled = false,
            whisperAutoReplyKeyword = "whatlockout",
        },
    }
end

local function applyLegacyFallback()
    MythicMateDB = MythicMateDB or {}
    MythicMateDB.minimapIcon = MythicMateDB.minimapIcon or { hide = false, minimapPos = 220 }
    MythicMateDB.settings = MythicMateDB.settings or {}
    MythicMateDB.ownCharacters = MythicMateDB.ownCharacters or {}

    if MythicMateDB.settings.showPlayerHoverTooltip == nil then
        MythicMateDB.settings.showPlayerHoverTooltip = true
    end
    if MythicMateDB.settings.showRaiderIOLockoutDialog == nil then
        MythicMateDB.settings.showRaiderIOLockoutDialog = true
    end
    if MythicMateDB.settings.premadeCustomFilterEnabled == nil then
        MythicMateDB.settings.premadeCustomFilterEnabled = true
    end
    if MythicMateDB.settings.useElvUISkinDefaults == nil then
        MythicMateDB.settings.useElvUISkinDefaults = true
    end
    if type(MythicMateDB.settings.activeTab) ~= "string" or MythicMateDB.settings.activeTab == "" then
        MythicMateDB.settings.activeTab = "DUNGEONS"
    end
    if type(MythicMateDB.settings.hideLockedByTab) ~= "table" then
        MythicMateDB.settings.hideLockedByTab = {}
    end
    if type(MythicMateDB.settings.typeFilterByTab) ~= "table" then
        MythicMateDB.settings.typeFilterByTab = {}
    end
    if MythicMateDB.settings.useInstanceAbbreviations == nil then
        MythicMateDB.settings.useInstanceAbbreviations = false
    end
    if type(MythicMateDB.settings.shareChannel) ~= "string" or MythicMateDB.settings.shareChannel == "" then
        MythicMateDB.settings.shareChannel = "PARTY"
    end
    if MythicMateDB.settings.whisperTarget ~= nil and type(MythicMateDB.settings.whisperTarget) ~= "string" then
        MythicMateDB.settings.whisperTarget = nil
    end
    if MythicMateDB.settings.whisperAutoReplyEnabled == nil then
        MythicMateDB.settings.whisperAutoReplyEnabled = false
    end
    if type(MythicMateDB.settings.whisperAutoReplyKeyword) ~= "string" or MythicMateDB.settings.whisperAutoReplyKeyword == "" then
        MythicMateDB.settings.whisperAutoReplyKeyword = "whatlockout"
    end
end

local function tryInitAceDB()
    local LibStub = _G.LibStub
    if not LibStub or type(LibStub) ~= "function" then return false end

    local AceDB = LibStub("AceDB-3.0", true)
    if not AceDB then return false end

    local legacySettings = (MythicMateDB and MythicMateDB.settings) and MythicMateDB.settings or nil
    local legacyMinimapIcon = MythicMateDB and MythicMateDB.minimapIcon or nil
    local legacyOwnCharacters = MythicMateDB and MythicMateDB.ownCharacters or nil
    local legacyShared = MythicMateDB and MythicMateDB.shared or nil

    MythicMateDB = AceDB:New("MythicMateDB", buildDefaults(), true)
    MythicMateDB.settings = MythicMateDB.profile

    if legacyMinimapIcon then MythicMateDB.minimapIcon = legacyMinimapIcon end
    if legacyOwnCharacters then MythicMateDB.ownCharacters = legacyOwnCharacters end
    if legacyShared then MythicMateDB.shared = legacyShared end

    if legacySettings and type(legacySettings) == "table" then
        for k, v in pairs(legacySettings) do
            MythicMateDB.settings[k] = v
        end
    end
    if MythicMateDB.settings.showRaiderIOLockoutDialog == nil then
        MythicMateDB.settings.showRaiderIOLockoutDialog = true
    end

    aceDBInitialized = true
    return true
end

function Settings.EnsureInitialized()
    if not tryInitAceDB() then
        applyLegacyFallback()
    end
    return true
end

function Settings.TryInitAceDB()
    if aceDBInitialized then return true end
    return tryInitAceDB()
end
