-- ══════════════════════════════════════════════════════════════
-- 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 zone = death.zone or (GDL:WantEnglish() and "unknown" or "unbekannt")
                    if GDL:WantEnglish() then
                        local unit = years == 1 and "year" or "years"
                        GDL:Print("|cff8A6A20[Anniversary]|r Today, |cffFFD100" .. years .. " " .. unit .. "|r ago, |cffFF6B6B" .. death.name .. "|r (" .. className .. ") fell in |cff69CCF0" .. zone .. "|r. Rest in peace.")
                    else
                        local unit = years == 1 and "Jahr" or "Jahren"
                        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
            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 zone2 = death.zone or (GDL:WantEnglish() and "unknown" or "unbekannt")
                if GDL:WantEnglish() then
                    GDL:Print("|cff8A6A20[In Memory]|r |cffFF6B6B" .. death.name .. "|r fell " .. daysSince .. " days ago in " .. zone2 .. ".")
                else
                    GDL:Print("|cff8A6A20[Gedenken]|r Vor " .. daysSince .. " Tagen fiel |cffFF6B6B" .. death.name .. "|r in " .. zone2 .. ".")
                end
            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)
