--[[
    Cerebro - Command Registry: Testing Commands
    Extracted from UI/CommandRegistry.lua (Phase 2a decomposition)

    Commands:
      test_ui         (original lines 557-570)
      test_prep       (original lines 571-584)
      test_rclc       (original lines 585-598)
      test_calc       (original lines 599-612)
      test_decay      (original lines 613-626)
      test_ledger     (original lines 627-640)
      test_session    (original lines 641-654)
      test_mplus      (original lines 655-712)
      test_audit      (original lines 713-726)
      test_diag       (original lines 727-740)
      test_env        (original lines 741-789)

    @module UI.Commands.Testing
    @version 0.12.0
]]--

local AddonName, PrivateTable = ...
local Cerebro = LibStub("AceAddon-3.0"):GetAddon(AddonName)

Cerebro._commandDefs = Cerebro._commandDefs or {}
local defs = Cerebro._commandDefs

-- Testing Commands
defs[#defs + 1] = {
	id = "test_ui",
	displayName = "Run UI Tests",
	description = "Executes UI module unit tests",
	officerOnly = false,
	slash = "/testui",
	handler = function(ctx)
		if SlashCmdList["TESTUI"] then
			SlashCmdList["TESTUI"]()
		else
			Cerebro:Print("UI tests not loaded.")
		end
	end
}

defs[#defs + 1] = {
	id = "test_prep",
	displayName = "Run Prep Tests",
	description = "Executes Preparation module unit tests",
	officerOnly = false,
	slash = "/testprep",
	handler = function(ctx)
		if SlashCmdList["TESTPREP"] then
			SlashCmdList["TESTPREP"]()
		else
			Cerebro:Print("Prep tests not loaded.")
		end
	end
}

defs[#defs + 1] = {
	id = "test_rclc",
	displayName = "Run RCLC Tests",
	description = "Executes RCLC Bridge unit tests",
	officerOnly = false,
	slash = "/testrclc",
	handler = function(ctx)
		if SlashCmdList["TESTRCLC"] then
			SlashCmdList["TESTRCLC"]()
		else
			Cerebro:Print("RCLC tests not loaded.")
		end
	end
}

defs[#defs + 1] = {
	id = "test_calc",
	displayName = "Run Calc Tests",
	description = "Executes Player calculation unit tests",
	officerOnly = false,
	slash = "/testcalc",
	handler = function(ctx)
		if SlashCmdList["TESTCALC"] then
			SlashCmdList["TESTCALC"]()
		else
			Cerebro:Print("Calc tests not loaded.")
		end
	end
}

defs[#defs + 1] = {
	id = "test_decay",
	displayName = "Run Decay Tests",
	description = "Executes Decay unit tests",
	officerOnly = false,
	slash = "/testdecay",
	handler = function(ctx)
		if SlashCmdList["TESTDECAY"] then
			SlashCmdList["TESTDECAY"]()
		else
			Cerebro:Print("Decay tests not loaded.")
		end
	end
}

defs[#defs + 1] = {
	id = "test_ledger",
	displayName = "Run Ledger Tests",
	description = "Executes Ledger unit tests",
	officerOnly = false,
	slash = "/testledger",
	handler = function(ctx)
		if SlashCmdList["TESTLEDGER"] then
			SlashCmdList["TESTLEDGER"]()
		else
			Cerebro:Print("Ledger tests not loaded.")
		end
	end
}

defs[#defs + 1] = {
	id = "test_session",
	displayName = "Run Session Tests",
	description = "Executes Session unit tests",
	officerOnly = false,
	slash = "/testsession",
	handler = function(ctx)
		if SlashCmdList["TESTSESSION"] then
			SlashCmdList["TESTSESSION"]()
		else
			Cerebro:Print("Session tests not loaded.")
		end
	end
}

