local _, ns = ...
local BDT = ns.DataTexts
local L = BDT.L
BDT.ProfileManager = {}

----------------------------------------------------
-- WoW API Locals
----------------------------------------------------
local GetNumSpecializations = GetNumSpecializations
local GetSpecialization     = GetSpecialization
local GetSpecializationInfo = GetSpecializationInfo

----------------------------------------------------
-- Initialize Spec Profiles
----------------------------------------------------
function BDT.ProfileManager:InitializeSpecProfiles()
    for i = 1, GetNumSpecializations() do
        local _, specName = GetSpecializationInfo(i)
        if specName and not BDT.db.char.chosenProfile[specName] then
            BDT.db.char.chosenProfile[specName] = BDT.cache.charKey .. "-" .. specName:lower()
        end
    end
end

----------------------------------------------------
-- Switch to the Active Spec's Profile
----------------------------------------------------
function BDT.ProfileManager:SwitchToSpecProfile()
    if not BDT.db.char.useSpecProfiles then return end

    local specIndex = GetSpecialization()
    if not specIndex then return end

    local _, specName = GetSpecializationInfo(specIndex)
    if not specName then return end

    -- Ensure a profile name exists for this spec
    if not BDT.db.char.chosenProfile[specName] then
        BDT.db.char.chosenProfile[specName] = BDT.cache.charKey .. "-" .. specName:lower()
    end

    local targetProfile = BDT.db.char.chosenProfile[specName]
    if BDT.db:GetCurrentProfile() ~= targetProfile then
        BDT.db:SetProfile(targetProfile)
    end
end
