--[[--------------------------------------------------------------------
  PaladinHolyPower - tabs/hp_tab.lua
  Holy Power Tab: With Three Subcategories (General, HP Log, HP Bar)
----------------------------------------------------------------------]]

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

local M = {}

-- Optional external subtab modules (used if present)
local HPGeneralSubTab = HPGeneralSubTab or _G.HPGeneralSubTab
local WastedHPSubTab  = WastedHPSubTab  or _G.WastedHPSubTab
local TotalHPSubTab   = TotalHPSubTab   or _G.TotalHPSubTab

-- Fallback builder for "General" if no external module is registered.
-- Keeps things functional without adding new files.
local function BuildGeneralFallback(panel, PHP, hpSettings)
  -- simple label so the tab isn't empty; replace later if you add HPGeneralSubTab
  local title = panel:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
  title:SetPoint("TOPLEFT", 8, -8)

  -- If Theme supports gradient, show a nice header; else plain text
  local Theme = PHP and PHP.Theme
  if Theme and Theme.ApplyGradientText then
    Theme.ApplyGradientText(title, "General", nil, nil, { outline = true })
  else
    title:SetText("General")
  end

  local note = panel:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
  note:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -8)
  note:SetText("Place your Holy Power general settings here (layout, visibility, behaviors).")
end

function M.Build(parent, PHP)
  -- Safely fetch settings subtables (never nil)
  local HPLSettings = (PHP.settings and PHP.settings.HPL) or {}
  local hpSettings  = (PHP.settings and PHP.settings.HP)  or {}

  -- Subtabs (now includes 'General' first)
  local subTabNames = { "General", "HP Log", "HP Bar" }
  local buildFns = {
    -- General
    function(panel)
      if HPGeneralSubTab and HPGeneralSubTab.Build then
        HPGeneralSubTab.Build(panel, PHP, hpSettings)
      else
        BuildGeneralFallback(panel, PHP, hpSettings)
      end
    end,
    -- HP Log (was WastedHPSubTab)
    function(panel)
      if WastedHPSubTab and WastedHPSubTab.Build then
        WastedHPSubTab.Build(panel, PHP, HPLSettings)
      end
    end,
    -- HP Bar (was TotalHPSubTab)
    function(panel)
      if TotalHPSubTab and TotalHPSubTab.Build then
        TotalHPSubTab.Build(panel, PHP, hpSettings)
      end
    end,
  }

  -- Place the subtab bar so its top aligns with the sidebar's first tab ("Home"):
  -- Sidebar tabs use TOP offset -8, so we mirror that here with padTop = 8.
  local subTabBar = PHP.CreateSubTabs(parent, subTabNames, buildFns, {
    padLeft = 8,   -- match sidebar tab's 8px inset
    padTop  = 8,   -- align vertically with the "Home" sidebar tab row
  })

  -- Nothing else to do here; CreateSubTabs handles showing first tab, etc.
end

HolyPowerTab = M
_G.HolyPowerTab = M

return M