'use strict';
/**
 * DeployPro — Windows Service Status Check
 * node service\status.js
 */

const { execFile } = require('child_process');

function runSC(args) {
  return new Promise(resolve => {
    execFile('sc.exe', args, (err, stdout) => resolve((stdout || '').trim()));
  });
}

(async () => {
  console.log('\n╔══════════════════════════════════════════╗');
  console.log('║   DeployPro — Service Status             ║');
  console.log('╚══════════════════════════════════════════╝\n');

  // node-windows registers the service as 'deploypro.exe' (auto-generated from display name)
  const query = await runSC(['query', 'deploypro.exe']);
  if (!query || query.includes('does not exist') || query.includes('FAILED')) {
    console.log('  Status : NOT INSTALLED');
    console.log('\n  Run install-service.bat to install.\n');
    process.exit(0);
  }

  const stateMatch = query.match(/STATE\s*:\s*\d+\s+([\w_]+)/);
  const state = stateMatch ? stateMatch[1] : 'UNKNOWN';

  const config = await runSC(['qc', 'deploypro.exe']);
  const pathMatch = config.match(/BINARY_PATH_NAME\s*:\s*(.+)/);
  const binPath = pathMatch ? pathMatch[1].trim() : '—';

  const startMatch = config.match(/START_TYPE\s*:\s*\d+\s+([\w_]+)/);
  const startType = startMatch ? startMatch[1] : '—';

  console.log(`  Service Name : DeployPro`);
  console.log(`  State        : ${state}`);
  console.log(`  Start Type   : ${startType}`);
  console.log(`  Binary Path  : ${binPath}`);
  console.log(`  Web UI       : http://localhost:7070`);
  console.log('');
  process.exit(0);
})();
