-- KiraUI-Plugin_SigilDiag.lua
-- Throwaway diagnostic for the planned VDH "Sigil of Flame dot counter".
--
-- The question it answers: is the Sigil of Flame DoT on ENEMIES readable via
-- the spellID-lookup API in combat, or is it secret-flagged (returns nothing)?
-- Working rules 1.3: GetUnitAuraBySpellID returns plain data for NON-secret
-- auras and plain NOTHING for secret ones - so "0 FOUND" while the dot is
-- visibly ticking on the pack = secret-flagged = no counter in that content.
--
-- Usage: dot a pack with Sigil of Flame, then /sigiltest. Run it:
--   1) out of combat, open world
--   2) in combat, open world
--   3) in combat, dungeon / M+
-- and report the three outputs. Delete this file once the verdict is in.

local addonName, ns = ...
local _G = _G

local UnitExists = UnitExists
local UnitCanAttack = UnitCanAttack
local InCombatLockdown = InCombatLockdown
local IsInInstance = IsInInstance
local issecretvalue = issecretvalue
local ipairs = ipairs

local GetUnitAuraBySpellID = _G.C_UnitAuras and _G.C_UnitAuras.GetUnitAuraBySpellID

-- debuff id first; cast id second in case the dot rides the cast id
local SIGIL_IDS = { 204598, 204596 }

local function secret(v) return issecretvalue and issecretvalue(v) end

-- 'FOUND' (readable dot), 'SECRET-OBJ' (unexpected secret return - report!),
-- or 'nothing' (no dot readable - NOT proof of absence in combat)
local function Probe(unit)
	for _, id in ipairs(SIGIL_IDS) do
		local aura = GetUnitAuraBySpellID(unit, id, 'HARMFUL|PLAYER')
		if secret(aura) then return 'SECRET-OBJ', id end
		if aura then return 'FOUND', id end
	end
	return 'nothing', nil
end

SLASH_KIRASIGILTEST1 = '/sigiltest'
_G.SlashCmdList.KIRASIGILTEST = function()
	local function p(...) print('|cffA330C9KiraUI-SigilTest|r', ...) end
	if not GetUnitAuraBySpellID then
		p('C_UnitAuras.GetUnitAuraBySpellID missing on this client - nothing to test.')
		return
	end

	local inInst, instType = IsInInstance()
	p(('context: combat=%s | instance=%s (%s) | build=%s'):format(
		InCombatLockdown() and 'YES' or 'no',
		inInst and 'yes' or 'no', tostring(instType),
		tostring(select(4, _G.GetBuildInfo()))))

	local plates, found = 0, 0
	for i = 1, 40 do
		local unit = 'nameplate' .. i
		if UnitExists(unit) then
			local can = UnitCanAttack('player', unit)
			if secret(can) or can then -- restricted read: keep the unit in
				plates = plates + 1
				local res, id = Probe(unit)
				if res ~= 'nothing' then
					found = found + 1
					p(('  %s: %s (id %s)'):format(unit, res, tostring(id)))
				end
			end
		end
	end
	if UnitExists('target') then
		local res, id = Probe('target')
		p(('  target: %s%s'):format(res, id and (' (id ' .. id .. ')') or ''))
	end
	p(('scanned %d hostile nameplates, Sigil dot FOUND on %d.'):format(plates, found))
	p('Reading the result: dots you can SEE ticking but 0 FOUND here = aura is secret in this content, counter impossible there. FOUND matching reality = counter viable here.')
end
