import { NextRequest, NextResponse } from 'next/server'
import { db } from '@/lib/db'

// GET /api/patients/[id]/ordonnances - Get all ordonnances for a patient
export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params
    const ordonnances = await db.ordonnance.findMany({
      where: { patientId: id },
      include: {
        fichiers: true,
        patient: {
          include: {
            client: true
          }
        }
      },
      orderBy: {
        dateEmission: 'desc'
      }
    })

    return NextResponse.json(ordonnances)
  } catch (error) {
    console.error('Error fetching ordonnances:', error)
    return NextResponse.json(
      { error: 'Failed to fetch ordonnances' },
      { status: 500 }
    )
  }
}
