_G.LIDCOUNCIL_CreateLootProfileBackupHelpers_Impl = function(deps)
  deps = type(deps) == "table" and deps or {}

  local TrimText = deps.TrimText
  local RootDB = deps.RootDB
  local DeepCopy = deps.DeepCopy
  local GetLootProfile = deps.GetLootProfile
  local GetLootProfileIdentityInfo = deps.GetLootProfileIdentityInfo
  local GetActiveLootSession = deps.GetActiveLootSession
  local TouchLootProfileOperational = deps.TouchLootProfileOperational

  local function trim(value)
    if type(TrimText) == "function" then
      return TrimText(value)
    end
    local text = tostring(value or "")
    text = text:gsub("^%s+", "")
    text = text:gsub("%s+$", "")
    return text
  end

  local function copy(value)
    if type(DeepCopy) == "function" then
      return DeepCopy(value)
    end
    return value
  end

  local function GetLootProfileBackupKey(profileName, profile)
    local resolvedName = trim(profileName)
    local identity = type(GetLootProfileIdentityInfo) == "function" and GetLootProfileIdentityInfo(resolvedName) or nil
    local profileId = type(identity) == "table" and trim(identity.profileId) or ""
    if profileId == "" and type(profile) == "table" and type(profile.meta) == "table" then
      profileId = trim(profile.meta.profileId)
    end
    if profileId ~= "" then
      return "profileId:" .. profileId
    end
    return "profileName:" .. resolvedName
  end

  local function EnsureLootProfileBackupBucket(profileName)
    local profile, resolvedName
    if type(GetLootProfile) == "function" then
      profile, resolvedName = GetLootProfile(profileName)
    end
    if type(profile) ~= "table" then
      return nil, "Profile not found."
    end

    local root = type(RootDB) == "function" and RootDB() or {}
    root.localProfileBackups = type(root.localProfileBackups) == "table" and root.localProfileBackups or {}
    local bucketKey = GetLootProfileBackupKey(resolvedName, profile)
    local bucket = type(root.localProfileBackups[bucketKey]) == "table" and root.localProfileBackups[bucketKey] or nil
    if not bucket then
      bucket = { profileName = resolvedName, backups = {} }
      root.localProfileBackups[bucketKey] = bucket
    end
    bucket.profileName = resolvedName
    bucket.profileId = type(profile.meta) == "table" and trim(profile.meta.profileId) or ""
    bucket.backups = type(bucket.backups) == "table" and bucket.backups or {}
    return bucket, profile, resolvedName
  end

  local function BuildLootProfileBackupSnapshot(profile)
    profile = type(profile) == "table" and profile or {}
    return {
      lootLedger = copy(profile.lootLedger or {}),
      lootLedgerMeta = copy(profile.lootLedgerMeta or {}),
      lootLedgerEntrySerial = tonumber(profile.lootLedgerEntrySerial) or 0,
      lootRollStats = copy(profile.lootRollStats or {}),
      lootListGroups = copy(profile.lootListGroups or {}),
      sessions = copy(profile.sessions or {}),
      liveBoardHistory = copy(profile.liveBoardHistory or {}),
    }
  end

  local function GetLootProfileBackups(profileName)
    local bucket, err, resolvedName = EnsureLootProfileBackupBucket(profileName)
    if type(bucket) ~= "table" then
      return {}, err
    end

    local list = {}
    for _, backup in ipairs(bucket.backups or {}) do
      if type(backup) == "table" and trim(backup.id) ~= "" then
        list[#list + 1] = {
          id = trim(backup.id),
          createdAt = tonumber(backup.createdAt) or 0,
          label = trim(backup.label),
          profileName = resolvedName,
        }
      end
    end
    table.sort(list, function(a, b)
      return (tonumber(a.createdAt) or 0) > (tonumber(b.createdAt) or 0)
    end)
    return list, resolvedName
  end

  local function GetBackupSessionLockMessage()
    local activeSession = type(GetActiveLootSession) == "function" and GetActiveLootSession() or nil
    if activeSession then
      return "Stop the active loot session before changing local backups."
    end
    return nil
  end

  local function SaveLootProfileBackup(profileName)
    local lockMessage = GetBackupSessionLockMessage()
    if lockMessage then
      return false, lockMessage
    end
    local bucket, profile, resolvedName = EnsureLootProfileBackupBucket(profileName)
    if type(bucket) ~= "table" then
      return false, tostring(profile or "Profile not found.")
    end

    bucket.nextSerial = math.max(0, tonumber(bucket.nextSerial) or 0) + 1
    local createdAt = time and time() or 0
    local label = date and date("%Y-%m-%d %H:%M:%S", createdAt) or tostring(createdAt)
    local backup = {
      id = "backup-" .. tostring(createdAt) .. "-" .. tostring(bucket.nextSerial),
      createdAt = createdAt,
      label = label,
      snapshot = BuildLootProfileBackupSnapshot(profile),
    }
    table.insert(bucket.backups, 1, backup)
    return true, backup, resolvedName
  end

  local function FindLootProfileBackup(bucket, backupId)
    local resolvedId = trim(backupId)
    if resolvedId == "" then
      return nil
    end
    for index, backup in ipairs(bucket and bucket.backups or {}) do
      if type(backup) == "table" and trim(backup.id) == resolvedId then
        return backup, index
      end
    end
    return nil
  end

  local function LoadLootProfileBackup(profileName, backupId)
    local lockMessage = GetBackupSessionLockMessage()
    if lockMessage then
      return false, lockMessage
    end

    local bucket, profile = EnsureLootProfileBackupBucket(profileName)
    if type(bucket) ~= "table" then
      return false, tostring(profile or "Profile not found.")
    end
    local backup = FindLootProfileBackup(bucket, backupId)
    if type(backup) ~= "table" or type(backup.snapshot) ~= "table" then
      return false, "Backup not found."
    end

    local snapshot = backup.snapshot
    profile.lootLedger = copy(snapshot.lootLedger or {})
    profile.lootLedgerMeta = copy(snapshot.lootLedgerMeta or {})
    profile.lootLedgerEntrySerial = tonumber(snapshot.lootLedgerEntrySerial) or 0
    profile.lootRollStats = copy(snapshot.lootRollStats or {})
    profile.lootListGroups = copy(snapshot.lootListGroups or {})
    profile.sessions = copy(snapshot.sessions or {})
    profile.liveBoardHistory = copy(snapshot.liveBoardHistory or {})
    if type(TouchLootProfileOperational) == "function" then
      TouchLootProfileOperational(profile)
    end
    return true, backup
  end

  local function DeleteLootProfileBackup(profileName, backupId)
    local lockMessage = GetBackupSessionLockMessage()
    if lockMessage then
      return false, lockMessage
    end
    local bucket, err = EnsureLootProfileBackupBucket(profileName)
    if type(bucket) ~= "table" then
      return false, tostring(err or "Profile not found.")
    end
    local _, index = FindLootProfileBackup(bucket, backupId)
    if not index then
      return false, "Backup not found."
    end
    table.remove(bucket.backups, index)
    return true
  end

  return {
    GetLootProfileBackups = GetLootProfileBackups,
    SaveLootProfileBackup = SaveLootProfileBackup,
    LoadLootProfileBackup = LoadLootProfileBackup,
    DeleteLootProfileBackup = DeleteLootProfileBackup,
  }
end
