// SF Extension — /notifications Command Handler
// View, filter, and clear the persistent notification history.
import { SFNotificationOverlay } from "../../notification-overlay.js";
import {
	clearNotifications,
	getUnreadCount,
	readNotifications,
	suppressPersistence,
	unsuppressPersistence,
} from "../../notification-store.js";

const MAX_INLINE_ENTRIES = 40;
function severityIcon(severity) {
	switch (severity) {
		case "error":
			return "✗";
		case "warning":
			return "⚠";
		case "success":
			return "✓";
		default:
			return "●";
	}
}
function formatTimestamp(ts) {
	try {
		const d = new Date(ts);
		return d.toLocaleString("en-US", {
			hour12: false,
			month: "short",
			day: "numeric",
			hour: "2-digit",
			minute: "2-digit",
		});
	} catch {
		return ts.slice(0, 19);
	}
}
function formatNotificationLine(e) {
	const repeat =
		(e.repeatCount ?? 1) > 1
			? ` ×${e.repeatCount}${
					e.lastTs ? ` (last ${formatTimestamp(e.lastTs)})` : ""
				}`
			: "";
	return `${severityIcon(e.severity)} [${formatTimestamp(e.ts)}] ${e.message}${repeat}`;
}
export async function handleNotificationsCommand(args, ctx, _pi) {
	// /notifications clear
	if (args === "clear") {
		clearNotifications();
		// Suppress persistence so the confirmation toast doesn't re-populate the store
		suppressPersistence();
		try {
			ctx.ui.notify("All notifications cleared.", "success");
		} finally {
			unsuppressPersistence();
		}
		return true;
	}
	// /notifications tail [N]
	if (args === "tail" || args.startsWith("tail ")) {
		const countStr = args.replace(/^tail\s*/, "").trim();
		const count = countStr ? parseInt(countStr, 10) : 20;
		const all = readNotifications();
		const n =
			Number.isNaN(count) || count < 1
				? 20
				: Math.min(count, MAX_INLINE_ENTRIES);
		const entries = all.slice(0, n);
		if (entries.length === 0) {
			ctx.ui.notify("No notifications.", "info");
			return true;
		}
		const lines = entries.map(formatNotificationLine);
		const suffix =
			all.length > entries.length
				? `\n... and ${all.length - entries.length} more (open /notifications to browse all)`
				: "";
		ctx.ui.notify(
			`Last ${entries.length} notification(s):\n${lines.join("\n")}${suffix}`,
			"info",
		);
		return true;
	}
	// /notifications filter <severity>
	if (args.startsWith("filter ")) {
		const severity = args
			.replace(/^filter\s+/, "")
			.trim()
			.toLowerCase();
		if (!["error", "warning", "info", "success"].includes(severity)) {
			ctx.ui.notify(
				"Usage: /notifications filter <error|warning|info|success>",
				"warning",
			);
			return true;
		}
		const entries = readNotifications().filter((e) => e.severity === severity);
		if (entries.length === 0) {
			ctx.ui.notify(`No ${severity} notifications.`, "info");
			return true;
		}
		const lines = entries.slice(0, 20).map(formatNotificationLine);
		const suffix =
			entries.length > 20
				? `\n... and ${entries.length - 20} more (open /notifications to browse all)`
				: "";
		ctx.ui.notify(
			`${severity} notifications (${entries.length}):\n${lines.join("\n")}${suffix}`,
			"info",
		);
		return true;
	}
	// /notifications (no args) — open overlay in TUI, or print summary
	if (args === "" || args === "status") {
		// Try overlay first (TUI mode)
		if (ctx.hasUI) {
			try {
				const result = await ctx.ui.custom(
					(tui, theme, _kb, done) =>
						new SFNotificationOverlay(tui, theme, () => done(true)),
					{
						overlay: true,
						overlayOptions: {
							width: "80%",
							minWidth: 60,
							maxHeight: "88%",
							anchor: "center",
							backdrop: true,
						},
					},
				);
				if (result !== undefined) {
					return true;
				}
			} catch {
				// Fall through to text output if overlay fails
			}
		}
		// Text fallback (RPC/machine surface)
		const unread = getUnreadCount();
		const entries = readNotifications().slice(0, 10);
		if (entries.length === 0) {
			ctx.ui.notify("No notifications.", "info");
			return true;
		}
		const lines = entries.map(formatNotificationLine);
		const header = unread > 0 ? `${unread} unread — ` : "";
		ctx.ui.notify(
			`${header}Recent notifications:\n${lines.join("\n")}`,
			"info",
		);
		return true;
	}
	// Unknown subcommand
	ctx.ui.notify(
		"Usage: /notifications [clear|tail [N]|filter <severity>]",
		"warning",
	);
	return true;
}
