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

    @module UI.Tabs.SystemLogs
    @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: System Logs
===============================================]]

function UISandbox:DrawSystemLogsTab(container)
	if not Cerebro:IsOfficer() then
		local label = AceGUI:Create("Label")
		label:SetText("|cffff4444Officer access required.|r")
		label:SetFullWidth(true)
		container:AddChild(label)
		return
	end

	-- Search Bar
	local searchGroup = AceGUI:Create("SimpleGroup")
	searchGroup:SetFullWidth(true)
	searchGroup:SetLayout("Flow")
	container:AddChild(searchGroup)

	-- Initialize search state if needed
	self.searchText = self.searchText or {}

	local searchBox = AceGUI:Create("EditBox")
	searchBox:SetLabel("Search (press Enter)")
	searchBox:SetWidth(300)
	searchBox:SetText(self.searchText["system_logs"] or "")
	searchBox:SetCallback("OnEnterPressed", function(widget, event, text)
		self.searchText["system_logs"] = text
		-- Refresh the tab
		container:ReleaseChildren()
		self:DrawSystemLogsTab(container)
	end)
	searchGroup:AddChild(searchBox)

	-- Content Area
	local contentGroup = AceGUI:Create("SimpleGroup")
	contentGroup:SetFullWidth(true)
	contentGroup:SetFullHeight(true)
	contentGroup:SetLayout("Fill")
	container:AddChild(contentGroup)

	local editBox = AceGUI:Create("MultiLineEditBox")
	editBox:SetLabel("")
	editBox:SetFullWidth(true)
	editBox:SetFullHeight(true)
	editBox:DisableButton(true)
	contentGroup:AddChild(editBox)

	-- Build audit log text
	local auditText = ""
	local list = Cerebro.db.global.audit or {}
	local searchFilter = (self.searchText["system_logs"] or ""):lower()

	if #list == 0 then
		editBox:SetText("No audit logs found.")
		return
	end

	-- Show last 100 (filtered), newest first
	local count = 0
	local currentEpoch = Cerebro.db.global.resetEpoch or 0
	for i = #list, 1, -1 do
		local entry = list[i]

		-- Epoch marker: show [Epoch N] for entries from previous epochs
		local epochTag = ""
		if entry.epoch and entry.epoch ~= currentEpoch then
			epochTag = string.format("|cff888888[Epoch %d]|r ", entry.epoch)
		end

		-- Format: [DATE TIME] [Epoch N] Admin: ACTION - Details
		local line = string.format("[%s] %s|cff00ccff%s|r: |cffffaa00%s|r - %s",
			date("%m/%d %H:%M", entry.timestamp),
			epochTag,
			Cerebro:GetShortPlayerName(entry.admin),
			entry.action or "ACTION",
			entry.details or ""
		)

		-- Apply search filter (case-insensitive)
		if searchFilter == "" or line:lower():find(searchFilter, 1, true) then
			auditText = auditText .. line .. "\n"
			count = count + 1
			if count >= 250 then break end
		end
	end

	if count == 0 then
		auditText = "No matching entries found."
	end

	editBox:SetText(auditText)

	-- Make Read-Only (Revert on change)
	editBox:SetCallback("OnTextChanged", function(widget)
		if widget:GetText() ~= auditText then
			widget:SetText(auditText)
		end
	end)
end
