local GC = select(2, ...)

local Init = {}
GC.Init = Init

--[[------------------------------------------------------------------------
    Initialize the SavedVariables databases
--------------------------------------------------------------------------]]
function Init.InitDB()
    GC.Debug.Log("InitDB")

    -- Main addon database
    GC.db = LibStub:GetLibrary("AceDB-3.0"):New("GallywixCoinsDB_V2",
                                                GC.CONST.DB_DEFAULTS, true)

    -- Loot tracking database
    GallywixCoinsDB = LibStub("AceDB-3.0"):New("GallywixCoinsLootDB_V2", GC.CONST.DB_LOOT, true)
end

--[[------------------------------------------------------------------------
    Get a value from the profile DB with defaults fallback
    Supports optional "TSM_REQUIRED" flag
--------------------------------------------------------------------------]]
function GC.GetFromDb(grp, key, ...)
    local tsmRequired
    for i = 1, select('#', ...) do
        local opt = select(i, ...)
        if opt and opt == "TSM_REQUIRED" then
            tsmRequired = true
        end
    end

    if tsmRequired and not GC.TSM.IsTSMLoaded() then return false end
 
    if not GC.db.profile[grp] then
        GC.db.profile[grp] = GC.CONST.DB_DEFAULTS.profile[grp] or {}
    end
    if GC.db.profile[grp][key] == nil then
        GC.db.profile[grp][key] = (GC.CONST.DB_DEFAULTS.profile[grp] and GC.CONST.DB_DEFAULTS.profile[grp][key])
    end
    return GC.db.profile[grp][key]
end

