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

console.log('🔧 Setting up Thermal Printer Service for Linux...\n');

// Create necessary directories
if (!fs.existsSync('logs')) {
  fs.mkdirSync('logs');
}

console.log('✅ Directories created');

// Set permissions for USB (Linux specific)
console.log('\n🔧 Setting USB permissions...');
const usbRules = `SUBSYSTEM=="usb", ATTR{idVendor}=="04b8", ATTR{idProduct}=="0e15", MODE="0666"
SUBSYSTEM=="usb", ATTR{idVendor}=="0416", ATTR{idProduct}=="5011", MODE="0666"`;

fs.writeFileSync('99-thermal-printer.rules', usbRules);

console.log('📋 To set USB permissions permanently:');
console.log('   sudo cp 99-thermal-printer.rules /etc/udev/rules.d/');
console.log('   sudo udevadm control --reload-rules');
console.log('   sudo udevadm trigger');
console.log('\nOr temporarily:');
console.log('   sudo chmod 666 /dev/usb/lp0');

// Check Node.js version
exec('node --version', (error, stdout) => {
  if (error) {
    console.log('❌ Node.js not found!');
    console.log('📥 Install Node.js: curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - && sudo apt install -y nodejs');
  } else {
    console.log(`✅ Node.js ${stdout.trim()} detected`);
  }
});

console.log('\n🎉 Setup complete!');
console.log('\n🚀 To start the service:');
console.log('   npm start');
console.log('\n📡 Endpoints:');
console.log('   • http://localhost:5000/health');
console.log('   • http://localhost:5000/status');
console.log('   • http://localhost:5000/print');
