import { NextResponse } from 'next/server'
import { prisma } from '@/lib/db'
import { requireAdmin } from '@/lib/auth-helpers'
import { MOCK_MESSAGES } from '@/lib/mock'

const USE_MOCK = process.env.USE_MOCK === 'true'

export async function GET() {
  try {
    await requireAdmin()
    const data = USE_MOCK
      ? MOCK_MESSAGES
      : await prisma.contactMessage.findMany({
          where: { deletedAt: null },
          orderBy: { createdAt: 'desc' },
        })
    return NextResponse.json({ data })
  } catch {
    return NextResponse.json({ error: 'Server error' }, { status: 500 })
  }
}
