import { buildOnboardingBlueprint, launchOnboardingBlueprint } from '@/lib/onboarding.js';

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

export async function POST(request) {
  const body = await request.json().catch(() => ({}));
  const blueprint = body?.blueprint?.agents?.length ? body.blueprint : buildOnboardingBlueprint(body || {});
  const result = launchOnboardingBlueprint(blueprint);
  return Response.json({
    ok: result.ok,
    created_agents: result.created_agents.map(agent => ({ id: agent.id, name: agent.name, engine: agent.engine, model: agent.model })),
    schedules: result.schedules.map(schedule => ({ id: schedule.id, agent_id: schedule.agent_id, schedule: schedule.schedule, next_run_at: schedule.next_run_at })),
    blueprint: result.blueprint,
  });
}
