import { getChatSession, renameChatSession, deleteChatSession } from '@/lib/runtime.js';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

export async function GET(_req, { params }) {
  const { id } = await params;
  const session = getChatSession(id);
  if (!session) return Response.json({ error: 'not found' }, { status: 404 });
  return Response.json({ session });
}

export async function PUT(req, { params }) {
  const { id } = await params;
  let body = {};
  try { body = await req.json(); } catch {}
  if (typeof body.title !== 'string') {
    return Response.json({ error: 'title required' }, { status: 400 });
  }
  const result = renameChatSession(id, body.title);
  if (!result.ok) return Response.json({ error: 'not found' }, { status: 404 });
  return Response.json({ ok: true });
}

export async function DELETE(_req, { params }) {
  const { id } = await params;
  const result = deleteChatSession(id);
  if (!result.ok) return Response.json({ error: 'not found' }, { status: 404 });
  return Response.json({ ok: true });
}
