/**
 * Remote Questions — status helpers
 */

import { existsSync, readdirSync } from "node:fs";
import { join } from "node:path";
import { homedir } from "node:os";
import { readPromptRecord } from "./store.js";

function getGsdHome(): string {
  return process.env.SF_HOME || join(homedir(), ".sf");
}

export interface LatestPromptSummary {
  id: string;
  status: string;
  updatedAt: number;
}

export function getLatestPromptSummary(): LatestPromptSummary | null {
  const runtimeDir = join(getGsdHome(), "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: LatestPromptSummary | null = 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;
}
