/**
 * GET /api/docs — List all project documentation files.
 * GET /api/docs?q=search+term — Full-text search across docs.
 */

import { NextRequest, NextResponse } from 'next/server';
import { listDocs, searchDocs } from '@/lib/docs';

export async function GET(req: NextRequest) {
  const q = req.nextUrl.searchParams.get('q');

  if (q) {
    const results = searchDocs(q);
    return NextResponse.json({ results, query: q });
  }

  const docs = listDocs();

  // Group by category
  const categories: Record<string, typeof docs> = {};
  for (const doc of docs) {
    const cat = doc.category;
    if (!categories[cat]) categories[cat] = [];
    categories[cat].push(doc);
  }

  return NextResponse.json({ docs, categories });
}
