/**
 * Install TestDataDB as a Windows Service
 *
 * Uses node-windows to register the server as a persistent service
 * with automatic restart on crash.
 *
 * Run: node install-service.js
 */

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

const svc = new Service({
    name: 'TestDataDB',
    description: 'Dataforth Test Data Database Server',
    script: path.join(__dirname, 'server.js'),
    nodeOptions: [],
    workingDirectory: __dirname,
    allowServiceLogon: true,
    // Restart configuration: max 3 restarts with 5-second delay
    maxRestarts: 3,
    maxRetries: 3,
    wait: 5,
    grow: 0.5
});

// Set log directory
svc.logpath = path.join('C:', 'Shares', 'testdatadb', 'logs');

svc.on('install', () => {
    console.log('[OK] TestDataDB service installed successfully.');
    console.log('[INFO] Starting service...');
    svc.start();
});

svc.on('start', () => {
    console.log('[OK] TestDataDB service started.');
});

svc.on('alreadyinstalled', () => {
    console.log('[WARNING] TestDataDB service is already installed.');
    console.log('[INFO] To reinstall, run uninstall-service.js first.');
});

svc.on('error', (err) => {
    console.error('[ERROR] Service installation failed:', err);
});

console.log('[INFO] Installing TestDataDB as a Windows service...');
console.log('[INFO] Log directory: C:\\Shares\\testdatadb\\logs\\');
svc.install();
