local ADDON_NAME, RPAL = ...
if type(RPAL)~="table" then return end

-- ============================================================
-- TEMPORARY RELEASE QA COMMANDS
-- ============================================================
-- These are intentionally presentation-only. They invoke the same runtime UI
-- and audio functions as real events but never award Renown, levels, rewards,
-- achievements or other persistent character data.

local function CurrentLevel()
    if RPAL.GetCurrentRenownLevel then
        local ok,level=pcall(function() return select(1,RPAL.GetCurrentRenownLevel()) end)
        if ok and tonumber(level) then return math.max(1,math.floor(level)) end
    end
    return 1
end

local function RewardsForLevel(level)
    local out={}
    for _,reward in ipairs(type(RPAL.LEVEL_REWARDS)=="table" and RPAL.LEVEL_REWARDS or {}) do
        if tonumber(reward.level)==tonumber(level) and reward.name then
            out[#out+1]={level=tonumber(reward.level),kind=reward.kind,name=reward.name}
        end
    end
    return out
end

local function Help()
    print("|cff66e6ffODYN QA PRESENTATION TESTS|r  |cffaaaaaa(no progression data is changed)|r")
    print("|cffffffff/odyn test mention|r — Simulate a real Hover Log name-mention alert")
    print("|cffffffff/odyn test emote|r — Simulate a directed /nod, /peer, /shy style alert")
    print("|cffffffff/odyn test achievement|r — Show an achievement-unlock toast + configured sound")
    print("|cffffffff/odyn test levelup|r — Show the next-level toast + configured sound")
    print("|cffffffff/odyn test levelup 25|r — Preview a specific level number")
    print("|cffffffff/odyn test renown 30|r — Run 30 visible +1 pickups, Eye loop/pulses and every-3-Renown audio")
    print("|cffffffff/odyn test eye|r — Legacy alias for a 75-Renown flow")
    print("|cffffffff/odyn test all|r — Run the main notification tests in sequence")
end

local function TestMention()
    if not RPAL.TestHoverLogAlert then return false,"Hover Log test hook is not ready." end
    local ok,detail=RPAL.TestHoverLogAlert("mention")
    return ok,detail or (ok and "Name mention notification simulated." or "Name mention test failed.")
end

local function TestEmote()
    if not RPAL.TestHoverLogAlert then return false,"Hover Log test hook is not ready." end
    local ok,detail=RPAL.TestHoverLogAlert("target")
    return ok,detail or (ok and "Directed emote notification simulated." or "Directed emote test failed.")
end

local function TestAchievement()
    if not RPAL.ShowAchievementUnlockedToast then return false,"Achievement toast system is not ready." end
    if RPAL.ResetToastDirector then RPAL.ResetToastDirector() end
    RPAL.ShowAchievementUnlockedToast("Test Achievement — Story Worth Remembering")
    return true,"Achievement unlock presentation triggered; nothing was unlocked or saved."
end

local function TestLevelUp(requested)
    if not RPAL.OnRenownLevelUp then return false,"Level-up presentation system is not ready." end
    local current=CurrentLevel()
    local level=tonumber(requested)
    if level then level=math.max(2,math.min(999,math.floor(level))) else level=current+1 end
    local oldLevel=math.max(1,level-1)
    local rewards=RewardsForLevel(level)
    if RPAL.ResetToastDirector then RPAL.ResetToastDirector() end
    RPAL.OnRenownLevelUp(level,oldLevel,"qa_test",rewards)
    return true,"Level "..tostring(level).." presentation triggered; your real level and rewards were not changed."
end

local function TestRenown(requested)
    local amount=tonumber(requested) or 75
    amount=math.max(1,math.min(2000,math.floor(amount)))
    if not RPAL.TestRenownEyeFX then return false,"Renown collection FX module is not ready." end
    if RPAL.ResetRenownGainSoundCycle then RPAL.ResetRenownGainSoundCycle() end
    local ok=RPAL.TestRenownEyeFX(amount)
    if not ok then return false,"Player Frame Eye is hidden, or Renown FX is not ready." end
    return true,tostring(amount).."-Renown +1 collection flow started. No Renown was awarded."
end


local function RunAll()
    TestMention()
    if not (C_Timer and C_Timer.After) then
        TestEmote();TestAchievement();TestLevelUp();TestRenown(15)
        return true,"All presentation tests triggered."
    end
    C_Timer.After(1.25,function() TestEmote() end)
    C_Timer.After(2.55,function() TestAchievement() end)
    C_Timer.After(5.20,function() TestLevelUp() end)
    C_Timer.After(9.10,function() TestRenown(15) end)
    return true,"Full QA sequence started: mention → emote → achievement → level up → 15 Renown."
end

function RPAL.RunQATest(args)
    args=tostring(args or ""):lower():gsub("^%s+",""):gsub("%s+$","")
    if args=="" or args=="help" or args=="?" then Help();return true,nil end

    local command,rest=args:match("^(%S+)%s*(.-)$")
    command=command or ""
    if command=="mention" or command=="name" then return TestMention() end
    if command=="emote" or command=="target" or command=="interaction" or command=="emoted" then return TestEmote() end
    if command=="achievement" or command=="ach" or command=="achievementunlock" then return TestAchievement() end
    if command=="levelup" or command=="level" then return TestLevelUp(rest) end
    if command=="renown" or command=="renowneye" or command=="eye" then
        if command=="eye" and (rest==nil or rest=="") then rest="75" end
        return TestRenown(rest)
    end
    if command=="all" or command=="sounds" then return RunAll() end

    Help()
    return false,"Unknown test '"..tostring(command).."'."
end
