import { NextResponse } from 'next/server'
import { getCurrentUser } from '@/lib/auth'
import { getDashboardLiveData } from '@/lib/services/live-data-service'

/**
 * GET /api/dashboard/live
 * Returns live dashboard stats for 10-second polling
 */
export async function GET() {
  try {
    const user = await getCurrentUser()
    if (!user) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
    }

    const data = await getDashboardLiveData()
    
    if (!data) {
      return NextResponse.json({ 
        error: 'Failed to fetch dashboard data',
        stats: null,
      }, { status: 500 })
    }

    return NextResponse.json({
      ...data,
      timestamp: new Date().toISOString(),
    })
  } catch (error) {
    console.error('[API] Error fetching live dashboard data:', error)
    return NextResponse.json({ 
      error: 'Internal server error',
      message: error instanceof Error ? error.message : 'Unknown error',
    }, { status: 500 })
  }
}
