--[[--------------------------------------------------------------------
  PaladinHolyPower - logic/startup.lua
  Startup: SavedVariables, migration, slash command, bootstraps.
  Pure PHP (no compatibility alias).
----------------------------------------------------------------------]]

local ADDON_NAME = ...

local PHP = _G.PaladinHolyPower
if not PHP then return end

------------------------------------------------------------------------
-- Pretty print
------------------------------------------------------------------------
function PHP.Print(msg)
  print("|cffF58CBA[Paladin•HP]|r " .. tostring(msg))
end

------------------------------------------------------------------------
-- Slash command: /php  and  /php log
------------------------------------------------------------------------
SLASH_PHP1 = "/php"
SlashCmdList.PHP = function(msg)
  msg = tostring(msg or "")
  -- trim whitespace
  msg = msg:match("^%s*(.-)%s*$")
  msg = msg:lower()

  if msg == "log" then
    -- open/toggle the HP Log window if available
    if PHP.HPLog_ToggleWindow then
      PHP.HPLog_ToggleWindow()
    elseif PHP.HPLog_OpenWindow then
      PHP.HPLog_OpenWindow()
    else
      PHP.Print("Log window not loaded yet.")
    end
    return
  end

  -- default: open settings
  if PHP.ShowSettingsPanel then
    PHP.ShowSettingsPanel()
  else
    PHP.Print("Settings panel not loaded yet.")
  end
end

------------------------------------------------------------------------
-- Optional: called by settings checkbox to re-evaluate minimap button
------------------------------------------------------------------------
function PHP.OnMinimapVisibilityChanged()
  if PHP.UpdateMinimapButtonVisibility then
    PHP.UpdateMinimapButtonVisibility()
  end
end

------------------------------------------------------------------------
-- ADDON_LOADED: initialize DB, migrate, and bootstrap features
------------------------------------------------------------------------
local initialized = false
local loader = CreateFrame("Frame")
loader:RegisterEvent("ADDON_LOADED")
loader:SetScript("OnEvent", function(_, _, name)
  if initialized or name ~= ADDON_NAME then return end

  -- Ensure SavedVariables table exists
  PaladinHolyPowerDB = PaladinHolyPowerDB or {}

  -- One-time migration from the old variable if present and new DB is empty
  if type(HPWasteSettings) == "table" and next(PaladinHolyPowerDB) == nil then
    PaladinHolyPowerDB = HPWasteSettings
    PaladinHolyPowerDB._migratedFromHPW = true
    HPWasteSettings = nil
  end

  -- Point addon at the DB
  PHP.settings = PaladinHolyPowerDB

  -- Apply defaults / upgrade if helper exists
  if type(PHP.CopyDefaults) == "function" then
    PHP.settings = PHP.CopyDefaults(PHP.settings)
  end

  -- Ensure tracker subtables exist
  PHP.settings.HPL = PHP.settings.HPL or {}
  PHP.settings.HP  = PHP.settings.HP  or {}

  -- Sensible first-run defaults
  if PHP.settings.HPL.enabled == nil then PHP.settings.HPL.enabled = true end
  if PHP.settings.HP.enabled  == nil then PHP.settings.HP.enabled  = true end

  -- Minimap button (if module loaded)
  if PHP.InitMinimapButton     then PHP.InitMinimapButton()     end
  if PHP.SafeShowMinimapButton then PHP.SafeShowMinimapButton() end

  -- Feature bootstraps (modules provide these if needed)
  if PHP.InitializeHPTrackerEnable  then PHP.InitializeHPTrackerEnable()  end
  if PHP.InitializeHPLTrackerEnable then PHP.InitializeHPLTrackerEnable() end

  -- First paint (safe if functions exist)
  if PHP.UpdateHPTracker  then PHP.UpdateHPTracker()  end
  if PHP.UpdateHPLTracker then PHP.UpdateHPLTracker() end

  initialized = true
end)