/**
 * Remote Questions — status helpers
 */
import { existsSync, readdirSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
import { readPromptRecord } from "./store.js";

function getSfHome() {
	return process.env.SF_HOME || join(homedir(), ".sf");
}
export function getLatestPromptSummary() {
	const runtimeDir = join(getSfHome(), "runtime", "remote-questions");
	if (!existsSync(runtimeDir)) return null;
	const files = readdirSync(runtimeDir).filter((f) => f.endsWith(".json"));
	if (files.length === 0) return null;
	let latest = null;
	for (const file of files) {
		const record = readPromptRecord(file.replace(/\.json$/, ""));
		if (!record) continue;
		if (!latest || record.updatedAt > latest.updatedAt) {
			latest = {
				id: record.id,
				status: record.status,
				updatedAt: record.updatedAt,
			};
		}
	}
	return latest;
}
