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, text } = body;

    if (!id || text === undefined) {
      return NextResponse.json({ error: "Missing required fields 'id' or 'text'." }, { status: 400 });
    }

    const manager = SessionManager.getInstance();
    const session = manager.getSession(id);

    if (!session) {
      return NextResponse.json({ error: "Session not found" }, { status: 404 });
    }

    await session.sendInput(text);

    return NextResponse.json({
      success: true,
      logs: session.logs
    });
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : "Unknown error";
    return NextResponse.json({ error: errorMessage }, { status: 500 });
  }
}
