--[[
    Cerebro - UISandbox Session Tab
    Extracted from UI/UISandbox.lua (Phase 2a decomposition)

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

local AddonName = "Cerebro"
local Cerebro = LibStub("AceAddon-3.0"):GetAddon(AddonName)
local AceGUI = LibStub("AceGUI-3.0")
local UISandbox = Cerebro:GetModule("UISandbox")

--[[=============================================
	Officer Tab: Session Management
===============================================]]

function UISandbox:DrawSessionTab(container)
	-- RBAC check (belt & suspenders - sidebar already gated)
	if not Cerebro:IsOfficer() then
		local label = AceGUI:Create("Label")
		label:SetText("|cffff4444Officer access required.|r")
		label:SetFullWidth(true)
		container:AddChild(label)
		return
	end

	local session = Cerebro.modules.Session and Cerebro.modules.Session:GetCurrentSession()

	-- Header
	local header = AceGUI:Create("Heading")
	header:SetText("Session Management")
	header:SetFullWidth(true)
	container:AddChild(header)

	if session then
		-- Active session info
		local activeLabel = AceGUI:Create("Label")
		activeLabel:SetText(string.format("|cff00ff00Active Session:|r %s", session.raidName or "Raid"))
		activeLabel:SetFullWidth(true)
		container:AddChild(activeLabel)

		local spacer = AceGUI:Create("Label")
		spacer:SetText("\n")
		spacer:SetFullWidth(true)
		container:AddChild(spacer)

		-- End Session button
		local endBtn = AceGUI:Create("Button")
		endBtn:SetText("End Raid Session")
		endBtn:SetWidth(180)
		endBtn:SetCallback("OnClick", function()
			Cerebro.modules.Session:EndSession()
			self:SelectTab("session") -- Refresh
		end)
		container:AddChild(endBtn)
	else
		-- No active session
		local inactiveLabel = AceGUI:Create("Label")
		inactiveLabel:SetText("|cff888888No active session.|r")
		inactiveLabel:SetFullWidth(true)
		container:AddChild(inactiveLabel)

		local spacer = AceGUI:Create("Label")
		spacer:SetText("\n")
		spacer:SetFullWidth(true)
		container:AddChild(spacer)

		-- Start Session button
		local startBtn = AceGUI:Create("Button")
		startBtn:SetText("Start Raid Session")
		startBtn:SetWidth(180)
		startBtn:SetCallback("OnClick", function()
			Cerebro.modules.Session:StartSession()
			self:SelectTab("session") -- Refresh
		end)
		container:AddChild(startBtn)
	end

	-- Additional officer tools
	local spacer2 = AceGUI:Create("Label")
	spacer2:SetText("\n")
	spacer2:SetFullWidth(true)
	container:AddChild(spacer2)

	-- Prep Check button
	local prepBtn = AceGUI:Create("Button")
	prepBtn:SetText("Run Prep Check")
	prepBtn:SetWidth(150)
	prepBtn:SetCallback("OnClick", function()
		if Cerebro.modules.Preparation then
			Cerebro.modules.Preparation:CheckRaid()
		end
	end)
	container:AddChild(prepBtn)

	-- Decay button
	local decayBtn = AceGUI:Create("Button")
	decayBtn:SetText("Run Weekly Decay")
	decayBtn:SetWidth(150)
	decayBtn:SetCallback("OnClick", function()
		StaticPopup_Show("CEREBRO_CONFIRM_DECAY")
	end)
	container:AddChild(decayBtn)
end
