// api.ts から分割（挙動不変）: Reflection（タスク別最新スナップショット）。
import { BASE } from './client';

// --- Reflection ---
export interface LatestReflectionForTask {
  snapshotId: string;
  outcome: string;
  memoryChanges: number | null;
  pieceEdited: boolean;
}

export async function getLatestReflectionForTask(
  taskId: number,
): Promise<LatestReflectionForTask | null> {
  const res = await fetch(`${BASE}/local/reflection/latest-for-task/${taskId}`);
  if (res.status === 404) return null;
  if (!res.ok) throw new Error(`getLatestReflectionForTask: ${res.status}`);
  const data = await res.json();
  // API returns null body when no reflection exists
  if (!data || !data.snapshotId) return null;
  return data as LatestReflectionForTask;
}
