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

// Persistent storage for trades using JSON file
const TRADES_FILE = path.join(process.cwd(), 'data', 'trades.json')

// Ensure data directory exists
const dataDir = path.join(process.cwd(), 'data')
if (!fs.existsSync(dataDir)) {
  fs.mkdirSync(dataDir, { recursive: true })
}

// Helper functions for persistent storage
function loadTrades() {
  try {
    if (fs.existsSync(TRADES_FILE)) {
      const data = fs.readFileSync(TRADES_FILE, 'utf8')
      return JSON.parse(data)
    }
  } catch (error) {
    console.error('Error loading trades:', error)
  }
  return []
}

function saveTrades(trades) {
  try {
    fs.writeFileSync(TRADES_FILE, JSON.stringify(trades, null, 2))
  } catch (error) {
    console.error('Error saving trades:', error)
  }
}

export async function GET() {
  try {
    // Load trades from persistent storage
    const tradesHistory = loadTrades()
    
    // Sort trades by timestamp (newest first)
    const sortedTrades = tradesHistory.sort((a, b) => b.timestamp - a.timestamp)
    
    return NextResponse.json({
      success: true,
      trades: sortedTrades,
      totalTrades: sortedTrades.length
    })

  } catch (error) {
    console.error('Error fetching trades history:', error)
    return NextResponse.json({
      success: false,
      error: 'Failed to fetch trades history',
      trades: []
    }, { status: 500 })
  }
}

export async function POST(request) {
  try {
    const body = await request.json()
    const { action, ...tradeData } = body

    if (action === 'add') {
      // Load existing trades
      const tradesHistory = loadTrades()
      
      // Add new trade to history
      const newTrade = {
        id: `trade_${Date.now()}_${Math.random().toString(36).substr(2, 8)}`,
        symbol: tradeData.symbol,
        side: tradeData.side,
        amount: parseFloat(tradeData.amount),
        price: parseFloat(tradeData.price),
        type: tradeData.type || 'market',
        status: tradeData.status || 'executed',
        timestamp: Date.now(),
        txId: tradeData.txId || null,
        fee: tradeData.fee || 0,
        pnl: tradeData.pnl || null, // For closing trades
        dex: tradeData.dex || 'JUPITER',
        notes: tradeData.notes || null
      }

      tradesHistory.push(newTrade)

      // Keep only last 100 trades to prevent memory issues
      if (tradesHistory.length > 100) {
        tradesHistory.splice(0, tradesHistory.length - 100)
      }

      // Save to persistent storage
      saveTrades(tradesHistory)

      return NextResponse.json({
        success: true,
        trade: newTrade,
        message: `Trade added to history: ${newTrade.side} ${newTrade.amount} ${newTrade.symbol}`
      })

    } else if (action === 'update') {
      // Load existing trades
      const tradesHistory = loadTrades()
      const { tradeId, updates } = tradeData
      
      // Find and update the trade
      const tradeIndex = tradesHistory.findIndex(trade => trade.id === tradeId)
      
      if (tradeIndex === -1) {
        return NextResponse.json({
          success: false,
          error: 'Trade not found'
        }, { status: 404 })
      }

      // Update the trade with new data
      tradesHistory[tradeIndex] = { ...tradesHistory[tradeIndex], ...updates }
      saveTrades(tradesHistory)

      return NextResponse.json({
        success: true,
        trade: tradesHistory[tradeIndex],
        message: `Trade updated: ${tradeId}`
      })

    } else if (action === 'clear') {
      // Clear trade history
      saveTrades([])
      
      return NextResponse.json({
        success: true,
        message: 'Trade history cleared'
      })
    }

    return NextResponse.json({
      success: false,
      error: 'Invalid action'
    }, { status: 400 })

  } catch (error) {
    console.error('Error managing trades history:', error)
    return NextResponse.json({
      success: false,
      error: 'Failed to manage trades history'
    }, { status: 500 })
  }
}
