require('dotenv').config();
const { Connection, Keypair } = require('@solana/web3.js');

async function testBasicSetup() {
  console.log('🔍 Testing basic wallet setup...');
  
  try {
    // Test environment variables
    console.log('📋 Environment check:');
    console.log('- SOLANA_RPC_URL:', process.env.SOLANA_RPC_URL ? '✅ Set' : '❌ Missing');
    console.log('- SOLANA_PRIVATE_KEY:', process.env.SOLANA_PRIVATE_KEY ? '✅ Set' : '❌ Missing');
    
    if (!process.env.SOLANA_PRIVATE_KEY) {
      throw new Error('SOLANA_PRIVATE_KEY not found in environment');
    }
    
    // Test keypair creation
    console.log('\n🔑 Testing keypair creation...');
    const secret = process.env.SOLANA_PRIVATE_KEY;
    const keypair = Keypair.fromSecretKey(Buffer.from(JSON.parse(secret)));
    console.log('✅ Keypair created successfully');
    console.log('🔑 Public key:', keypair.publicKey.toString());
    
    // Test connection
    console.log('\n🌐 Testing Solana connection...');
    const rpcUrl = process.env.SOLANA_RPC_URL || 'https://api.mainnet-beta.solana.com';
    const connection = new Connection(rpcUrl, 'confirmed');
    
    // Test balance check
    const balance = await connection.getBalance(keypair.publicKey);
    console.log('✅ Connection successful');
    console.log('💰 SOL Balance:', (balance / 1e9).toFixed(4), 'SOL');
    
    if (balance === 0) {
      console.log('⚠️  Warning: Wallet has 0 SOL balance. You need SOL for transactions.');
    }
    
    console.log('\n✅ Basic setup is working correctly!');
    console.log('🔄 The issue might be with Drift SDK or user account initialization.');
    
  } catch (error) {
    console.error('❌ Basic setup failed:', error.message);
    console.error('Stack:', error.stack);
  }
}

testBasicSetup().catch(console.error);
