/**
 * Schedule Launch Banner — one-line stderr banner for due scheduled items.
 *
 * Purpose: surface pending schedule entries to the user on every sf launch
 * so reminders and due tasks are visible without running /schedule list.
 *
 * Consumer: cli.ts interactive startup path.
 */

import { isAutonomousDispatchScheduleEntry } from "./schedule/schedule-autonomous-dispatch.js";
import { createScheduleStore } from "./schedule/schedule-store.js";

/**
 * Print a one-line banner to stderr if there are due schedule entries.
 *
 * Reads project scope, de-duplicates by id, and prints:
 *   [forge] 2 scheduled items due: Review PR #123, Standup reminder
 *
 * Fast-exits when no due items exist. Non-fatal — never throws.
 *
 * @param {string} basePath
 * @returns {void}
 */
export function printScheduleBanner(basePath) {
	const store = createScheduleStore(basePath);
	const now = new Date().toISOString();

	/** @type {import("./schedule/schedule-types.js").ScheduleEntry[]} */
	let due = [];
	try {
		due = store.findDue("project", now);
	} catch {
		// Best-effort — never block startup
	}

	const autonomousDispatch = due.filter(isAutonomousDispatchScheduleEntry);
	const passive = due.filter(
		(entry) => !isAutonomousDispatchScheduleEntry(entry),
	);

	if (due.length === 0) return;

	// Sort by due_at ascending
	passive.sort((a, b) => new Date(a.due_at) - new Date(b.due_at));
	autonomousDispatch.sort((a, b) => new Date(a.due_at) - new Date(b.due_at));

	if (passive.length > 0) {
		const titles = passive
			.slice(0, 3)
			.map((e) => e.payload?.message || e.id.slice(0, 8));
		const more = passive.length > 3 ? ` (+${passive.length - 3} more)` : "";
		const label =
			passive.length === 1 ? "scheduled item due" : "scheduled items due";

		process.stderr.write(
			`[forge] ${passive.length} ${label}: ${titles.join(", ")}${more}. Manage: /schedule list\n`,
		);
	}

	if (autonomousDispatch.length > 0) {
		const label =
			autonomousDispatch.length === 1
				? "scheduled autonomous dispatch item due"
				: "scheduled autonomous dispatch items due";
		process.stderr.write(
			`[forge] ${autonomousDispatch.length} ${label}; autonomous mode will consume project entries.\n`,
		);
	}
}

/**
 * Alias for printScheduleBanner.
 *
 * @param {string} basePath
 * @returns {void}
 */
export function showScheduleBanner(basePath) {
	return printScheduleBanner(basePath);
}
