#!/usr/bin/env node

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

const BIN_DIR = path.join(__dirname, '..', 'bin');

function getPlatformInfo() {
  const platform = process.platform;
  const arch = process.arch;
  
  return {
    platform,
    arch,
    ext: platform === 'win32' ? '.exe' : ''
  };
}

async function installBinary() {
  console.log('🐝 Installing HiveTechs Consensus...');
  
  const { ext } = getPlatformInfo();
  
  try {
    // The binary is already included in the npm package
    const packagedBinary = path.join(BIN_DIR, `hive${ext}`);
    
    if (!fs.existsSync(packagedBinary)) {
      throw new Error('Binary not found in package. Please report this issue.');
    }
    
    // Make sure it's executable on Unix systems
    if (process.platform !== 'win32') {
      fs.chmodSync(packagedBinary, '755');
    }
    
    // Verify the binary works
    try {
      execSync(`"${packagedBinary}" --version`, { stdio: 'ignore' });
    } catch (e) {
      console.warn('⚠️  Could not verify binary, but installation completed');
    }
    
    console.log('✅ HiveTechs Consensus installed successfully!');
    console.log('🚀 Run \'hive --help\' to get started');
    console.log('');
    console.log('📚 Quick start:');
    console.log('   hive quickstart    # Set up API keys and profiles');
    console.log('   hive ask "..."     # Ask a question');
    console.log('   hive tui           # Launch interactive interface');
    
  } catch (error) {
    console.error('❌ Installation failed:', error.message);
    process.exit(1);
  }
}

// Run installation
installBinary();