local WhatLockoutNS = _G.WhatLockout
if not WhatLockoutNS then return end

WhatLockoutNS.Settings = WhatLockoutNS.Settings or {}
local Settings = WhatLockoutNS.Settings

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

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

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

    if WhatLockoutDB.settings.showPlayerHoverTooltip == nil then
        WhatLockoutDB.settings.showPlayerHoverTooltip = true
    end
    if WhatLockoutDB.settings.showRaiderIOLockoutDialog == nil then
        WhatLockoutDB.settings.showRaiderIOLockoutDialog = true
    end
    if WhatLockoutDB.settings.useElvUISkinDefaults == nil then
        WhatLockoutDB.settings.useElvUISkinDefaults = true
    end
    if type(WhatLockoutDB.settings.activeTab) ~= "string" or WhatLockoutDB.settings.activeTab == "" then
        WhatLockoutDB.settings.activeTab = "DUNGEONS"
    end
    if type(WhatLockoutDB.settings.hideLockedByTab) ~= "table" then
        WhatLockoutDB.settings.hideLockedByTab = {}
    end
    if type(WhatLockoutDB.settings.typeFilterByTab) ~= "table" then
        WhatLockoutDB.settings.typeFilterByTab = {}
    end
    if WhatLockoutDB.settings.useInstanceAbbreviations == nil then
        WhatLockoutDB.settings.useInstanceAbbreviations = false
    end
    if type(WhatLockoutDB.settings.shareChannel) ~= "string" or WhatLockoutDB.settings.shareChannel == "" then
        WhatLockoutDB.settings.shareChannel = "PARTY"
    end
    if WhatLockoutDB.settings.whisperTarget ~= nil and type(WhatLockoutDB.settings.whisperTarget) ~= "string" then
        WhatLockoutDB.settings.whisperTarget = nil
    end
    if WhatLockoutDB.settings.whisperAutoReplyEnabled == nil then
        WhatLockoutDB.settings.whisperAutoReplyEnabled = false
    end
    if type(WhatLockoutDB.settings.whisperAutoReplyKeyword) ~= "string" or WhatLockoutDB.settings.whisperAutoReplyKeyword == "" then
        WhatLockoutDB.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 = (WhatLockoutDB and WhatLockoutDB.settings) and WhatLockoutDB.settings or nil
    local legacyMinimapIcon = WhatLockoutDB and WhatLockoutDB.minimapIcon or nil
    local legacyOwnCharacters = WhatLockoutDB and WhatLockoutDB.ownCharacters or nil
    local legacyShared = WhatLockoutDB and WhatLockoutDB.shared or nil

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

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

    if legacySettings and type(legacySettings) == "table" then
        for k, v in pairs(legacySettings) do
            WhatLockoutDB.settings[k] = v
        end
    end
    if WhatLockoutDB.settings.showRaiderIOLockoutDialog == nil then
        WhatLockoutDB.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
