-- ══════════════════════════════════════════════════════════════
-- MODUL: Anniversary - Gedenktag-Erinnerungen
-- Zeigt Todestage von Gildenmitgliedern im Chat an
-- ══════════════════════════════════════════════════════════════

local addonName, addon = ...
local GDL = _G["GuildDeathLog"]
local Anniversary = {}

local shown = {}

function Anniversary:CheckAnniversaries()
    if not GDL.currentGuildName then return end
    local guildData = GuildDeathLogDB and GuildDeathLogDB.guilds and GuildDeathLogDB.guilds[GDL.currentGuildName]
    if not guildData or not guildData.deaths then return end
    
    local now = time()
    local today = date("*t", now)
    
    for _, death in pairs(guildData.deaths) do
        if death.timestamp and death.name then
            local deathDate = date("*t", death.timestamp)
            if deathDate.month == today.month and deathDate.day == today.day then
                local years = today.year - deathDate.year
                if years > 0 and not shown[death.name .. years] then
                    shown[death.name .. years] = true
                    local className = GDL.GetClassName and GDL:GetClassName(death.classId) or ""
                    local unit = years == 1 and "Jahr" or "Jahren"
                    local zone = death.zone or "unbekannt"
                    GDL:Print("|cff8A6A20[Gedenktag]|r Heute vor |cffFFD100" .. years .. " " .. unit .. "|r fiel |cffFF6B6B" .. death.name .. "|r (" .. className .. ") in |cff69CCF0" .. zone .. "|r. Ruhe in Frieden.")
                end
            end
            
            local daysSince = math.floor((now - death.timestamp) / 86400)
            if (daysSince == 7 or daysSince == 30 or daysSince == 100) and not shown[death.name .. "d" .. daysSince] then
                shown[death.name .. "d" .. daysSince] = true
                local label = daysSince .. " Tage"
                GDL:Print("|cff8A6A20[Gedenken]|r Vor " .. label .. " fiel |cffFF6B6B" .. death.name .. "|r in " .. (death.zone or "unbekannt") .. ".")
            end
        end
    end
end

function Anniversary:Initialize()
    if Anniversary._initialized then return end
    Anniversary._initialized = true
    C_Timer.After(15, function()
        self:CheckAnniversaries()
    end)
end

GDL:RegisterModule("Anniversary", Anniversary)
