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

// Verify that precompiled binaries exist for the current platform
const platform = os.platform();
const arch = os.arch();

const archMap = {
  'x64': 'x64',
  'ia32': 'x86',
  'arm64': 'arm64',
  'arm': 'arm'
};

const normalizedArch = archMap[arch] || arch;
const platformArch = `${platform}-${normalizedArch}`;

const prebuildPath = path.join(__dirname, '..', 'prebuilds', platformArch, 'HypertubeNodejsRuntimeAddon.node');

console.log(`Checking for precompiled binary...`);
console.log(`Platform: ${platformArch}`);
console.log(`Expected path: ${prebuildPath}`);

if (fs.existsSync(prebuildPath)) {
  const stats = fs.statSync(prebuildPath);
  console.log(`✓ Precompiled binary found!`);
  console.log(`  Size: ${(stats.size / 1024).toFixed(2)} KB`);
  console.log(`  Path: ${prebuildPath}`);
  
  // Try to load it
  try {
    const addon = require(prebuildPath);
    console.log(`✓ Binary loads successfully!`);
    console.log(`  Exports: ${Object.keys(addon).join(', ')}`);
    process.exit(0);
  } catch (err) {
    console.error(`✗ Binary exists but failed to load:`);
    console.error(`  ${err.message}`);
    process.exit(1);
  }
} else {
  console.error(`✗ Precompiled binary not found!`);
  console.error(`  Please build and package the binary first:`);
  console.error(`    npm run prebuild`);
  console.error(`    npm run package:prebuild ${platform} ${normalizedArch}`);
  process.exit(1);
}

