import { NextResponse } from 'next/server'
import { bitqueryService } from '../../../lib/bitquery-service'

export async function GET() {
  try {
    console.log('📊 Fetching market data...')

    // Check if Bitquery service is configured
    if (!bitqueryService.isConfigured()) {
      return NextResponse.json(
        { 
          success: false, 
          error: 'Bitquery service not configured' 
        },
        { status: 503 }
      )
    }

    // Get token prices from Bitquery
    const prices = await bitqueryService.getTokenPrices(['SOL', 'BTC', 'ETH'])

    // Get service status
    const status = await bitqueryService.getServiceStatus()

    return NextResponse.json({
      success: true,
      data: {
        prices,
        bitqueryStatus: status,
        lastUpdate: Date.now()
      },
      timestamp: Date.now()
    })

  } catch (error) {
    console.error('❌ Market data API error:', error)
    return NextResponse.json(
      { 
        success: false, 
        error: 'Failed to fetch market data',
        message: error.message 
      },
      { status: 500 }
    )
  }
}
