import { NextResponse } from 'next/server'
import { requireAdmin, getSession } from '@/lib/auth'
import { createAuditLog } from '@/lib/audit'

export async function DELETE(request: Request) {
  try {
    const adminCheck = await requireAdmin()
    if (adminCheck) return adminCheck

    const session = await getSession()

    // In production, this would clear:
    // 1. Redis cache
    // 2. In-memory caches
    // 3. CDN cache if applicable

    // Log the action
    await createAuditLog({
      userId: session?.userId,
      action: 'CACHE_CLEARED',
      category: 'MAINTENANCE',
      details: { timestamp: new Date().toISOString() },
      ipAddress: request.headers.get('x-forwarded-for') || undefined
    })

    return NextResponse.json({ success: true })
  } catch (error) {
    console.error('Failed to clear cache:', error)
    return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
  }
}
