import { exec } from 'child_process';

export function shutdown(): Promise<{ succeed: boolean; message?: string }> {
	if (process.platform === 'win32') {
		return shutdownWindows();
	}

	return Promise.resolve({
		succeed: false,
		message: 'Platform not supported',
	});
}

function shutdownWindows(): Promise<{ succeed: boolean; message?: string }> {
	return new Promise<{ succeed: boolean; message?: string }>(
		(resolve, reject) => {
			exec('shutdown /s /t 5', (error, stdout, stderr) => {
				if (error) {
					console.error(
						`Error shutting down Windows: ${error.message}`
					);
					return resolve({ succeed: false, message: error.message });
				}
				if (stderr) {
					console.error(`Error shutting down Windows: ${stderr}`);
					return resolve({ succeed: false, message: stderr });
				}
				console.log(`Windows shutdown command executed: ${stdout}`);
				resolve({ succeed: true });
			});
		}
	);
}
