import { requireSession } from '@/lib/auth.js';
import { getProject, getProjectWorkspaceSummary } from '@/lib/projects.js';

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

export async function GET(req, { params }) {
  try {
    await requireSession(req);
    const { slug } = await params;
    const project = getProject(slug);
    if (!project) return Response.json({ error: 'not found' }, { status: 404 });
    const summary = getProjectWorkspaceSummary(project.id, { limit: 100, changelogLimit: 50 });
    return Response.json({
      project,
      libraryItems: summary?.libraryItems || [],
      folders: summary?.folders || [],
      changelog: summary?.changelog || [],
    });
  } 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 || 'failed to load project library' }, { status: 500 });
  }
}
