import { NextRequest, NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import prisma from '@/lib/prisma'

export const dynamic = 'force-dynamic'

export async function GET(req: NextRequest) {
  try {
    const session = await getServerSession(authOptions)
    if (!session?.user?.id) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })

    const servers = await prisma.pterodactylServer.findMany({
      where: { userId: session.user.id },
      include: {
        package: true,
        renewals: {
          orderBy: { paidAt: 'desc' },
          take: 5
        }
      },
      orderBy: { createdAt: 'desc' }
    })

    return NextResponse.json(servers)
  } catch (error) {
    return NextResponse.json({ error: 'Gagal memuat server' }, { status: 500 })
  }
}
