local _, ns = ...

ns.Alerts = {}
local Alerts = ns.Alerts

local soundCVars = {
    "Sound_MasterVolume",
    "Sound_SFXVolume",
    "Sound_EnableAllSound",
    "Sound_EnableSFX",
    "Sound_MusicVolume",
    "Sound_AmbienceVolume",
}
local previous = {}

function Alerts:EnhanceSounds(enable)
    if not (ns.DB and ns.DB.profile.alerts.enhanceSounds) then return end
    if enable then
        for _, cvar in ipairs(soundCVars) do previous[cvar] = GetCVar(cvar) end
        SetCVar("Sound_EnableAllSound", 1)
        SetCVar("Sound_EnableSFX", 1)
        SetCVar("Sound_MasterVolume", 1)
        SetCVar("Sound_SFXVolume", math.max(0.2, tonumber(ns.DB.profile.alerts.enhanceSoundsScale) or 1))
        SetCVar("Sound_MusicVolume", 0)
        SetCVar("Sound_AmbienceVolume", 0)
    else
        for cvar, value in pairs(previous) do SetCVar(cvar, value) end
    end
end

function Alerts:PlayBiteAlert()
    if ns.DB and ns.DB.profile.alerts.soundOnBite then
        if PlaySound then PlaySound(SOUNDKIT and SOUNDKIT.TELL_MESSAGE or 3081, "Master") end
    end
end

function Alerts:PlayRareAlert(itemLink)
    if ns.DB and ns.DB.profile.alerts.flashOnRare and UIFrameFlash then
        local target = nil
        if ns.state and ns.state.frame and ns.state.frame:IsShown() then
            target = ns.state.frame.header or ns.state.frame
        elseif ns.state and ns.state.compactFrame and ns.state.compactFrame:IsShown() then
            target = ns.state.compactFrame
        end
        if target then
            UIFrameFlash(target, 0.15, 0.15, 0.7, false, 0, 0)
        end
    end
    self:PlayBiteAlert()
    ns.Utils:Print(string.format("Rare catch recorded: %s", itemLink or "Unknown"))
end

function Alerts:NotifyMissingLure()
    if not (ns.DB and ns.DB.profile.alerts.lureReminder) then return end
    local now = GetTime and GetTime() or 0
    if (ns.state.lastLureReminderAt or 0) + 45 > now then return end
    ns.state.lastLureReminderAt = now
    ns.Utils:Print("No fishing lure is active.")
end

function Alerts:CheckPatientChest()
    if not (ns.DB and ns.DB.profile.alerts.patientChestAlert) then return end
    if not C_UnitAuras then return end
    local found = C_UnitAuras.GetPlayerAuraBySpellID(1269521) or C_UnitAuras.GetPlayerAuraBySpellID(1235378)
    if not found then return end
    local now = GetTime and GetTime() or 0
    if (ns.state.lastPatientChestAt or 0) + 30 > now then return end
    ns.state.lastPatientChestAt = now
    local target = nil
    if ns.state and ns.state.frame and ns.state.frame:IsShown() then
        target = ns.state.frame.header or ns.state.frame
    elseif ns.state and ns.state.compactFrame and ns.state.compactFrame:IsShown() then
        target = ns.state.compactFrame
    end
    if target and UIFrameFlash then UIFrameFlash(target, 0.2, 0.2, 1.0, false, 0, 0) end
    ns.Utils:Print("|cFFFFD700Patient Chest nearby!|r Check your surroundings for a Patient Chest.")
end
