import { createProject, listProjects } from '../../../lib/projects.js';
import { requireSession } from '@/lib/auth.js';
export const dynamic = 'force-dynamic';
export const runtime = 'nodejs';
export async function GET(req) {
  try {
    await requireSession(req);
    const url = new URL(req.url);
    // TODO: scope to session.org_id once listProjects supports org_id filter:
    // listProjects({ status, org_id: session.org_id })
    return Response.json({ projects: listProjects({ status: url.searchParams.get('status') || 'active' }) });
  } catch (err) {
    if (err && typeof err === 'object' && err.status) {
      return Response.json({ error: err.error || 'unauthorized' }, { status: err.status });
    }
    throw err;
  }
}
export async function POST(req) {
  try {
    await requireSession(req);
    const body = await req.json();
    return Response.json({ project: createProject(body) }, { status: 201 });
  } catch (err) {
    if (err && typeof err === 'object' && err.status) {
      return Response.json({ error: err.error || 'unauthorized' }, { status: err.status });
    }
    return Response.json({ error: err.message }, { status: err.code === 'PROJECT_SLUG_EXISTS' ? 409 : 400 });
  }
}
