import { NextRequest, NextResponse } from 'next/server'
import pool from '@/lib/db'
import { requireAdmin, getAdminFromCookie } from '@/lib/admin-auth'
import { logAudit } from '@/lib/auth'

interface Ctx { params: Promise<{ id: string; investorId: string }> }

export async function DELETE(request: NextRequest, ctx: Ctx) {
  const guard = await requireAdmin(request)
  if (guard.kind === 'response') return guard.response

  const { id, investorId } = await ctx.params
  const admin = await getAdminFromCookie()

  await pool.query(
    `DELETE FROM dataroom_releases WHERE document_id = $1 AND investor_id = $2`,
    [id, investorId],
  )

  await logAudit(null, 'dataroom_release_revoked', { document_id: id, investor_id: investorId }, request, undefined, undefined, admin?.id)
  return NextResponse.json({ success: true })
}
