import {
  listSpacesWithArtifactIds,
  createSpace,
} from '../../../../lib/dashboard-spaces.js';

export async function GET() {
  try {
    return Response.json(listSpacesWithArtifactIds());
  } catch (err) {
    console.error('[api/dashboard/spaces GET] error:', err?.stack || err);
    return Response.json({ error: err?.message || String(err) }, { status: 500 });
  }
}

export async function POST(request) {
  try {
    const body = await request.json().catch(() => ({}));
    const space = createSpace({ name: body?.name });
    return Response.json({ space }, { status: 201 });
  } catch (err) {
    const status = err?.code === 'SPACE_LIMIT' ? 409 : 400;
    return Response.json({ error: err?.message || String(err), code: err?.code }, { status });
  }
}
