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

export async function GET(request: Request) {
  try {
    const { searchParams } = new URL(request.url);
    const id = searchParams.get("id");
    const manager = SessionManager.getInstance();

    if (id) {
      const session = manager.getSession(id);
      if (!session) {
        return NextResponse.json({ error: "Session not found" }, { status: 404 });
      }
      return NextResponse.json({
        ...session.getStatus(),
        logs: session.logs,
      });
    }

    const sessions = manager.listSessions();
    return NextResponse.json({ sessions });
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : "Unknown error";
    return NextResponse.json({ error: errorMessage }, { status: 500 });
  }
}
