local ADDON_NAME, ns = ...
local WF = ns.WF

WF.UI = WF.UI or {}
WF.UI.HelpPages = WF.UI.HelpPages or {}
WF.UI.HelpPages.OverviewFarmTimer = WF.UI.HelpPages.OverviewFarmTimer or {}

local Modules = WF.UI.HelpPages.OverviewFarmTimer

function Modules.EnsureMessages(ctx, env, FT)
  if not FT then return nil end
  if FT._messagesInited then return FT end
  FT._messagesInited = true

  local C4 = env.C4
  local TEXT = env.TEXT
  local ACCENT = env.ACCENT

function FT:_RaidWarn(msg)
  msg = tostring(msg or "")
  if msg == "" then return end
  
  local ar, ag, ab = C4(ACCENT, TEXT)
  
  if RaidNotice_AddMessage and RaidWarningFrame then
    RaidNotice_AddMessage(RaidWarningFrame, msg, { r = ar, g = ag, b = ab }, 1.2)
  end
  
  if PlaySound and SOUNDKIT and SOUNDKIT.RAID_WARNING then
    pcall(PlaySound, SOUNDKIT.RAID_WARNING, "Master")
  end
end
  
function FT:_GroupAnnounce(msg)
  if not (FT.cfg and FT.cfg.party) then return end
  if not SendChatMessage then return end
  
  msg = tostring(msg or "")
  if msg == "" then return end
  
  local inRaid = (IsInRaid and IsInRaid()) and true or false
  local inGroup = (IsInGroup and IsInGroup()) and true or false
  if not (inRaid or inGroup) then return end
  
  local channels = {}
  if inRaid then
    local isLead = (UnitIsGroupLeader and UnitIsGroupLeader("player")) or false
    local isAssist = (UnitIsGroupAssistant and UnitIsGroupAssistant("player")) or false
    if isLead or isAssist then
      channels[#channels + 1] = "RAID_WARNING"
    end
    channels[#channels + 1] = "RAID"
  elseif inGroup then
    channels[#channels + 1] = "PARTY"
  end
  
  if #channels <= 0 then return end
  
  local attempts = 2
  local delays = { 0.00, 0.75 }
  
  local function _Send(ch)
    for i = 1, #ch do
      pcall(SendChatMessage, msg, ch[i])
    end
  end
  
  _Send(channels)
  
  for i = 2, attempts do
    local delay = delays[i] or ((i - 1) * 0.75)
    if C_Timer and C_Timer.After then
      C_Timer.After(delay, function()
        if not (FT and FT.cfg and FT.cfg.party) then return end
  
        local stillInRaid = (IsInRaid and IsInRaid()) and true or false
        local stillInGroup = (IsInGroup and IsInGroup()) and true or false
        if not (stillInRaid or stillInGroup) then return end
  
        local stillChannels = {}
        if stillInRaid then
          local stillLead = (UnitIsGroupLeader and UnitIsGroupLeader("player")) or false
          local stillAssist = (UnitIsGroupAssistant and UnitIsGroupAssistant("player")) or false
          if stillLead or stillAssist then
            stillChannels[#stillChannels + 1] = "RAID_WARNING"
          end
          stillChannels[#stillChannels + 1] = "RAID"
        elseif stillInGroup then
          stillChannels[#stillChannels + 1] = "PARTY"
        end
  
        if #stillChannels > 0 then
          _Send(stillChannels)
        end
      end)
    end
  end
end
  
-- Human-friendly announce strings (raid warning + optional party announce)
function FT:_PhaseStartMsg(phase)
  phase = tostring(phase or "Kill")
  if phase == "Kill" then return "WhatFarm: Start Killing" end
  if phase == "Loot" then return "WhatFarm: Start Looting" end
  if phase == "Grace" then return "WhatFarm: Reset" end
  return "WhatFarm: " .. phase
end
  
function FT:_PhaseWarnMsg(phase, sec)
  phase = tostring(phase or "")
  sec = tonumber(sec) or 0
  
  if phase == "Kill" then return "WhatFarm: Killing ends in " .. sec .. "s" end
  if phase == "Loot" then return "WhatFarm: Looting ends in " .. sec .. "s" end
  if phase == "Grace" then return "WhatFarm: Reset ends in " .. sec .. "s" end
  
  if phase == "" then
    return "WhatFarm: " .. sec .. "s"
  end
  
  return "WhatFarm: " .. phase .. " ends in " .. sec .. "s"
end

  return FT
end
