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

    Commands:
      award_points    (original lines 1125-1175)
      subtract_points (original lines 1177-1223)
      award_readiness (original lines 1225-1259)

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

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

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

-- Award Points (EP/GP)
defs[#defs + 1] = {
	id = "award_points",
	displayName = "Award Points",
	description = "Adds EP or GP to a player",
	officerOnly = true,
	hidden = false,
	dangerous = false,
	slash = "/cb add [player] [amount] (ep/gp) [reason...]",
	usage = "/cb add [player] [amount] (ep/gp) [reason...]",
	examples = {
		"/cb add Shezzfoo 100 Bonus",
		"/cb add Shezzfoo 50 gp Loot award",
	},
	argsSchema = {
		{name = "player", type = "string", required = true},
		{name = "amount", type = "number", required = true},
		{name = "type", type = "string", default = "ep", values = {"ep", "gp"}},
		{name = "reason", type = "string", variadic = true, default = "Officer Adjust"},
	},
	handler = function(ctx, argv)
		local player = argv[1]
		local amount = tonumber(argv[2])
		local typeArg = (argv[3] and argv[3]:lower()) or "ep"
		local reasonStart = 4

		-- Check if arg3 is type or reason
		if typeArg ~= "ep" and typeArg ~= "gp" then
			typeArg = "ep"
			reasonStart = 3
		end

		local reason = table.concat(argv, " ", reasonStart)
		if reason == "" then reason = "Officer Adjust" end

		if not player or not amount then
			Cerebro:Print("Usage: /cb add [player] [amount] (ep/gp) [reason]")
			return
		end

		local fullName = Cerebro:GetFullPlayerName(player)

		if typeArg == "gp" then
			Cerebro.modules.EPGP:ChargeGP(fullName, amount, "Manual", reason)
			Cerebro:Print(string.format("Added %d GP to %s.", amount, player))
		else
			Cerebro.modules.EPGP:AwardEP(fullName, amount, "OFFICER_ADJUST", reason)
			Cerebro:Print(string.format("Added %d EP to %s.", amount, player))
		end
	end
}

-- Subtract Points
defs[#defs + 1] = {
	id = "subtract_points",
	displayName = "Subtract Points",
	description = "Subtracts EP or GP from a player",
	officerOnly = true,
	hidden = false,
	dangerous = false,
	aliases = {"sub", "subtract"},
	slash = "/cb sub [player] [amount] (ep/gp) [reason...]",
	usage = "/cb sub [player] [amount] (ep/gp) [reason...]",
	argsSchema = {
		{name = "player", type = "string", required = true},
		{name = "amount", type = "number", required = true},
		{name = "type", type = "string", default = "ep", values = {"ep", "gp"}},
		{name = "reason", type = "string", variadic = true, default = "Officer Adjust"},
	},
	handler = function(ctx, argv)
		local player = argv[1]
		local amount = tonumber(argv[2])
		local typeArg = (argv[3] and argv[3]:lower()) or "ep"
		local reasonStart = 4

		if typeArg ~= "ep" and typeArg ~= "gp" then
			typeArg = "ep"
			reasonStart = 3
		end

		local reason = table.concat(argv, " ", reasonStart)
		if reason == "" then reason = "Officer Adjust" end

		if not player or not amount then
			Cerebro:Print("Usage: /cb sub [player] [amount] (ep/gp) [reason]")
			return
		end

		local fullName = Cerebro:GetFullPlayerName(player)

		if typeArg == "gp" then
			Cerebro.modules.EPGP:ChargeGP(fullName, -amount, "Manual", reason)
			Cerebro:Print(string.format("Subtracted %d GP from %s.", amount, player))
		else
			Cerebro.modules.EPGP:AwardEP(fullName, -amount, "OFFICER_ADJUST", reason)
			Cerebro:Print(string.format("Subtracted %d EP from %s.", amount, player))
		end
	end
}

-- Readiness Award
defs[#defs + 1] = {
	id = "award_readiness",
	displayName = "Award Readiness EP",
	description = "Awards readiness EP (weekly capped) to a player",
	officerOnly = true,
	hidden = false,
	dangerous = false,
	slash = "/cb readiness award [player] [amount]",
	usage = "/cb readiness award [player] [amount]",
	argsSchema = {
		{name = "subcommand", type = "string", required = true, values = {"award"}},
		{name = "player", type = "string", required = true},
		{name = "amount", type = "number", required = true},
	},
	handler = function(ctx, argv)
		local subCmd = argv[1] and argv[1]:lower()
		if subCmd ~= "award" then
			Cerebro:Print("Usage: /cb readiness award [player] [amount]")
			return
		end

		local player = argv[2]
		local amount = tonumber(argv[3])

		if not player or not amount then
			Cerebro:Print("Usage: /cb readiness award [player] [amount]")
			return
		end

		local fullName = Cerebro:GetFullPlayerName(player)
		local awarded = Cerebro.modules.EPGP:AwardReadinessEP(fullName, amount, "Manual Readiness")
		Cerebro:Print(string.format("Awarded %d Readiness EP to %s.", awarded, player))
	end
}
