import { readFile } from "node:fs/promises";
import path from "node:path";
import { notFound } from "next/navigation";
import AppShell from "@/components/shell/AppShell";
import RecordSession, { type RecordItem } from "./RecordSession";
import { readManifest } from "@/lib/projects/report.mjs";
import { projectHref } from "@/lib/hrefs";
import { deckGeometry, deckOn } from "umtool-report-to-video/deck";
import { draftScript, pickedTake, takeSourceOf } from "umtool-report-to-video/post-narration";
import { normalizeParts, partsFileOf } from "@/lib/report/postNarrationCli.mjs";
import { avatarEntryOf } from "umtool-report-to-video/post-avatars";
import type { ProjectRef } from "@/lib/project-types";
import { projectRedaction } from "@/lib/report/postRedaction.mjs";
import { termsFor } from "umtool-report-to-video/post-redact";
// THE RECORDING SESSION (operator, 3 Nov: "I need to do a dramatic reading of
// this starting tomorrow"): /projects/
/narration/record walks the cut's
// post items in order -- each post's card and attachment, its SCRIPT as a
// teleprompter in its PARTS (the placeholder take's parts.json when there is
// one, else the script split the same way), recorded part by part, every part
// on disk as soon as it is kept (lib/report/recordSession.mjs), the post's
// recorded take made and picked when the last part is in.
type Take = { file: string; engine?: string; voice?: string; seconds: number; parts?: unknown; source?: { kind?: string } };
type Entry = Record & { id: string; type?: string; text?: string; author?: string; handle?: string; date?: string; media?: Array<{ src: string; kind: string }>; narration?: { script?: string; takes?: Take[] } };
export default async function RecordPage({ project, search = {} }: { project: ProjectRef; search?: Record }) {
const m = (await readManifest(project.dir)) as Record | null;
if (!m) notFound();
const render = (m.render ?? {}) as Record;
const posts = ((Array.isArray(m.timeline) ? m.timeline : []) as Entry[]).filter((e) => e?.type === "post");
if (!posts.length) notFound();
const items: RecordItem[] = [];
// The words to leave out of the read (post-redact.mjs): marked on the teleprompter, never removed from the script.
const redaction = await projectRedaction(project.dir, m);
for (const [i, e] of posts.entries()) {
const takes = e.narration?.takes ?? [];
const picked = pickedTake(e) as (Take & { index: number }) | null;
// The placeholder: the newest synthesized take (its parts the teleprompter's).
const placeholder = [...takes].reverse().find((t) => (takeSourceOf(t) as { kind: string }).kind === "tts") ?? null;
let parts = placeholder ? normalizeParts(placeholder.parts) : null;
if (placeholder && !parts) {
parts = normalizeParts(await readFile(path.join(project.dir, ...partsFileOf(placeholder.file).split("/")), "utf8").then((t) => JSON.parse(t), () => null));
}
const recorded = takes.some((t) => (takeSourceOf(t) as { kind: string }).kind === "recorded");
const av = e.handle ? avatarEntryOf(m, e.handle) : null;
items.push({
id: e.id, n: i + 1, author: String(e.author ?? ""), handle: String(e.handle ?? ""), date: String(e.date ?? ""),
text: String(e.text ?? ""),
script: String(e.narration?.script ?? draftScript(e.text ?? "").script),
plays: !picked ? "none" : (takeSourceOf(picked) as { kind: string }).kind === "recorded" ? "recorded" : "placeholder",
recorded,
placeholder: placeholder ? { file: placeholder.file, seconds: placeholder.seconds, parts: (parts ?? []).map((p) => ({ n: p.n, para: p.para ?? 0, text: p.text ?? "", start: p.start, seconds: p.seconds, ...(p.file ? { file: p.file } : {}) })) } : null,
media: (e.media ?? []).slice(0, 1).map((x) => ({ src: x.src, kind: x.kind }))[0] ?? null,
avatar: av ? String((av[1] as { src?: string }).src ?? "") || null : null,
skip: termsFor(e, redaction) as RecordItem["skip"],
});
}
const box = deckOn(render) ? deckGeometry(render).footage : { width: Number(render.width ?? 1920), height: Number(render.height ?? 1080) };
const want = search.post ? items.findIndex((x) => x.id === search.post) : -1;
const start = want >= 0 ? want : Math.max(0, items.findIndex((x) => !x.recorded));
return (
x.recorded).length} of ${items.length} posts recorded`}
/>
);
}