import { getPaths } from "yt-dlp-transcript-common/lib/paths";
import { lastRunsByTask, readGenerationUsage, utcDay } from "yt-dlp-transcript-common/controller/generationUsage";
import PolicyPanel from "@/components/articles/PolicyPanel";
import AppShell from "@/components/shell/AppShell";
import { Panel } from "@/components/ui/panel";
import { readPolicy, type PolicyView } from "@/lib/articles/generation";
import { policyChip } from "@/lib/create/policy.mjs";
import { articleHref, settingsHref } from "@/lib/hrefs";
import { daySpend, recentDays } from "@/lib/settings/spend.mjs";
import Link from "next/link";
export const dynamic = "force-dynamic";
// ---------------------------------------------------------------------------
// SETTINGS · GENERATION (plans/umtool-redesign.md, P5): the generation policy
// -- where each task runs (Claude Code, the API, the local model), the presets,
// the models (the extractor an API sweep reads its batches with among them),
// the caps, and whether the API key's FILE is there (its path; never the key)
// -- written through the editor's settings action (PolicyPanel, release 27
// S8, moved here from the new-article form). Under it, the day's spend, job by
// job, from the editor's usage ledger (the total P2b's rail says).
//
// Read from the editor; an editor that does not answer is said, and the
// ledger is still read (it is a file).
// ---------------------------------------------------------------------------
const usd = (v: number) => `$${v.toFixed(2)}`;
const hhmm = (iso: string) => iso.slice(11, 16);
export default async function GenerationPage() {
let policy: PolicyView | null = null;
let policyError: string | null = null;
try {
policy = await readPolicy();
} catch (e) {
policyError = e instanceof Error ? e.message : String(e);
}
const rows = await readGenerationUsage(getPaths().generationUsageFile).catch(() => []);
const day = utcDay();
const spend = daySpend(rows, day);
const daily = policy?.generation.caps.daily ?? 0;
const today = { day, usd: spend.usd, jobs: spend.jobs.length, daily };
const week = recentDays(rows, day, 7);
const weekMax = Math.max(...week.map((d) => d.usd), 0.01);
return (