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

    Commands:
      start_session       (original lines 200-213)
      end_session         (original lines 214-227)
      force_end_session   (original lines 228-241)
      prep_check          (original lines 243-257)
      attendance_summary  (original lines 2510-2648)

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

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

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

-- Session Commands (Officer)
defs[#defs + 1] = {
	id = "start_session",
	displayName = "Start Raid Session",
	description = "Begins a new raid session with attendance tracking",
	officerOnly = true,
	slash = "/cb start",
	handler = function(ctx)
		if Cerebro.modules.Session then
			Cerebro.modules.Session:StartSession()
		else
			Cerebro:Print("Session module not loaded.")
		end
	end
}

defs[#defs + 1] = {
	id = "end_session",
	displayName = "End Raid Session",
	description = "Ends the current raid session",
	officerOnly = true,
	slash = "/cb end",
	handler = function(ctx)
		if Cerebro.modules.Session then
			Cerebro.modules.Session:EndSession()
		else
			Cerebro:Print("Session module not loaded.")
		end
	end
}

defs[#defs + 1] = {
	id = "force_end_session",
	displayName = "Force End Session",
	description = "Nuclear cleanup: wipes current session with NO EP awards. Use when a session is stuck or corrupted.",
	officerOnly = true,
	slash = "/cb forceend",
	handler = function(ctx)
		if Cerebro.modules.Session then
			Cerebro.modules.Session:ForceEndSession()
		else
			Cerebro:Print("Session module not loaded.")
		end
	end
}

-- Preparation Commands (Officer)
defs[#defs + 1] = {
	id = "prep_check",
	displayName = "Run Prep Check",
	description = "Inspects raid members for gems, enchants, and consumables",
	officerOnly = true,
	slash = "/cb prep",
	handler = function(ctx)
		if Cerebro.modules.Preparation then
			Cerebro.modules.Preparation:CheckRaid()
		else
			Cerebro:Print("Preparation module not loaded.")
		end
	end
}

-- Attendance Commands (Officer)
defs[#defs + 1] = {
	id = "attendance_summary",
	displayName = "Attendance Summary",
	description = "Show current session attendance summary",
	officerOnly = true,
	hidden = false,
	dangerous = false,
	aliases = {"attendance"},
	slash = "/cb attendance",
	usage = "/cb attendance [live|late|missing|report [session-id]]",
	handler = function(ctx, argv)
		local subCmd = argv[1] and argv[1]:lower()

		-- Sub-commands
		if subCmd == "live" then
			-- Real-time roster
			if not IsInRaid() then
				Cerebro:Print("Not in a raid group.")
				return
			end
			local roster = Cerebro:GetRaidRoster()
			Cerebro:Print(string.format("=== Live Roster (%d players) ===", #roster))
			for _, member in ipairs(roster) do
				-- Read-only lookup: do NOT use GetPlayerData() here as it auto-creates
				-- records for cross-realm PUGs who aren't in our guild
				local data = Cerebro.db.global.players[member.name]
				local status = data and data.status or "Unknown"
				Cerebro:Print(string.format("  %s (%s)", Cerebro:ColorPlayerName(member.name, member.class), status))
			end
			return
		end

		if subCmd == "late" then
			-- Late arrivals
			local session = Cerebro.db.global.currentSession
			if not session then
				Cerebro:Print("No active session.")
				return
			end
			local lateCount = 0
			Cerebro:Print("=== Late Arrivals ===")
			for player, entry in pairs(session.roster) do
				if entry.onTime == false then
					Cerebro:Print("  " .. Cerebro:ColorPlayerName(player))
					lateCount = lateCount + 1
				end
			end
			if lateCount == 0 then
				Cerebro:Print("  No late arrivals recorded.")
			end
			return
		end

		if subCmd == "missing" then
			-- Core players not in raid
			if not IsInRaid() then
				Cerebro:Print("Not in a raid group.")
				return
			end
			local roster = Cerebro:GetRaidRoster()
			local inRaid = {}
			for _, member in ipairs(roster) do
				inRaid[member.name] = true
			end

			local missingCount = 0
			Cerebro:Print("=== Missing Core Players ===")
			for name, data in pairs(Cerebro.db.global.players) do
				if data.status == "Core" and not inRaid[name] then
					Cerebro:Print("  " .. Cerebro:ColorPlayerName(name, data.class))
					missingCount = missingCount + 1
				end
			end
			if missingCount == 0 then
				Cerebro:Print("  All Core players are in raid.")
			end
			return
		end

		if subCmd == "report" then
			local sessionID = argv[2]
			if not sessionID then
				Cerebro:Print("Usage: /cb attendance report [session-id]")
				Cerebro:Print("Available sessions:")
				local count = 0
				for id, session in pairs(Cerebro.db.global.history or {}) do
					count = count + 1
					if count <= 5 then
						Cerebro:Print("  " .. id)
					end
				end
				if count > 5 then
					Cerebro:Print("  ... and " .. (count - 5) .. " more")
				end
				return
			end

			local session = Cerebro.db.global.history[sessionID]
			if not session then
				Cerebro:Print("Session not found: " .. sessionID)
				return
			end

			Cerebro:Print(string.format("=== Session Report: %s ===", sessionID))
			Cerebro:Print(string.format("Raid: %s", session.raidName or "Unknown"))
			Cerebro:Print(string.format("Bosses Killed: %d", #(session.bosses or {})))
			local onTimeCount, lateCount = 0, 0
			for player, entry in pairs(session.roster) do
				if entry.onTime then onTimeCount = onTimeCount + 1 end
				if entry.onTime == false then lateCount = lateCount + 1 end
			end
			Cerebro:Print(string.format("On-Time: %d, Late: %d", onTimeCount, lateCount))
			return
		end

		-- Default: summary
		local session = Cerebro.db.global.currentSession
		if not session then
			Cerebro:Print("No active session. Use /cb attendance report [session-id] for history.")
			return
		end

		local bossCount = #(session.bosses or {})
		local rosterCount = 0
		local onTimeCount, lateCount = 0, 0
		for player, entry in pairs(session.roster) do
			rosterCount = rosterCount + 1
			if entry.onTime then onTimeCount = onTimeCount + 1 end
			if entry.onTime == false then lateCount = lateCount + 1 end
		end

		Cerebro:Print("=== Current Session Attendance ===")
		Cerebro:Print(string.format("Raid: %s", session.raidName or "Unknown"))
		Cerebro:Print(string.format("Started: %s", Cerebro:FormatRelativeTime(session.startTime)))
		Cerebro:Print(string.format("Bosses Killed: %d", bossCount))
		Cerebro:Print(string.format("Players: %d (On-Time: %d, Late: %d)", rosterCount, onTimeCount, lateCount))
	end
}
