import { NextRequest, NextResponse } from 'next/server'
import { getSessionFromCookie, revokeSession, clearSessionCookie, logAudit } from '@/lib/auth'

export async function POST(request: NextRequest) {
  const session = await getSessionFromCookie()

  if (session) {
    await revokeSession(session.sessionId)
    await logAudit(session.sub, 'logout', {}, request, undefined, session.sessionId)
  }

  await clearSessionCookie()
  return NextResponse.json({ success: true })
}
