import { prisma } from '@/lib/prisma';
import { NextRequest, NextResponse } from 'next/server';

export async function GET(request: NextRequest) {
  const url = new URL(request.url);
  const clientId = parseInt(url.searchParams.get('clientId') || '', 10);
  const section = url.searchParams.get('section');
  if (isNaN(clientId)) {
    return NextResponse.json({ error: 'ID client invalide' }, { status: 400 });
  }
  try {
    const where: any = { clientId };
    if (section) where.documentCategory = section;
    const documents = await prisma.document.findMany({
      where,
      orderBy: { uploadDate: 'desc' },
    });
    const docsWithUrl = documents.map(doc => ({
      ...doc,
      downloadUrl: `/api/clients/${clientId}/documents/${doc.id}/download`
    }));
    return NextResponse.json(docsWithUrl, { status: 200 });
  } catch (e) {
    return NextResponse.json({ error: 'Erreur lors de la récupération des documents' }, { status: 500 });
  }
} 