#!/usr/bin/env node

/**
 * Test Drift connection via API
 */

const fetch = require('node-fetch')

async function testDriftAPI() {
  console.log('🌊 Testing Drift API endpoints...')
  
  try {
    // Test login endpoint
    console.log('🔐 Testing login endpoint...')
    const response = await fetch('http://localhost:3000/api/drift/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      }
    })
    
    if (!response.ok) {
      throw new Error(`HTTP ${response.status}: ${response.statusText}`)
    }
    
    const result = await response.json()
    console.log('Login result:', JSON.stringify(result, null, 2))
    
    if (result.isLoggedIn) {
      console.log('✅ Login successful! Testing balance...')
      
      // Test balance endpoint
      const balanceResponse = await fetch('http://localhost:3000/api/drift/balance')
      if (balanceResponse.ok) {
        const balance = await balanceResponse.json()
        console.log('💰 Balance:', JSON.stringify(balance, null, 2))
      }
      
      // Test positions endpoint
      const positionsResponse = await fetch('http://localhost:3000/api/drift/positions')
      if (positionsResponse.ok) {
        const positions = await positionsResponse.json()
        console.log('📊 Positions:', positions.length)
      }
    }
    
  } catch (error) {
    console.error('❌ API test failed:', error.message)
  }
}

console.log('Please start the development server first with: npm run dev')
console.log('Then run this test script')

testDriftAPI().catch(console.error)
