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

    Commands:
      show_loothistory  (original lines 813-830)
      rclc_command      (original lines 832-909)
      ep_command        (original lines 910-1005)

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

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

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

-- RCLC Loot History (formerly Wishlist)
defs[#defs + 1] = {
	id = "show_loothistory",
	displayName = "Show Loot History",
	description = "Opens the RCLC Loot History tab",
	officerOnly = false,
	hidden = false,
	dangerous = false,
	aliases = {"loothistory", "lh"},
	slash = "/cb loothistory or /cb lh",
	handler = function(ctx)
		if Cerebro.modules.UI then
			Cerebro.modules.UI:ShowWishlist()  -- Reuses existing tab show method
		else
			Cerebro:Print("UI module not loaded.")
		end
	end
}

-- RCLC Import Operations (parent command with subcommands)
defs[#defs + 1] = {
	id = "rclc_command",
	displayName = "RCLC Operations",
	description = "RCLC import, refresh, and status commands",
	officerOnly = false,  -- Subcommands have their own gates
	hidden = false,
	dangerous = false,
	aliases = {"rclc"},
	slash = "/cb rclc [import|refresh|status|paste]",
	handler = function(ctx, argv)
		local subCmd = argv[1] and argv[1]:lower() or "help"

		local importModule = Cerebro:GetModule("RCLC_Import", true)
		if not importModule then
			Cerebro:Print("RCLC Import module not loaded. Is RCLootCouncil installed?")
			return
		end

		if subCmd == "import" then
			-- Officer-only import
			if not Cerebro:IsOfficer() then
				Cerebro:Print("RCLC import requires officer permissions.")
				return
			end

			local dateArg = argv[2]
			if not dateArg then
				Cerebro:Print("Usage: /cb rclc import MM-DD-YYYY")
				Cerebro:Print("  Imports RCLC loot data from the specified date forward.")
				return
			end

			local sinceDate = importModule:ParseDateArg(dateArg)
			if not sinceDate then
				Cerebro:Print("Invalid date format. Use: MM-DD-YYYY (e.g., 03-19-2026)")
				return
			end

			Cerebro:Print(string.format("Starting RCLC import from %s...", dateArg))
			importModule:ImportFromRCLC(sinceDate)

		elseif subCmd == "refresh" then
			-- All players can refresh the tab
			importModule._tabStale = true
			importModule:DetectNewRCLCDates()
			Cerebro:RefreshAllUI()
			Cerebro:Print("RCLC Loot History tab refreshed.")

		elseif subCmd == "status" then
			importModule:PrintStatus()

		elseif subCmd == "dryrun" or subCmd == "dry-run" or subCmd == "simulate" then
			-- Dry-run: safe simulation, no officer check needed
			local dateArg = argv[2]
			if not dateArg then
				Cerebro:Print("Usage: /cb rclc dryrun MM-DD-YYYY")
				Cerebro:Print("  Shows what an import WOULD do without modifying data.")
				return
			end

			local sinceDate = importModule:ParseDateArg(dateArg)
			if not sinceDate then
				Cerebro:Print("Invalid date format. Use: MM-DD-YYYY (e.g., 02-19-2026)")
				return
			end

			importModule:SimulateImport(sinceDate)

		else
			Cerebro:Print("=== RCLC Commands ===")
			Cerebro:Print("/cb rclc dryrun MM-DD-YYYY - Simulate import (safe, read-only)")
			Cerebro:Print("/cb rclc import MM-DD-YYYY - Import RCLC loot data (Officer)")
			Cerebro:Print("/cb rclc refresh - Refresh Loot History tab")
			Cerebro:Print("/cb rclc status - Show last import status")
		end
	end
}

-- EP Import Operations (parent command with subcommands)
defs[#defs + 1] = {
	id = "ep_command",
	displayName = "EP Operations",
	description = "EP reimport, simulation, and status commands",
	officerOnly = false,  -- Subcommands have their own gates
	hidden = false,
	dangerous = false,
	aliases = {"ep"},
	slash = "/cb ep [reimport|dryrun|status|sessions]",
	handler = function(ctx, argv)
		local subCmd = argv[1] and argv[1]:lower() or "help"

		local epModule = Cerebro:GetModule("EP_Import", true)
		if not epModule then
			Cerebro:Print("EP Import module not loaded.")
			return
		end

		if subCmd == "reimport" then
			-- Officer-only reimport
			if not Cerebro:IsOfficer() then
				Cerebro:Print("EP reimport requires officer permissions.")
				return
			end

			local dateArg = argv[2]
			if not dateArg then
				Cerebro:Print("Usage: /cb ep reimport MM-DD-YYYY")
				Cerebro:Print("  Reimports session EP from the specified date forward.")
				return
			end

			local sinceTimestamp = epModule:ParseDateArg(dateArg)
			if not sinceTimestamp then
				Cerebro:Print("Invalid date format. Use: MM-DD-YYYY (e.g., 03-19-2026)")
				return
			end

			Cerebro:Print(string.format("Starting EP reimport from %s...", dateArg))
			epModule:ReimportFromSessions(sinceTimestamp)

		elseif subCmd == "dryrun" or subCmd == "dry-run" or subCmd == "simulate" then
			-- Dry-run: safe simulation, officer check
			if not Cerebro:IsOfficer() then
				Cerebro:Print("EP simulate requires officer permissions.")
				return
			end

			local dateArg = argv[2]
			if not dateArg then
				Cerebro:Print("Usage: /cb ep dryrun MM-DD-YYYY")
				Cerebro:Print("  Shows what a reimport WOULD do without modifying data.")
				return
			end

			local sinceTimestamp = epModule:ParseDateArg(dateArg)
			if not sinceTimestamp then
				Cerebro:Print("Invalid date format. Use: MM-DD-YYYY (e.g., 03-19-2026)")
				return
			end

			epModule:SimulateReimport(sinceTimestamp)

		elseif subCmd == "status" then
			epModule:PrintStatus()

		elseif subCmd == "sessions" then
			-- List available sessions
			local sessions = epModule:GetSessionDates()
			if #sessions == 0 then
				Cerebro:Print("No sessions found in history.")
				return
			end

			Cerebro:Print("=== Session History ===")
			for i, s in ipairs(sessions) do
				if i > 20 then
					Cerebro:Print(string.format("  ... and %d more", #sessions - 20))
					break
				end
				Cerebro:Print(string.format("  %s  %s  (%d txns, %d players)",
					s.dateStr, s.raidName, s.txnCount, s.playerCount))
			end
			Cerebro:Print(string.format("Total: %d sessions", #sessions))

		else
			Cerebro:Print("=== EP Commands ===")
			Cerebro:Print("/cb ep dryrun MM-DD-YYYY - Simulate reimport (safe, read-only)")
			Cerebro:Print("/cb ep reimport MM-DD-YYYY - Reimport session EP (Officer)")
			Cerebro:Print("/cb ep sessions - List available session history")
			Cerebro:Print("/cb ep status - Show last reimport status")
		end
	end
}
