import { existsSync } from "node:fs";
import { join } from "node:path";
import { Key } from "@singularity-forge/tui";
import { shortcutDesc } from "../../shared/mod.js";
import { projectRoot } from "../commands/context.js";
import { SFDashboardOverlay } from "../dashboard-overlay.js";
import { SFNotificationOverlay } from "../notification-overlay.js";
import { ParallelMonitorOverlay } from "../parallel-monitor-overlay.js";
import { SF_SHORTCUTS } from "../shortcut-defs.js";
export function registerShortcuts(pi) {
	const overlayOptions = {
		width: "90%",
		minWidth: 80,
		maxHeight: "92%",
		anchor: "center",
	};
	const openDashboardOverlay = async (ctx) => {
		const basePath = projectRoot();
		if (!existsSync(join(basePath, ".sf"))) {
			ctx.ui.notify("No .sf/ directory found. Run /next to start.", "info");
			return;
		}
		await ctx.ui.custom(
			(tui, theme, _kb, done) =>
				new SFDashboardOverlay(tui, theme, () => done(true)),
			{
				overlay: true,
				overlayOptions,
			},
		);
	};
	const openNotificationsOverlay = async (ctx) => {
		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,
				},
			},
		);
	};
	const openParallelOverlay = async (ctx) => {
		const basePath = projectRoot();
		const parallelDir = join(basePath, ".sf", "parallel");
		if (!existsSync(parallelDir)) {
			ctx.ui.notify(
				"No parallel workers found. Run /parallel start first.",
				"info",
			);
			return;
		}
		await ctx.ui.custom(
			(tui, theme, _kb, done) =>
				new ParallelMonitorOverlay(tui, theme, () => done(true), basePath),
			{
				overlay: true,
				overlayOptions,
			},
		);
	};
	pi.registerShortcut(Key.ctrlAlt(SF_SHORTCUTS.dashboard.key), {
		description: shortcutDesc(
			SF_SHORTCUTS.dashboard.action,
			SF_SHORTCUTS.dashboard.command,
		),
		handler: openDashboardOverlay,
	});
	// Fallback for terminals where Ctrl+Alt letter chords are not forwarded reliably.
	pi.registerShortcut(Key.ctrlShift(SF_SHORTCUTS.dashboard.key), {
		description: shortcutDesc(
			`${SF_SHORTCUTS.dashboard.action} (fallback)`,
			SF_SHORTCUTS.dashboard.command,
		),
		handler: openDashboardOverlay,
	});
	pi.registerShortcut(Key.ctrlAlt(SF_SHORTCUTS.notifications.key), {
		description: shortcutDesc(
			SF_SHORTCUTS.notifications.action,
			SF_SHORTCUTS.notifications.command,
		),
		handler: openNotificationsOverlay,
	});
	// Fallback for terminals where Ctrl+Alt letter chords are not forwarded reliably.
	pi.registerShortcut(Key.ctrlShift(SF_SHORTCUTS.notifications.key), {
		description: shortcutDesc(
			`${SF_SHORTCUTS.notifications.action} (fallback)`,
			SF_SHORTCUTS.notifications.command,
		),
		handler: openNotificationsOverlay,
	});
	pi.registerShortcut(Key.ctrlAlt(SF_SHORTCUTS.parallel.key), {
		description: shortcutDesc(
			SF_SHORTCUTS.parallel.action,
			SF_SHORTCUTS.parallel.command,
		),
		handler: openParallelOverlay,
	});
	// No Ctrl+Shift+P fallback — conflicts with cycleModelBackward (shift+ctrl+p).
	// Use Ctrl+Alt+P or /parallel watch instead.
}
