local _, SinsToolkit = ...

local Vault = {}
SinsToolkit.ProfileVault = Vault

local MAX_ACTION_SLOTS = 180
local CHARACTER_MACRO_START = (MAX_ACCOUNT_MACROS or 120) + 1
local HasActionCompat = (C_ActionBar and C_ActionBar.HasAction) or HasAction

local function EnsureDB()
    if SinsToolkit.EnsureDB then SinsToolkit.EnsureDB() end
    SinsToolkitDB.profileVaultBackups = SinsToolkitDB.profileVaultBackups or {}
end

local function DeepCopy(value)
    if type(value) ~= "table" then return value end
    local copy = {}
    for k, v in pairs(value) do
        copy[k] = DeepCopy(v)
    end
    return copy
end

local function GetCharacterKey()
    local name = UnitName("player") or UNKNOWN
    local realm = GetRealmName() or ""
    return name .. "-" .. realm, name, realm
end

local function GetClassInfo()
    local localizedClass, classToken = UnitClass("player")
    return classToken or "UNKNOWN", localizedClass or "Unknown"
end

local function MakeBackupID()
    local characterKey = GetCharacterKey()
    return characterKey .. "-" .. time()
end

local function CaptureAction(slot)
    local actionType, id, subType = GetActionInfo(slot)
    if not actionType or not id then return nil end

    local action = { slot = slot, type = actionType, id = id, subType = subType }
    if actionType == "macro" then
        local name, icon, body = GetMacroInfo(id)
        action.name = name
        action.icon = icon
        action.body = body
    elseif actionType == "equipmentset" and C_EquipmentSet then
        action.name = C_EquipmentSet.GetEquipmentSetInfo(id)
    end
    return action
end

