'use strict';
/**
 * DeployPro — Windows Service Uninstaller
 * Run this script as Administrator:
 *   node service\uninstall.js
 */

const path    = require('path');
const Service = require('node-windows').Service;

const ROOT = path.resolve(__dirname, '..');

const svc = new Service({
  name:   'DeployPro',
  id:     'DeployPro',
  script:  path.join(ROOT, 'server.js'),
});

svc.on('uninstall', () => {
  console.log('\n[DeployPro] Service uninstalled successfully.');
  console.log('[DeployPro] The application files remain intact.');
  console.log('[DeployPro] To reinstall, run: node service\\install.js');
  process.exit(0);
});

svc.on('stop', () => {
  console.log('[DeployPro] Service stopped. Uninstalling...');
  svc.uninstall();
});

svc.on('notinstalled', () => {
  console.log('[DeployPro] Service is not currently installed.');
  process.exit(0);
});

svc.on('error', (err) => {
  console.error('[DeployPro] Service error:', err);
  process.exit(1);
});

console.log('\n╔══════════════════════════════════════════╗');
console.log('║  DeployPro — Windows Service Uninstaller ║');
console.log('╚══════════════════════════════════════════╝\n');
console.log('[DeployPro] Stopping and uninstalling service: DeployPro\n');

// Stop first (if running), then uninstall
if (svc.exists) {
  svc.stop();
  // 'stop' event fires → calls svc.uninstall()
} else {
  svc.uninstall();
}
