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) // Automatically create position for this trade try { const positionResponse = await fetch('http://localhost:3000/api/trading/positions', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'add', symbol: mockTrade.symbol, side: mockTrade.side.toUpperCase(), amount: mockTrade.amount, entryPrice: mockTrade.price, txId: mockTrade.id }) }) if (positionResponse.ok) { const positionData = await positionResponse.json() console.log('Position created:', positionData.position?.id) } } catch (error) { console.error('Failed to create position:', error) } 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 } ] }) }