--[[
============================================================================
ETB Prototype - MemoryProfiler.lua
============================================================================
Memory profiling for development.
============================================================================
]]--

local ADDON_NAME, NS = ...
local ETB = NS.ETB

ETB.MemoryProfiler = {
    tracking = false,
    samples = {},
    baseline = nil,
}

local MP = ETB.MemoryProfiler

function MP:GetMemory()
    UpdateAddOnMemoryUsage()
    return GetAddOnMemoryUsage(ADDON_NAME)
end

function MP:Start()
    self.tracking = true
    self.baseline = self:GetMemory()
    wipe(self.samples)
    table.insert(self.samples, { time = GetTime(), mem = self.baseline, label = "Start" })
    if ETB.DebugSystem then
        ETB.DebugSystem:Info("MemProfiler", "Started (baseline: %.1f KB)", self.baseline)
    else
        ETB:Debug("MemProfiler", "Started (baseline: %.1f KB)", self.baseline)
    end
end

function MP:Stop()
    self.tracking = false
    local current = self:GetMemory()
    table.insert(self.samples, { time = GetTime(), mem = current, label = "Stop" })
    local delta = current - self.baseline
    if ETB.DebugSystem then
        ETB.DebugSystem:Info("MemProfiler", "Stopped (delta: %+.1f KB)", delta)
    else
        ETB:Debug("MemProfiler", "Stopped (delta: %+.1f KB)", delta)
    end
    self:Report()
end

function MP:Sample(label)
    if not self.tracking then return end
    table.insert(self.samples, { time = GetTime(), mem = self:GetMemory(), label = label })
end

function MP:Report()
    if #self.samples < 2 then
        print("|cffff0000No samples recorded|r")
        return
    end
    
    print("|cff00ff00=== Memory Report ===|r")
    local prev = nil
    for i, s in ipairs(self.samples) do
        local delta = prev and string.format(" (%+.1f)", s.mem - prev.mem) or ""
        print(string.format("  %s: %.1f KB%s", s.label or ("Sample " .. i), s.mem, delta))
        prev = s
    end
    
    local total = self.samples[#self.samples].mem - self.samples[1].mem
    local duration = self.samples[#self.samples].time - self.samples[1].time
    print(string.format("|cffffff00Total: %+.1f KB over %.1f sec|r", total, duration))
end

SLASH_ETBMEM1 = "/etbmem"
SlashCmdList["ETBMEM"] = function(msg)
    local cmd = msg:lower()
    if cmd == "start" then
        MP:Start()
    elseif cmd == "stop" then
        MP:Stop()
    elseif cmd == "now" then
        local mem = MP:GetMemory()
        print(string.format("|cff00ff00[ETB]|r Memory: %.1f KB (%.2f MB)", mem, mem/1024))
    elseif cmd == "gc" then
        local before = collectgarbage("count")
        collectgarbage("collect")
        local after = collectgarbage("count")
        print(string.format("|cff00ff00[ETB]|r GC freed: %.1f KB", before - after))
    else
        print("|cff00ff00=== Memory Profiler ===|r")
        print("/etbmem start - Start tracking")
        print("/etbmem stop - Stop and report")
        print("/etbmem now - Current memory")
        print("/etbmem gc - Force GC")
    end
end

if ETB.DebugSystem then
    ETB.DebugSystem:Info("MemProfiler", "Memory Profiler loaded")
else
    ETB:Debug("MemProfiler", "Memory Profiler loaded")
end
