// Simple test of wallet functionality outside Electron
const ClipsCrypto = require('./modules/clips-crypto.js');
const fs = require('fs');
const path = require('path');

console.log('Testing PaperclipWallet core functionality...\n');

// Test 1: Key generation
console.log('1. Testing key generation:');
const keyPair = ClipsCrypto.generateKeyPair();
console.log('   Generated address:', keyPair.address);
console.log('   Address format valid:', ClipsCrypto.isValidAddress(keyPair.address));
console.log('   Private key length:', keyPair.privateKey.length);
console.log('   Public key length:', keyPair.publicKey.length);

// Test 2: Address validation
console.log('\n2. Testing address validation:');
const validAddresses = [
  keyPair.address,
  'CLIP-1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF'
];
const invalidAddresses = [
  'CLIP-123',
  'ETH-1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF',
  'CLIP-GGGG567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF',
  'CLIP1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF'
];

validAddresses.forEach(addr => {
  console.log(`   ${addr}: ${ClipsCrypto.isValidAddress(addr) ? 'VALID' : 'INVALID'}`);
});

invalidAddresses.forEach(addr => {
  console.log(`   ${addr}: ${ClipsCrypto.isValidAddress(addr) ? 'VALID' : 'INVALID'}`);
});

// Test 3: Transaction creation and signing
console.log('\n3. Testing transaction creation:');
const transaction = ClipsCrypto.createTransaction({
  sender: keyPair.address,
  receiver: 'CLIP-1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF',
  amount: 1000,
  nonce: 0,
  type: 'transfer',
  data: '',
  privateKey: keyPair.privateKey
});

console.log('   Transaction created successfully:', !!transaction);
console.log('   Transaction signed:', !!transaction.signature);
console.log('   Signature length:', transaction.signature ? transaction.signature.length : 0);

// Test 4: Transaction verification
console.log('\n4. Testing transaction verification:');
const isValid = ClipsCrypto.verifyTransaction(transaction, keyPair.publicKey);
console.log('   Transaction signature valid:', isValid);

// Test 5: Transaction serialization
console.log('\n5. Testing transaction serialization:');
const txHex = ClipsCrypto.transactionToHex(transaction);
console.log('   Transaction hex length:', txHex.length);
console.log('   Transaction hex (first 100 chars):', txHex.substring(0, 100) + '...');

const deserializedTx = ClipsCrypto.hexToTransaction(txHex);
console.log('   Deserialization successful:', deserializedTx.sender === transaction.sender);

// Test 6: Private key import
console.log('\n6. Testing private key import:');
try {
  const importedWallet = ClipsCrypto.importPrivateKey(keyPair.privateKey);
  console.log('   Import successful:', importedWallet.address === keyPair.address);
  console.log('   Imported address:', importedWallet.address);
} catch (error) {
  console.log('   Import failed:', error.message);
}

// Test 7: Seed phrase generation
console.log('\n7. Testing seed phrase generation:');
const seedPhrase = ClipsCrypto.generateSeedPhrase();
console.log('   Seed phrase:', seedPhrase);
console.log('   Word count:', seedPhrase.split(' ').length);

console.log('\nAll core wallet functionality tests completed!');
console.log('\nThe wallet crypto layer is working correctly.');
console.log('Next step: Test the full wallet with Electron interface.');