-- ══════════════════════════════════════════════════════════════
-- MODUL: Report - Woechentlicher Gildenbericht
-- ══════════════════════════════════════════════════════════════

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

function Report:GenerateWeeklyReport()
    if not GDL.currentGuildName then
        GDL:Print("|cffFF6666" .. GDL:T("Keine Gilde gefunden.") .. "|r")
        return
    end
    local guildData = GuildDeathLogDB and GuildDeathLogDB.guilds and GuildDeathLogDB.guilds[GDL.currentGuildName]
    if not guildData or not guildData.deaths then
        GDL:Print("|cffFF6666Keine Daten vorhanden.|r")
        return
    end
    
    local now = time()
    local weekAgo = now - (7 * 86400)
    local monthAgo = now - (30 * 86400)
    
    local weekDeaths = 0
    local monthDeaths = 0
    local totalDeaths = 0
    local classCounts = {}
    local zoneCounts = {}
    local highestLevel = 0
    local highestName = ""
    local recentDeath = nil
    
    for _, death in pairs(guildData.deaths) do
        totalDeaths = totalDeaths + 1
        
        if death.timestamp and death.timestamp >= weekAgo then
            weekDeaths = weekDeaths + 1
        end
        if death.timestamp and death.timestamp >= monthAgo then
            monthDeaths = monthDeaths + 1
        end
        
        if death.classId then
            classCounts[death.classId] = (classCounts[death.classId] or 0) + 1
        end
        if death.zone and death.zone ~= "" then
            zoneCounts[death.zone] = (zoneCounts[death.zone] or 0) + 1
        end
        
        if death.level and death.level > highestLevel then
            highestLevel = death.level
            highestName = death.name or "?"
        end
        
        if not recentDeath or (death.timestamp and death.timestamp > (recentDeath.timestamp or 0)) then
            recentDeath = death
        end
    end
    
    local deadliestZone, deadliestCount = "?", 0
    for zone, count in pairs(zoneCounts) do
        if count > deadliestCount then
            deadliestZone = zone
            deadliestCount = count
        end
    end
    
    local deadliestClass, deadliestClassCount = 0, 0
    for classId, count in pairs(classCounts) do
        if count > deadliestClassCount then
            deadliestClass = classId
            deadliestClassCount = count
        end
    end
    
    GDL:Print("|cff8A6A20" .. GDL:T("═══ WOECHENTLICHER GILDENBERICHT ═══") .. "|r")
    GDL:Print("|cffBB9944" .. GDL:T("Gilde:") .. "|r " .. GDL.currentGuildName)
    GDL:Print("|cffBB9944" .. GDL:T("Zeitraum:") .. "|r " .. GDL:T("Letzte 7 Tage"))
    GDL:Print("---")
    GDL:Print("|cffFFD100" .. GDL:T("Tode diese Woche:") .. "|r " .. weekDeaths)
    GDL:Print("|cffFFD100" .. GDL:T("Tode diesen Monat:") .. "|r " .. monthDeaths)
    GDL:Print("|cffFFD100" .. GDL:T("Tode gesamt:") .. "|r " .. totalDeaths)
    GDL:Print("---")
    GDL:Print("|cffFF6B6B" .. GDL:T("Todesfalle Nr. 1:") .. "|r " .. deadliestZone .. " (" .. deadliestCount .. " " .. GDL:T("Tode") .. ")")
    if deadliestClass > 0 then
        local className = GDL.GetClassName and GDL:GetClassName(deadliestClass) or (GDL:T("Klasse ") .. deadliestClass)
        GDL:Print("|cffFF6B6B" .. GDL:T("Gefaehrdetste Klasse:") .. "|r " .. className .. " (" .. deadliestClassCount .. " " .. GDL:T("Tode") .. ")")
    end
    GDL:Print("|cffFF6B6B" .. GDL:T("Hoechster Gefallener:") .. "|r " .. highestName .. " (Level " .. highestLevel .. ")")
    if recentDeath then
        local ago = math.floor((now - (recentDeath.timestamp or now)) / 3600)
        local timeStr = ago < 1 and GDL:T("gerade eben") or (ago .. GDL:T("h her"))
        GDL:Print("|cffFFCC00" .. GDL:T("Letzter Tod:") .. "|r " .. (recentDeath.name or "?") .. " in " .. (recentDeath.zone or "?") .. " (" .. timeStr .. ")")
    end
    GDL:Print("|cff8A6A20════════════════════════════════════|r")
end

function Report:Initialize()
    if Report._initialized then return end
    Report._initialized = true
    -- Automatischer Wochenbericht: einmal pro Woche beim ersten Login
    C_Timer.After(12, function()
        if not GDL.currentGuildName then return end
        GuildDeathLogDB = GuildDeathLogDB or {}
        local lastReport = GuildDeathLogDB.lastWeeklyReport or 0
        local weekAgo    = time() - 604800
        if lastReport < weekAgo then
            self:GenerateWeeklyReport()
            GuildDeathLogDB.lastWeeklyReport = time()
            GDL:Debug("Report: Automatischer Wochenbericht ausgegeben")
        end
    end)
end

GDL:RegisterModule("Report", Report)
