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

export const dynamic = 'force-dynamic'

export async function GET(_request: NextRequest) {
  const session = await getSessionFromCookie()
  if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
  const investorId = session.sub

  const { rows } = await pool.query(
    `SELECT d.id, d.filename, d.display_name, d.description_de, d.description_en,
            d.mime_type, d.file_size, r.released_at
     FROM dataroom_releases r
     JOIN dataroom_documents d ON d.id = r.document_id
     WHERE r.investor_id = $1
     ORDER BY r.released_at DESC`,
    [investorId],
  )
  return NextResponse.json({ documents: rows })
}
