import { NextResponse } from 'next/server'

export async function POST(request: Request) {
  try {
    const body = await request.json()
    const { symbol, side, amount, type = 'market' } = body

    // Validate input
    if (!symbol || !side || !amount) {
      return NextResponse.json({
        error: 'Missing required fields: symbol, side, amount'
      }, { status: 400 })
    }

    // Mock trading execution
    const mockTrade = {
      id: `trade_${Date.now()}`,
      symbol,
      side, // 'buy' or 'sell'
      amount: parseFloat(amount),
      type,
      price: side === 'buy' ? 144.11 : 144.09, // Mock prices
      status: 'executed',
      timestamp: new Date().toISOString(),
      fee: parseFloat(amount) * 0.001 // 0.1% fee
    }

    console.log('Simulated trade executed:', mockTrade)

    return NextResponse.json({
      success: true,
      trade: mockTrade,
      message: `Successfully ${side} ${amount} ${symbol}`
    })
  } catch (error) {
    return NextResponse.json({
      error: 'Failed to execute trade',
      message: error instanceof Error ? error.message : 'Unknown error'
    }, { status: 500 })
  }
}

export async function GET() {
  return NextResponse.json({
    message: 'Trading endpoint is active. Use POST to execute trades.',
    supportedMethods: ['POST'],
    requiredFields: ['symbol', 'side', 'amount'],
    optionalFields: ['type'],
    positions: [
      { symbol: 'SOL', size: 1.5, entryPrice: 140.50, markPrice: 144.11, unrealizedPnl: 5.415 },
      { symbol: 'ETH', size: 0.1, entryPrice: 2350, markPrice: 2400, unrealizedPnl: 5.0 }
    ]
  })
}
