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 analysis = await prisma.processAnalysis.findFirst({
    where: {
      id,
      userId: session.user.id,
    },
    select: {
      id: true,
      title: true,
      filename: true,
      fileSize: true,
      extractedText: true,
      analysis: true,
      summary: true,
      status: true,
      createdAt: true,
    },
  })

  if (!analysis) {
    return NextResponse.json({ error: 'Análise não encontrada' }, { status: 404 })
  }

  return NextResponse.json({ analysis })
}
