import { NextRequest, NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { prisma } from '@/lib/prisma'

export async function GET(
  req: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  const session = await getServerSession(authOptions)
  if (!session?.user?.id) {
    return NextResponse.json({ error: 'Não autorizado' }, { status: 401 })
  }

  const { id } = await params

  const audit = await prisma.contractAudit.findFirst({
    where: { id, userId: session.user.id },
  })

  if (!audit) {
    return NextResponse.json({ error: 'Auditoria não encontrada' }, { status: 404 })
  }

  return NextResponse.json({ audit })
}
