// GET /api/shared/spaces?status=...&space_type=...
// Lists the local share ledger for spaces/projects (no network). Optional
// filters by status ('published'|'unpublished'|'error') and space_type.

import { listSharedSpaces } from '../../../../lib/shared-cloud.js';
import { requireSession } from '../../../../lib/auth.js';

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

export async function GET(req) {
  try {
    await requireSession(req);
    const { searchParams } = new URL(req.url);
    const spaces = listSharedSpaces({
      status: searchParams.get('status') || undefined,
      space_type: searchParams.get('space_type') || undefined,
    });
    return Response.json({ ok: true, spaces });
  } catch (err) {
    if (err && typeof err === 'object' && err.status) {
      return Response.json({ ok: false, error: err.error || 'unauthorized' }, { status: err.status });
    }
    return Response.json({ ok: false, error: err?.message || 'server_error' }, { status: 500 });
  }
}
