-- Assistant Profiles registry: exposes profile lifecycle, staging, import/export, and diagnostics.
-- Destructive or copy actions must remain snapshot-backed and confirmation-gated.
local addonName, MSUF = ...
MSUF = MSUF or _G.MSUF_NS or {}
local M = MSUF.MSUF2 or _G.MSUF2 or {}
MSUF.MSUF2 = M

local A = MSUF.Assistant or {}
MSUF.Assistant = A
M.Assistant = A

local Registry = A.Registry
if not (Registry and type(Registry.RegisterAction) == "function" and type(Registry.RegisterSetting) == "function") then return end

-- Profile assistant registry.
-- Actions here stage or invoke profile operations, but parsing must preserve user-provided
-- profile names exactly enough for import/export/copy flows to resolve them safely.
local Profile = A.ProfileWorkflow
local ResolveProfileName = A.ResolveProfileName
local ProfileExists = A.ProfileExists
local ActiveProfileName = A.ActiveProfileName
local RequireUUFBestEffortAccepted = type(Profile) == "table" and Profile.RequireUUFBestEffortAccepted or nil
local RegisterImportExportActions = A.ProfileRegistry and A.ProfileRegistry.RegisterImportExportActions
local RegisterLifecycleActions = A.ProfileRegistry and A.ProfileRegistry.RegisterLifecycleActions
if type(Profile) ~= "table"
    or type(ResolveProfileName) ~= "function"
    or type(ProfileExists) ~= "function"
    or type(ActiveProfileName) ~= "function"
    or type(RequireUUFBestEffortAccepted) ~= "function"
then
    return
end

Registry:RegisterAction({
    key = "reset_profile",
    label = "Reset Active Profile",
    type = "profile",
    combatSafe = false,
    confirmRequired = true,
    captureSnapshot = true,
    captureProfileSnapshot = true,
    run = function()
        if M and type(M.ResetPageToDefaults) == "function" and M.ResetPageToDefaults("profiles") then
            return true, "Done. Reset the active profile."
        end
        return false, "Open Profiles first so I can reset the profile."
    end,
})

Registry:RegisterSetting({
    key = "profiles.specAutoSwitch",
    label = "Auto-switch Profile by Specialization",
    category = "Profiles / Spec Profiles",
    unit = "global",
    frameType = "profiles",
    attribute = "specAutoSwitch",
    type = "boolean",
    aliases = {
        "auto switch profile by specialization", "auto switch profile by spec",
        "profile auto switch", "spec profile switching", "specialization profile switching",
        "profile by specialization", "profile by spec",
        "profil automatisch wechseln", "profil auto switch", "profil nach spec",
        "profil nach spezialisierung", "spec profil wechsel", "spezialisierungs profil wechsel",
    },
    get = function() return Profile.SpecAutoSwitchEnabled() end,
    set = function(value) Profile.SetSpecAutoSwitch(value and true or false) end,
    apply = function() Profile.Refresh() end,
    combatSafe = false,
})

if type(RegisterImportExportActions) == "function" then
    RegisterImportExportActions({
        Registry = Registry,
        Profile = Profile,
        ProfileExists = ProfileExists,
        ActiveProfileName = ActiveProfileName,
        RequireUUFBestEffortAccepted = RequireUUFBestEffortAccepted,
        Assistant = A,
        Menu = M,
    })
end

if type(RegisterLifecycleActions) == "function" then
    RegisterLifecycleActions({
        Registry = Registry,
        Profile = Profile,
        ResolveProfileName = ResolveProfileName,
        ProfileExists = ProfileExists,
        Assistant = A,
    })
end

Registry:RegisterAction({
    key = "set_spec_profile",
    label = "Set Spec Profile",
    type = "profile",
    combatSafe = false,
    captureSnapshot = true,
    captureProfileSnapshot = true,
    run = function(args)
        local specValue = args and args.spec
        local profileValue = args and args.name
        local specID, specName = Profile.ResolveSpecID(specValue)
        if specName == "multiple" then return false, "I found multiple matching specializations. Which full specialization name or ID do you want me to use?" end
        if not specID then return false, "Which specialization do you want me to use? A name or ID is enough." end
        if type(profileValue) ~= "string" or profileValue == "" then return false, "Which profile do you want me to use for " .. Profile.SpecLabel(specID) .. "?" end
        local requested = profileValue
        local resolved, how = ResolveProfileName(profileValue)
        if how == "multiple" then return false, "I found multiple matching profiles. Which full profile name do you want me to use?" end
        if not ProfileExists(resolved) then return false, "I don't see that profile: " .. tostring(requested) .. "." end
        if not Profile.SetSpecProfile(specID, resolved) then return false, "Open Profiles first so I can assign the specialization profile." end
        Profile.Refresh()
        return true, "Done. " .. Profile.SpecLabel(specID) .. " now uses profile " .. tostring(resolved) .. "."
    end,
})

Registry:RegisterAction({
    key = "clear_spec_profile",
    label = "Clear Spec Profile",
    type = "profile",
    combatSafe = false,
    captureSnapshot = true,
    captureProfileSnapshot = true,
    run = function(args)
        local specValue = args and args.spec
        local specID, specName = Profile.ResolveSpecID(specValue)
        if specName == "multiple" then return false, "I found multiple matching specializations. Which full specialization name or ID do you want me to use?" end
        if not specID then return false, "Which specialization do you want me to use? A name or ID is enough." end
        if not Profile.SetSpecProfile(specID, nil) then return false, "Open Profiles first so I can clear the specialization profile." end
        Profile.Refresh()
        return true, "Done. Cleared the profile assignment for " .. Profile.SpecLabel(specID) .. "."
    end,
})