defs[#defs + 1] = {
	id = "test_mplus",
	displayName = "Run M+ Tests",
	description = "Generate test M+ runs. Use --live to award EP, or 'target' to test sync.",
	officerOnly = false,
	slash = "/cb testmplus [count] [--live] OR /cb testmplus target Player-Realm [--live]",
	handler = function(ctx, argv)
		local subCmd = argv[1] and argv[1]:lower()

		-- Target mode: /cb testmplus target Player-Realm [--live]
		if subCmd == "target" then
			-- Dev gate: only in dev mode
			if not (SlashCmdList["TESTMPLUS"] or Cerebro._TEST_HARNESS_LOADED) then
				Cerebro:Print("|cffff0000Error:|r target mode requires dev deploy (--with-tests).")
				return
			end

			-- RBAC: Officer only
			if not Cerebro:IsOfficer() then
				Cerebro:Print("|cffff0000Error:|r target mode is officer-only.")
				return
			end

			local targetPlayer = argv[2]
			local isLive = false
			for i = 3, #argv do
				if argv[i] == "--live" then isLive = true end
			end

			local mplus = Cerebro:GetModule("MythicPlus", true)
			if mplus and mplus.AwardTargetEP then
				mplus:AwardTargetEP(targetPlayer, isLive)
			else
				Cerebro:Print("M+ module not loaded.")
			end
			return
		end

		-- Standard mode: /cb testmplus [count] [--live]
		local count = 1
		local isLive = false

		for i = 1, #argv do
			if argv[i] == "--live" then
				isLive = true
			elseif tonumber(argv[i]) then
				count = tonumber(argv[i])
			end
		end

		local mplus = Cerebro:GetModule("MythicPlus", true)
		if mplus and mplus.GenerateTestRuns then
			mplus:GenerateTestRuns(count, isLive)
		else
			Cerebro:Print("M+ module not loaded.")
		end
	end
}

defs[#defs + 1] = {
	id = "test_audit",
	displayName = "Run Audit Tests",
	description = "Executes Audit logging unit tests",
	officerOnly = false,
	slash = "/testaudit",
	handler = function(ctx)
		if SlashCmdList["TESTAUDIT"] then
			SlashCmdList["TESTAUDIT"]()
		else
			Cerebro:Print("Audit tests not loaded.")
		end
	end
}

defs[#defs + 1] = {
	id = "test_diag",
	displayName = "Run Diagnostics Tests",
	description = "Executes Diagnostics unit tests",
	officerOnly = false,
	slash = "/testdiag",
	handler = function(ctx)
		if SlashCmdList["TESTDIAG"] then
			SlashCmdList["TESTDIAG"]()
		else
			Cerebro:Print("Diagnostics tests not loaded.")
		end
	end
}

defs[#defs + 1] = {
	id = "test_env",
	displayName = "Run Environment Tests",
	description = "Tests season tracking and game version utilities",
	officerOnly = false,
	slash = "/cb testenv",
	handler = function(_)
		Cerebro:Print("=== Season & Environment Tests ===")

		-- Raw Build Info (for debugging PTR detection)
		local version, build, _, toc = GetBuildInfo()
		Cerebro:Print(string.format("Raw Build: version=%s, build=%s, toc=%s",
			tostring(version), tostring(build), tostring(toc)))
		Cerebro:Print(string.format("IsTestBuild() = %s",
			IsTestBuild and tostring(IsTestBuild()) or "nil (function not found)"))
		Cerebro:Print(string.format("Realm: %s", GetRealmName() or "?"))

		-- Test 1: GetGameVersion
		local ver = Cerebro:GetGameVersion()
		local pass = type(ver) == "table" and type(ver.version) == "string"
		Cerebro:Print(string.format("%s GetGameVersion returns table (v%s)",
			pass and "|cff00ff00PASS|r" or "|cffff0000FAIL|r", ver.version or "?"))

		-- Test 2: GetSeasonInfo
		local info = Cerebro:GetSeasonInfo()
		pass = type(info) == "table" and type(info.seasonID) == "number"
		Cerebro:Print(string.format("%s GetSeasonInfo returns table (Season %d)",
			pass and "|cff00ff00PASS|r" or "|cffff0000FAIL|r", info.seasonID or -1))

		-- Test 3: GetCurrentSeasonWeek
		local week = Cerebro:GetCurrentSeasonWeek()
		pass = week == nil or type(week) == "number"
		Cerebro:Print(string.format("%s GetCurrentSeasonWeek returns %s",
			pass and "|cff00ff00PASS|r" or "|cffff0000FAIL|r", week and ("Week " .. week) or "nil"))

		-- Test 4: IsReleaseBuild
		local isRelease = Cerebro:IsReleaseBuild()
		pass = type(isRelease) == "boolean"
		Cerebro:Print(string.format("%s IsReleaseBuild returns %s",
			pass and "|cff00ff00PASS|r" or "|cffff0000FAIL|r", tostring(isRelease)))

		-- Test 5: SeasonStartDates
		pass = type(Cerebro.SeasonStartDates) == "table" and Cerebro.SeasonStartDates[13] ~= nil
		Cerebro:Print(string.format("%s SeasonStartDates has Season 13",
			pass and "|cff00ff00PASS|r" or "|cffff0000FAIL|r"))

		Cerebro:Print("=== Tests Complete ===")
	end
}
