import { NextResponse } from "next/server";
import { SessionManager } from "@/lib/agents/SessionManager";

export async function POST(request: Request) {
  try {
    const body = await request.json();
    const { id, delete: deleteSession } = body;

    if (!id) {
      return NextResponse.json({ error: "Missing required field 'id'." }, { status: 400 });
    }

    const manager = SessionManager.getInstance();
    
    if (deleteSession) {
      const success = await manager.deleteSession(id);
      return NextResponse.json({ success });
    } else {
      const success = await manager.stopSession(id);
      return NextResponse.json({ success });
    }
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : "Unknown error";
    return NextResponse.json({ error: errorMessage }, { status: 500 });
  }
}
