#!/usr/bin/env npx tsx

/**
 * Quick Start Script for Credential Manager
 *
 * Run this script to see the credential manager in action:
 * npx tsx scripts/run-credential-manager-demo.ts
 */

import { CredentialManager } from '../src/core/credential-manager/CredentialManager'
import { Platform } from '../src/types/credential'
import { TradingOperation } from '../src/core/credential-manager/TradingIntegration'

async function quickDemo() {
  console.log('🚀 Quick Credential Manager Demo\n')

  // Initialize with minimal config
  const credentialManager = new CredentialManager()

  await credentialManager.loadConfiguration({
    accounts: [
      {
        id: 'test-pacifica',
        name: 'Test Pacifica Account',
        platform: Platform.PACIFICA,
        enabled: true,
        credentials: {
          type: 'ed25519' as const,
          privateKey: 'f26670e2ca334117f8859f9f32e50251641953a30b54f6ffcf82db836cfdfea5'
        }
      }
    ]
  })

  console.log('✅ Credential manager initialized')

  // Test basic signing
  const message = new TextEncoder().encode('Hello, multi-platform trading!')
  console.log('\n🔐 Testing signature...')

  const startTime = Date.now()
  const result = await credentialManager.sign('test-pacifica', message)
  const duration = Date.now() - startTime

  if (result.success) {
    console.log(`✅ Signature successful!`)
    console.log(`📋 Algorithm: ${result.algorithm}`)
    console.log(`⚡ Time: ${duration}ms`)
    console.log(`🔐 Signature: ${result.signature?.substring(0, 32)}...`)
  } else {
    console.log(`❌ Signature failed: ${result.error}`)
  }

  // Test trading integration
  console.log('\n💹 Testing trading integration...')

  const tradingRequest = {
    operation: TradingOperation.PLACE_ORDER,
    platform: Platform.PACIFICA,
    parameters: {
      symbol: 'SOL/USDC',
      side: 'buy' as const,
      quantity: 1.0,
      type: 'market' as const
    }
  }

  const tradingResult = await credentialManager.signTradingRequest(tradingRequest)

  if (tradingResult.success) {
    console.log(`✅ Trading request signed!`)
    console.log(`📋 Platform: ${tradingResult.selectedAccount.platform}`)
    console.log(`🏦 Account: ${tradingResult.selectedAccount.accountId}`)
    console.log(`📝 Format: ${tradingResult.platformSignature.format}`)
  }

  await credentialManager.shutdown()
  console.log('\n🎉 Demo completed! The credential manager is working perfectly.')
  console.log('\n📚 Next steps:')
  console.log('  1. Update config/credential-manager.example.json with your credentials')
  console.log('  2. Run the full demo: npx tsx src/examples/credential-manager-demo.ts')
  console.log('  3. Integrate with your trading system using the TradingIntegration interface')
}

quickDemo().catch(console.error)