import { NextResponse } from 'next/server'
import fs from 'fs'
import path from 'path'

export const dynamic = 'force-dynamic'

export async function GET() {
  try {
    // Try multiple possible paths
    const possiblePaths = [
      path.join(process.cwd(), '..', 'hostpioneers.com', 'public_html', 'contacts.json'),
      path.join(process.cwd(), 'public', '..', 'contacts.json'),
      '/var/www/hostpioneers.com/public_html/contacts.json',
    ]
    
    let data = '[]'
    for (const filePath of possiblePaths) {
      if (fs.existsSync(filePath)) {
        data = fs.readFileSync(filePath, 'utf-8')
        break
      }
    }
    
    const submissions = JSON.parse(data || '[]')
    return NextResponse.json({ submissions: submissions.reverse() })
  } catch (error) {
    console.error('Error:', error)
    return NextResponse.json({ submissions: [], error: 'Failed to fetch' }, { status: 200 })
  }
}
