import { NextResponse } from 'next/server'
import { prisma } from '@/lib/db'
import { requireAdmin } from '@/lib/auth'

export async function GET() {
  try {
    const adminCheck = await requireAdmin()
    if (adminCheck) return adminCheck

    const history = await prisma.updateHistory.findMany({
      orderBy: { appliedAt: 'desc' },
      take: 50,
      include: {
        user: {
          select: { username: true }
        }
      }
    })

    return NextResponse.json(history)
  } catch (error) {
    console.error('Failed to fetch update history:', error)
    return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
  }
}
