--------------------------------------------------------------------------------
-- S-Frames: Bag Module Core (Bags/Core.lua)
-- Entry point, registered on PLAYER_LOGIN
--------------------------------------------------------------------------------

SFrames.Bags.Core = {}

-- Ensure default config exists
if not SFrames.Config.Bags then
    SFrames.Config.Bags = {
        enable   = true,
        columns  = 10,
        bagSpacing = 0,
        scale    = 1,
        sellGrey = true,
        bankColumns = 12,
        bankSpacing = 0,
        bankScale   = 1,
    }
end

local function EnsureDB()
    if not SFramesDB then SFramesDB = {} end
    if not SFramesDB.Bags then
        SFramesDB.Bags = {}
        for k, v in pairs(SFrames.Config.Bags) do
            SFramesDB.Bags[k] = v
        end
    end
    -- Fill missing keys with defaults
    for k, v in pairs(SFrames.Config.Bags) do
        if SFramesDB.Bags[k] == nil then
            SFramesDB.Bags[k] = v
        end
    end
end

local function HookBagFunctions()
    -- Save original functions
    local _OpenAllBags     = OpenAllBags
    local _CloseAllBags    = CloseAllBags
    local _ToggleBag       = ToggleBag
    local _ToggleBackpack  = ToggleBackpack
    local _ToggleKeyRing   = ToggleKeyRing
    SFrames.Bags._origToggleKeyRing = _ToggleKeyRing

    OpenAllBags = function()
        if SFramesDB.Bags.enable then
            SFrames.Bags.Container:Open()
        else
            _OpenAllBags()
        end
    end

    CloseAllBags = function()
        if SFramesDB.Bags.enable then
            SFrames.Bags.Container:Close()
        else
            _CloseAllBags()
        end
    end

    ToggleBag = function(id)
        if id == -2 or id == (KEYRING_CONTAINER or -2) then
            _ToggleBag(id)
            return
        end
        if SFramesDB.Bags.enable then
            SFrames.Bags.Container:Toggle()
        else
            _ToggleBag(id)
        end
    end

    ToggleBackpack = function()
        if SFramesDB.Bags.enable then
            SFrames.Bags.Container:Toggle()
        else
            _ToggleBackpack()
        end
    end

    -- Keyring: always use original keyring window
    ToggleKeyRing = function()
        _ToggleKeyRing()
    end
end

function SFrames.Bags.Core:Initialize()
    SFrames:Print("Debug: Bags Core Initializing...")
    EnsureDB()
    if not SFramesDB.Bags.enable then
        SFrames:Print("Debug: Bags are disabled in config.")
        return
    end

    SFrames:Print("Debug: Hooking functions...")
    HookBagFunctions()
    
    SFrames:Print("Debug: Init Container...")
    SFrames.Bags.Container:Initialize()
    
    SFrames:Print("Debug: Init Bank...")
    if SFrames.Bags.Bank then SFrames.Bags.Bank:Initialize() end

    SFrames:Print("Debug: Init Features...")
    SFrames.Bags.Features:Initialize()
    
    SFrames:Print("Debug: Bags Init Complete.")
end

-- NOTE: Initialize is called from Core.lua SFrames:Initialize() on PLAYER_LOGIN.
-- Do NOT register PLAYER_LOGIN here to avoid double initialization.
