#!/usr/bin/env -S node --unhandled-rejections=strict

import apptask from './apptask.js';
import backupCleaner from './backupcleaner.js';
import backupIntegrity from './backupintegrity.js';
import backuptask from './backuptask.js';
import BoxError from './boxerror.js';
import dashboard from './dashboard.js';
import database from './database.js';
import dns from './dns.js';
import dyndns from './dyndns.js';
import externalLdap from './externalldap.js';
import fs from 'node:fs';
import locks from './locks.js';
import mailServer from './mailserver.js';
import net from 'node:net';
import reverseProxy from './reverseproxy.js';
import safe from 'safetydance';
import tasks from './tasks.js';
import timers from 'timers/promises';
import updater from './updater.js';
import logger from './logger.js';

const TASKS = { // indexed by task type
    app: apptask.run,
    appBackup: backuptask.appBackup,
    backup: backuptask.fullBackup,
    boxUpdate: updater.updateBox,
    checkCerts: reverseProxy.checkCerts,
    prepareDashboardLocation: dashboard.prepareLocation,
    cleanBackups: backupCleaner.run,
    syncExternalLdap: externalLdap.sync,
    changeMailLocation: mailServer.changeLocation,
    syncDnsRecords: dns.syncDnsRecords,
    syncDyndns: dyndns.sync,
    checkBackupIntegrity: backupIntegrity.check,

    // testing
    identity: async (arg, progressCallback) => { progressCallback({ percent: 20 }); return arg; },
    error: async (arg, progressCallback) => { progressCallback({ percent: 20 }); throw new Error(`Failed for arg: ${arg}`); },
    crash: (arg) => { throw new Error(`Crashing for arg: ${arg}`); }, // the test looks for this debug string in the log file
    sleep: async (arg) => await timers.setTimeout(parseInt(arg, 10))
};

if (process.argv.length !== 4) {
    console.error('Pass the taskid and logfile as argument');
    process.exit(1);
}

const taskId = process.argv[2];
const logFile = process.argv[3];
let logFd = null;

async function setupLogging() {
    logFd = fs.openSync(logFile, 'a');
    // we used to write using a stream before but it caches internally and there is no way to flush it when things crash
    process.stdout.write = process.stderr.write = function (...args) {
        const callback = typeof args[args.length-1] === 'function' ? args.pop() : function () {}; // callback is required for fs.write
        fs.write.apply(fs, [logFd, ...args, callback]);
    };
    process.stdout.logFile = logFile; // used by update task
}

// happy eyeballs workaround. see box.js for detailed note
async function setupNetworking() {
    net.setDefaultAutoSelectFamilyAttemptTimeout(2500);
}

// this is also used as the 'uncaughtException' handler which can only have synchronous functions
// taskworker.sh forwards the exit code of the actual worker. It's either a raw signal number OR the exit code. So, choose exit codes > 31
// 50 - internal error , 70 - SIGTERM exit
function exitSync(status) {
    if (status.error) fs.write(logFd, status.error.stack + '\n', function () {});
    fs.write(logFd, `${(new Date()).toISOString()} Exiting with code ${status.code}\n`, function () {});
    fs.fsyncSync(logFd);
    fs.closeSync(logFd);
    process.exit(status.code);
}

function toTaskError(runError) {
    if (runError instanceof BoxError) return runError.toPlainObject();
    return {
        message: `Task crashed. ${runError.message}`,
        stack: runError.stack,
        code: tasks.ECRASHED
    };
}

// Main process starts here
const startTime = new Date();

async function main() {
    try {
        await setupLogging();
        await setupNetworking();
        await database.initialize();
        locks.setTaskId(taskId);
    } catch (initError) {
        console.error(initError);
        return process.exit(50);
    }

    const { log } = logger('taskworker'); // import this here so that logging handler is already setup

    process.on('SIGTERM', () => {
        log('Terminated');
        exitSync({ code: 70 });
    });

    // ensure we log task crashes with the task logs. neither console.log nor debug are sync for some reason
    process.on('uncaughtException', (error) => exitSync({ error, code: 1 }));

    log(`Starting task ${taskId}. Logs are at ${logFile}`);

    const [getError, task] = await safe(tasks.get(taskId));
    if (getError) return exitSync({ error: getError, code: 50 });
    if (!task) return exitSync({ error: new Error(`Task ${taskId} not found`), code: 50 });

    async function progressCallback(progress) {
        await safe(tasks.update(taskId, progress), { debug: log });
    }

    const taskName = task.type.replace(/_.*/,'');
    log(`Running task of type ${taskName}`);
    const [runError, result] = await safe(TASKS[taskName].apply(null, task.args.concat(progressCallback)));
    const progress = {
        result: result || null,
        error: runError ? toTaskError(runError) : null,
        percent: 100
    };

    await safe(tasks.setCompleted(taskId, progress), { debug: log });

    log(`Task took ${(new Date() - startTime)/1000} seconds`);

    exitSync({ error: runError, code: (!runError || runError instanceof BoxError) ? 0 : 50 }); // handled error vs run time crash
}

main();