local function CaptureActions()
    local actions = {}
    for slot = 1, MAX_ACTION_SLOTS do
        local action = CaptureAction(slot)
        if action then
            actions[#actions + 1] = action
        end
    end
    return actions
end

local function CaptureMacros()
    local macros = { account = {}, character = {} }
    local accountCount, characterCount = GetNumMacros()

    for index = 1, accountCount do
        local name, icon, body = GetMacroInfo(index)
        if name then
            macros.account[#macros.account + 1] = { name = name, icon = icon, body = body }
        end
    end

    for offset = 1, characterCount do
        local index = CHARACTER_MACRO_START + offset - 1
        local name, icon, body = GetMacroInfo(index)
        if name then
            macros.character[#macros.character + 1] = { name = name, icon = icon, body = body }
        end
    end

    return macros
end

local function CaptureBindings()
    local bindings = {}
    for index = 1, GetNumBindings() do
        local command, _, key1, key2 = GetBinding(index)
        if command and (key1 or key2) then
            bindings[#bindings + 1] = { command = command, key1 = key1, key2 = key2 }
        end
    end
    return bindings
end

function Vault:CreateBackup(name)
    EnsureDB()

    name = (name or ""):match("^%s*(.-)%s*$")
    if name == "" then
        name = "Backup"
    end

    local characterKey, characterName, realmName = GetCharacterKey()
    local classToken, className = GetClassInfo()
    local backup = {
        id = MakeBackupID(),
        name = name,
        character = characterName,
        realm = realmName,
        characterKey = characterKey,
        classToken = classToken,
        className = className,
        createdAt = date("%Y-%m-%d %H:%M:%S"),
        createdAtEpoch = time(),
        actions = CaptureActions(),
        macros = CaptureMacros(),
        bindings = CaptureBindings(),
    }

    SinsToolkitDB.profileVaultBackups[backup.id] = backup
    return backup
end

local function ClearActionSlot(slot)
    if HasActionCompat and HasActionCompat(slot) then
        PickupAction(slot)
        ClearCursor()
    end
end

local function PickupStoredAction(action)
    if not action then return false end

    if action.type == "spell" then
        if C_Spell and C_Spell.PickupSpell then
            C_Spell.PickupSpell(action.id)
        elseif PickupSpell then
            PickupSpell(action.id)
        end
    elseif action.type == "item" then
        if C_Item and C_Item.PickupItem then
            C_Item.PickupItem(action.id)
        elseif PickupItem then
            PickupItem(action.id)
        end
    elseif action.type == "macro" then
        PickupMacro(action.name or action.id)
    elseif action.type == "flyout" and PickupSpellBookItem then
        PickupSpellBookItem(action.id, BOOKTYPE_SPELL)
    elseif action.type == "equipmentset" and C_EquipmentSet and action.name then
        C_EquipmentSet.PickupEquipmentSetByName(action.name)
    elseif action.type == "summonmount" and C_MountJournal and C_MountJournal.Pickup then
        C_MountJournal.Pickup(action.id)
    elseif action.type == "companion" and PickupCompanion then
        PickupCompanion(action.subType or "CRITTER", action.id)
    end

    return GetCursorInfo() ~= nil
end

local function RestoreActions(actions)
    local restored = 0
    local skipped = 0

    for slot = 1, MAX_ACTION_SLOTS do
        ClearActionSlot(slot)
    end

    for _, action in ipairs(actions or {}) do
        ClearCursor()
        if PickupStoredAction(action) then
            PlaceAction(action.slot)
            restored = restored + 1
        else
            skipped = skipped + 1
        end
        ClearCursor()
    end

    return restored, skipped
end

local function FindMacroIndex(name, perCharacter)
    local accountCount, characterCount = GetNumMacros()
    local startIndex = perCharacter and CHARACTER_MACRO_START or 1
    local endIndex = perCharacter and (CHARACTER_MACRO_START + characterCount - 1) or accountCount

    for index = startIndex, endIndex do
        local macroName = GetMacroInfo(index)
        if macroName == name then
            return index
        end
    end
end

local function UpsertMacro(macro, perCharacter)
    if not macro or not macro.name then return end

    local index = FindMacroIndex(macro.name, perCharacter)
    if index then
        EditMacro(index, macro.name, macro.icon or "INV_Misc_QuestionMark", macro.body or "")
    else
        CreateMacro(macro.name, macro.icon or "INV_Misc_QuestionMark", macro.body or "", perCharacter)
    end
end

local function RestoreMacros(macros)
    for _, macro in ipairs((macros and macros.account) or {}) do
        UpsertMacro(macro, false)
    end
    for _, macro in ipairs((macros and macros.character) or {}) do
        UpsertMacro(macro, true)
    end
end

local function RestoreBindings(bindings)
    for index = 1, GetNumBindings() do
        local _, _, key1, key2 = GetBinding(index)
        if key1 then SetBinding(key1, nil) end
        if key2 then SetBinding(key2, nil) end
    end

    for _, binding in ipairs(bindings or {}) do
        local key1, key2 = GetBindingKey(binding.command)
        if key1 then SetBinding(key1, nil) end
        if key2 then SetBinding(key2, nil) end
    end

    for _, binding in ipairs(bindings or {}) do
        if binding.key1 then SetBinding(binding.key1, binding.command) end
        if binding.key2 then SetBinding(binding.key2, binding.command) end
    end
    SaveBindings(GetCurrentBindingSet())
end

function Vault:RestoreBackup(id)
    EnsureDB()
    if InCombatLockdown and InCombatLockdown() then
        return false, "Profile Vault restore is blocked while in combat."
    end

    local backup = SinsToolkitDB.profileVaultBackups[id]
    if not backup then
        return false, "Backup not found."
    end

    RestoreMacros(backup.macros)
    local restoredActions, skippedActions = RestoreActions(backup.actions)
    RestoreBindings(backup.bindings)
    local bindingCount = backup.bindings and #backup.bindings or 0
    return true, ("Restored %s. Actions: %d restored, %d skipped. Bindings: %d applied."):format(
        backup.name or "backup",
        restoredActions or 0,
        skippedActions or 0,
        bindingCount
    )
end

function Vault:DeleteBackup(id)
    EnsureDB()
    SinsToolkitDB.profileVaultBackups[id] = nil
end

function Vault:GetBackups()
    EnsureDB()
    local backups = {}
    for id, backup in pairs(SinsToolkitDB.profileVaultBackups) do
        local copy = DeepCopy(backup)
        copy.id = id
        backups[#backups + 1] = copy
    end
    table.sort(backups, function(a, b)
        return (a.createdAtEpoch or 0) > (b.createdAtEpoch or 0)
    end)
    return backups
end
