-- VDS_SessionIdentity
-- ============================================================================
-- Single source of truth for player identity (session.identity).
-- NEVER call UnitName/GetRealmName directly elsewhere.
-- LESSON (HDGR 9363a86d): six independent derivation sites caused split-brain
-- across several commits. Ship this canonical module from day 1.

VDS = VDS or {}
VDS.SessionIdentity = VDS.SessionIdentity or {}

local SI = VDS.SessionIdentity

-- Canonical accessor; never call UnitName/GetRealmName directly.
function SI.GetCharKey(state)
    return state.session.identity.charKey
end

function SI.GetName(state)
    return state.session.identity.name
end

function SI.GetRealm(state)
    return state.session.identity.realm
end

VDS.Modules:Declare({
    name = "SessionIdentity",
    dependencies = {},

    onEnable = function(self)
        local name = _G.UnitName("player")
        if not name then
            -- LESSON (HDGR 049383bd): silent nil return would leave identity empty all session; fail loud.
            VDS.Log:Warn("identity", "UnitName returned nil; identity unresolved")
            return
        end
        local _, classFile = _G.UnitClass("player")
        VDS.Store:Dispatch({
            type    = VDS.Constants.ACTIONS.SESSION_IDENTITY_SET,
            payload = { name = name, realm = _G.GetRealmName(), class = classFile or "" },
        })
    end,
})
