import assert from 'node:assert';
import BoxError from './boxerror.js';
import child_process from 'node:child_process';
import logger from './logger.js';
import path from 'node:path';
import safe from 'safetydance';
import _ from './underscore.js';

const { log } = logger('shell');

function lineCount(buffer) {
    assert(Buffer.isBuffer(buffer));

    const NEW_LINE = Buffer.from('\n');
    let index = buffer.indexOf(NEW_LINE);
    let count = 0;
    while (index >= 0) {
        index = buffer.indexOf(NEW_LINE, index+1);
        ++count;
    }
    return count;
}

const SUDO = '/usr/bin/sudo';
const KILL_CHILD_CMD = path.join(import.meta.dirname, 'scripts/kill-child.sh');

function spawn(tag, file, args, options) {
    assert.strictEqual(typeof tag, 'string');
    assert.strictEqual(typeof file, 'string');
    assert(Array.isArray(args));
    assert.strictEqual(typeof options, 'object'); // note: spawn() has no encoding option of it's own

    log(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')}`);

    const maxLines = options.maxLines || Number.MAX_SAFE_INTEGER;
    const outputLogger = options.logger || null;
    const abortSignal = options.abortSignal || null; // note: we use our own handler and not the child_process one

    return new Promise((resolve, reject) => {
        const spawnOptions = _.omit(options, [ 'maxLines', 'logger', 'abortSignal', 'onMessage', 'input', 'encoding', 'timeout', 'onTimeout' ]);
        const cp = child_process.spawn(file, args, spawnOptions);
        const stdoutBuffers = [], stderrBuffers = [];
        let stdoutLineCount = 0, stderrLineCount = 0, killTimerId = null, timedOut = false, terminated = false;

        cp.stdout.on('data', (data) => {
            if (outputLogger) return outputLogger(data);
            stdoutBuffers.push(data);
            stdoutLineCount += lineCount(data);
            if (stdoutLineCount >= maxLines) return cp.kill('SIGKILL');
        });
        cp.stderr.on('data', (data) => {
            if (outputLogger) return outputLogger(data);
            stderrBuffers.push(data);
            stderrLineCount += lineCount(data);
            if (stderrLineCount >= maxLines) return cp.kill('SIGKILL');
        });

        cp.on('close', function (code, signal) { // always called. after 'exit' or 'error'
            const stdoutBuffer = Buffer.concat(stdoutBuffers);
            const stdout = options.encoding ? stdoutBuffer.toString(options.encoding) : stdoutBuffer;

            if (killTimerId) clearTimeout(killTimerId);

            // if terminated or timedout, the code is ignored
            if (!terminated && !timedOut && code === 0) return resolve(stdout);

            const stderrBuffer = Buffer.concat(stderrBuffers);
            const stderr = options.encoding ? stderrBuffer.toString(options.encoding) : stderrBuffer;

            const e = new BoxError(BoxError.SHELL_ERROR, `${file} exited with code ${code} signal ${signal}`);
            e.stdout = stdout; // when promisified, this is the way to get stdout
            e.stdoutString = options.encoding ? stdout : stdout.toString(); // make error printable
            e.stdoutLineCount = stdoutLineCount;
            e.stderr = stderr; // when promisified, this is the way to get stderr
            e.stderrString = options.encoding ? stderr : stderr.toString(); // make error printable
            e.stderrLineCount = stderrLineCount;
            e.code = code;
            e.signal = signal;
            e.timedOut = timedOut;
            e.terminated = terminated;

            log(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')} errored`, e);

            reject(e);
        });

        cp.on('error', function (error) { // when the command itself could not be started
            log(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')} errored`, error);
        });

        cp.terminate = function () {
            terminated = true;
            // many approaches to kill sudo launched process failed. we now have a sudo wrapper to kill the full tree
            child_process.execFile('/usr/bin/sudo', [ KILL_CHILD_CMD, cp.pid, process.pid ], { encoding: 'utf8' }, (error, stdout, stderr) => {
                if (error) log(`${tag}: failed to kill children`, stdout, stderr);
                else log(`${tag}: terminated ${cp.pid}`, stdout, stderr);
            });
        };

        abortSignal?.addEventListener('abort', () => {
            log(`${tag}: aborting ${cp.pid}`);
            cp.terminate();
        }, { once: true });

        if (options.onMessage) cp.on('message', options.onMessage); // ipc mode messages

        if (options.timeout) {
            killTimerId = setTimeout(async () => {
                log(`${tag}: timedout`);
                timedOut = true;
                if (typeof options.onTimeout !== 'function') return cp.terminate();
                await safe(options.onTimeout(), { debug: log });
            }, options.timeout);
        }

        // https://github.com/nodejs/node/issues/25231
        if ('input' in options) { // when empty, just closes
            cp.stdin.write(options.input);
            cp.stdin.end();
        }
    });
}

async function bash(tag, script, options) {
    assert.strictEqual(typeof tag, 'string');
    assert.strictEqual(typeof script, 'string');
    assert.strictEqual(typeof options, 'object');

    return await spawn(tag, '/bin/bash', [ '-c', script ], options);
}

async function sudo(tag, args, options) {
    assert.strictEqual(typeof tag, 'string');
    assert(Array.isArray(args));
    assert.strictEqual(typeof options, 'object');

    const sudoArgs = [ '--non-interactive' ]; // avoid prompting the user for input of any kind
    if (options.preserveEnv) sudoArgs.push('-E'); // -E preserves environment

    if (options.onMessage) { // enable ipc
         sudoArgs.push('--close-from=4'); // keep the ipc open. requires closefrom_override in sudoers file
         options.stdio = ['pipe', 'pipe', 'pipe', 'ipc'];
    }

    if (!options.encoding) options.encoding = 'utf8'; // better error message output

    const spawnArgs = [ ...sudoArgs, ...args ];
    return await spawn(tag, SUDO, spawnArgs, options);
}

function shell(tag) {
    assert.strictEqual(typeof tag, 'string');

    return {
        bash: bash.bind(null, tag),
        spawn: spawn.bind(null, tag),
        sudo: sudo.bind(null, tag),
    };
}

export default shell;
