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

export async function POST(request: Request) {
  try {
    const body = await request.json();
    const { type, name, workspaceRoot, scopedDirs } = body;

    const allowed = ['mock', 'claude', 'claude-docker', 'claude-ssh', 'aider', 'aider-docker'];
    if (!type || !allowed.includes(type)) {
      return NextResponse.json({ error: `Invalid type. Must be one of: ${allowed.join(', ')}` }, { status: 400 });
    }

    const manager = SessionManager.getInstance();
    const session = await manager.createSession(type, name, workspaceRoot, scopedDirs);

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