--[[
============================================================================
ETB Prototype - TestHarness.lua
============================================================================
Quick validation tests for addon functionality.
============================================================================
]]--

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

ETB.TestHarness = {
    tests = {},
    results = {},
}

local TH = ETB.TestHarness

function TH:Register(name, fn)
    self.tests[name] = fn
end

function TH:Run(name)
    local fn = self.tests[name]
    if not fn then return nil, "Test not found" end
    local ok, result, msg = pcall(fn)
    if not ok then return false, "Crashed: " .. tostring(result) end
    return result, msg
end

function TH:RunAll()
    local passed, failed, skipped = 0, 0, 0
    
    print("|cff00ff00=== ETB Test Harness ===|r")
    
    for name, _ in pairs(self.tests) do
        local result, msg = self:Run(name)
        local icon = result == true and "✅" or result == false and "❌" or "⚠️"
        local color = result == true and "|cff00ff00" or result == false and "|cffff0000" or "|cffffff00"
        
        print(string.format("  %s %s%s|r: %s", icon, color, name, msg or ""))
        
        if result == true then passed = passed + 1
        elseif result == false then failed = failed + 1
        else skipped = skipped + 1 end
        
        self.results[name] = { result = result, msg = msg }
    end
    
    print(string.format("|cff00ff00Passed:|r %d  |cffff0000Failed:|r %d  |cffffff00Skipped:|r %d", 
        passed, failed, skipped))
end

-- Register default tests
C_Timer.After(1, function()
    TH:Register("API_TransmogCollection", function()
        if not C_TransmogCollection then return false, "Not found" end
        return true, "Available"
    end)
    
    TH:Register("API_TransmogSets", function()
        if not C_TransmogSets then return nil, "Not available (optional)" end
        local sets = C_TransmogSets.GetAllSets()
        return true, string.format("%d sets", sets and #sets or 0)
    end)
    
    TH:Register("ETB_BlizzardData", function()
        if not ETB.BlizzardData then return false, "Not loaded" end
        return true, "Available"
    end)
    
    TH:Register("ETB_Filtering", function()
        if not ETB.Filtering then return false, "Not loaded" end
        return true, "Available"
    end)
    
    TH:Register("ETB_MainFrame", function()
        if not ETB.MainFrame then return false, "Not loaded" end
        return true, "Available"
    end)
    
    TH:Register("Memory_Check", function()
        UpdateAddOnMemoryUsage()
        local mem = GetAddOnMemoryUsage(ADDON_NAME)
        if mem > 10000 then return false, string.format("%.1f MB (too high)", mem/1024) end
        if mem > 5000 then return nil, string.format("%.1f MB (above 5MB target)", mem/1024) end
        return true, string.format("%.1f KB (OK)", mem)
    end)
    
    if ETB.DebugSystem then
        ETB.DebugSystem:Info("TestHarness", "Test Harness loaded")
    else
        ETB:Debug("TestHarness", "Test Harness loaded")
    end
end)

SLASH_ETBTEST1 = "/etbtest"
SlashCmdList["ETBTEST"] = function(msg)
    local cmd = msg:lower()
    if cmd == "" or cmd == "all" then
        TH:RunAll()
    elseif cmd == "list" then
        print("|cff00ff00=== Available Tests ===|r")
        for name, _ in pairs(TH.tests) do
            print("  - " .. name)
        end
    elseif TH.tests[msg] then
        local result, msg = TH:Run(msg)
        local icon = result == true and "✅" or result == false and "❌" or "⚠️"
        print(string.format("%s %s: %s", icon, msg, msg or ""))
    else
        print("|cff00ff00=== ETB Tests ===|r")
        print("/etbtest - Run all tests")
        print("/etbtest list - List tests")
        print("/etbtest <name> - Run specific test")
    end
end
