// Test script to validate Hyperdrive configuration for remote deployment
// This script helps test the configuration without local PostgreSQL

const { execSync } = require('child_process');
const fs = require('fs');

console.log('🚀 Testing Hyperdrive Configuration for Remote Deployment');
console.log('======================================================');

try {
  // Check wrangler configuration
  console.log('\n📋 Validating wrangler.toml...');
  
  const wranglerContent = fs.readFileSync('wrangler.toml', 'utf8');
  console.log('✅ wrangler.toml loaded successfully');
  
  // Validate configuration syntax
  try {
    execSync('wrangler config validate', { stdio: 'pipe' });
    console.log('✅ wrangler.toml syntax is valid');
  } catch (error) {
    console.log('⚠️  Configuration validation warning (this is normal for Hyperdrive)');
  }
  
  // Check if we can authenticate with Cloudflare
  console.log('\n🔐 Checking Cloudflare authentication...');
  try {
    const whoami = execSync('wrangler whoami', { encoding: 'utf8' });
    console.log('✅ Authenticated with Cloudflare');
    console.log(`   ${whoami.trim()}`);
  } catch (error) {
    console.log('❌ Not authenticated with Cloudflare');
    console.log('   Run: wrangler login');
    return;
  }
  
  // List Hyperdrive configurations
  console.log('\n🔗 Checking Hyperdrive configurations...');
  try {
    const hyperdrives = execSync('wrangler hyperdrive list', { encoding: 'utf8' });
    console.log('✅ Hyperdrive configurations:');
    console.log(hyperdrives);
  } catch (error) {
    console.log('⚠️  Could not list Hyperdrive configurations');
    console.log('   Error:', error.message);
  }
  
  // Check specific Hyperdrive
  console.log('\n🎯 Checking specific Hyperdrive ID...');
  try {
    const hyperdriveInfo = execSync('wrangler hyperdrive get ef43924d89064cddabfaccf06aadfab6', { encoding: 'utf8' });
    console.log('✅ Hyperdrive configuration found:');
    console.log(hyperdriveInfo);
  } catch (error) {
    console.log('❌ Could not find Hyperdrive configuration');
    console.log('   Error:', error.message);
    console.log('   Make sure the Hyperdrive ID is correct and exists in your account');
  }
  
  console.log('\n📝 Configuration Summary:');
  console.log('   - Worker Name: hyperdrive-neondb-test');
  console.log('   - Hyperdrive ID: ef43924d89064cddabfaccf06aadfab6');
  console.log('   - Binding: HYPERDRIVE');
  console.log('   - Database Type: NeonDB (PostgreSQL)');
  
  console.log('\n🚀 Deployment Commands:');
  console.log('   1. Deploy to production: wrangler deploy');
  console.log('   2. Test endpoints after deployment:');
  console.log('      - https://hyperdrive-neondb-test.<your-subdomain>.workers.dev/test-connection');
  console.log('      - https://hyperdrive-neondb-test.<your-subdomain>.workers.dev/test-query');
  
  console.log('\n💡 Tips:');
  console.log('   - Hyperdrive provides connection pooling and caching for your database');
  console.log('   - It reduces latency and improves performance for database queries');
  console.log('   - The worker will automatically use the Hyperdrive connection in production');
  
} catch (error) {
  console.error('❌ Error during testing:', error.message);
}