// Simple configuration validation script
// This validates the wrangler.toml and Worker code without requiring API access

const fs = require('fs');
const path = require('path');

console.log('🔍 Validating Hyperdrive Configuration Files');
console.log('============================================');

// Check wrangler.toml
console.log('\n📋 Checking wrangler.toml...');
try {
  const wranglerContent = fs.readFileSync('wrangler.toml', 'utf8');
  console.log('✅ wrangler.toml exists');
  
  // Check for required fields
  const checks = [
    { field: 'name', regex: /name\s*=\s*["']([^"']+)["']/, required: true },
    { field: 'main', regex: /main\s*=\s*["']([^"']+)["']/, required: true },
    { field: 'compatibility_date', regex: /compatibility_date\s*=\s*["']([^"']+)["']/, required: true },
    { field: 'nodejs_compat', regex: /nodejs_compat/, required: true },
    { field: 'hyperdrive binding', regex: /binding\s*=\s*["']HYPERDRIVE["']/, required: true },
    { field: 'hyperdrive id', regex: /id\s*=\s*["']ef43924d89064cddabfaccf06aadfab6["']/, required: true }
  ];
  
  checks.forEach(check => {
    if (check.regex.test(wranglerContent)) {
      console.log(`   ✅ ${check.field} configured`);
    } else {
      console.log(`   ❌ ${check.field} missing or incorrect`);
    }
  });
  
} catch (error) {
  console.log('❌ wrangler.toml not found or unreadable');
}

// Check Worker code
console.log('\n📝 Checking Worker code...');
try {
  const workerContent = fs.readFileSync('src/index.ts', 'utf8');
  console.log('✅ src/index.ts exists');
  
  const codeChecks = [
    { name: 'Hyperdrive binding usage', regex: /env\.HYPERDRIVE/ },
    { name: 'Test connection endpoint', regex: /\/test-connection/ },
    { name: 'Test query endpoint', regex: /\/test-query/ },
    { name: 'PostgreSQL import', regex: /pg/ },
    { name: 'Error handling', regex: /try\s*{[\s\S]*catch/ }
  ];
  
  codeChecks.forEach(check => {
    if (check.regex.test(workerContent)) {
      console.log(`   ✅ ${check.name} implemented`);
    } else {
      console.log(`   ⚠️  ${check.name} not found`);
    }
  });
  
} catch (error) {
  console.log('❌ src/index.ts not found or unreadable');
}

// Check package.json
console.log('\n📦 Checking package.json...');
try {
  const packageContent = fs.readFileSync('package.json', 'utf8');
  const packageJson = JSON.parse(packageContent);
  console.log('✅ package.json exists and is valid JSON');
  
  const deps = {
    'pg': packageJson.dependencies?.pg,
    '@cloudflare/workers-types': packageJson.devDependencies?.['@cloudflare/workers-types'],
    '@types/pg': packageJson.devDependencies?.['@types/pg'],
    'typescript': packageJson.devDependencies?.typescript,
    'wrangler': packageJson.devDependencies?.wrangler
  };
  
  Object.entries(deps).forEach(([dep, version]) => {
    if (version) {
      console.log(`   ✅ ${dep}: ${version}`);
    } else {
      console.log(`   ❌ ${dep}: not found`);
    }
  });
  
} catch (error) {
  console.log('❌ package.json not found or invalid JSON');
}

console.log('\n📊 Configuration Summary:');
console.log('   - Project: hyperdrive-neondb-test');
console.log('   - Hyperdrive ID: ef43924d89064cddabfaccf06aadfab6');
console.log('   - Database: NeonDB (PostgreSQL)');
console.log('   - Binding: HYPERDRIVE');
console.log('   - Compatibility: nodejs_compat enabled');

console.log('\n🚀 Next Steps:');
console.log('   1. Ensure you have proper Cloudflare API permissions');
console.log('   2. Verify the Hyperdrive configuration exists in your Cloudflare dashboard');
console.log('   3. Deploy with: wrangler deploy');
console.log('   4. Test endpoints after deployment');

console.log('\n💡 Troubleshooting:');
console.log('   - If API token has insufficient permissions, use: wrangler login');
console.log('   - Check Hyperdrive exists: https://dash.cloudflare.com/[account-id]/workers/hyperdrive');
console.log('   - Verify NeonDB connection string is correct in Hyperdrive config');