
(function () {
    var scriptFile = new File($.fileName);
    var scriptDir = scriptFile.parent;

    var scriptDirPath = scriptDir.fsName.replace(/\\/g, "/");
    var libDir = new Folder(scriptDirPath + "/lib");

    var modules = [
        "lib/00_logger",
        "lib/06_versioning",
        "lib/01_json_polyfill",
        "lib/i18n/en_us",
        "lib/i18n/es_hn",
        "lib/07_i18n",
        "lib/02_settings",
        "lib/03_ui_builder",
        "lib/04_core_logic",
        "lib/05_security"
    ];

    var isProduction = true;

    for (var i = 0; i < modules.length; i++) {
        var moduleBin = new File(scriptDirPath + "/" + modules[i] + ".jsxbin");
        var moduleJsx = new File(scriptDirPath + "/" + modules[i] + ".jsx");

        var moduleFile = null;
        if (moduleBin.exists) {
            moduleFile = moduleBin;
        } else if (moduleJsx.exists) {
            moduleFile = moduleJsx;
            isProduction = false; // Using .jsx = dev mode
        } else {
            alert("Error: Required module not found.\n\n" +
                "Missing: " + modules[i] + "\n\n" +
                "Tried:\n" +
                "  - " + moduleBin.fsName + "\n" +
                "  - " + moduleJsx.fsName + "\n\n" +
                "No JSXBIN files found. Please contact your administrator.");
            return;
        }

        try {
            $.evalFile(moduleFile);
        } catch (e) {
            alert("Error loading module: " + modules[i] + "\n\n" + e.toString());
            return;
        }
    }

    $.global.PRLB_PRODUCTION = isProduction;

    if (typeof security === "undefined" || !security.check) {
        alert("Security module not available. Access denied.");
        return;
    }
    var securityResult = security.check();
    if (!securityResult.ok) {
        security.showActivationError(securityResult.machineID);
        if (typeof prlbLogger !== "undefined") {
            prlbLogger.log("SECURITY", "WARN", "Access denied (ProofLab Launcher) - Machine ID: " + securityResult.machineID);
        }
        return;
    }

    var licenseRole = "UNKNOWN";
    if (typeof security !== "undefined" && security.getRoleFromLicense) {
        licenseRole = security.getRoleFromLicense();
    }

    var devModeActive = false;
    if (typeof security !== "undefined" && security.checkDevelopmentMode) {
        devModeActive = security.checkDevelopmentMode();
    }

    var uiMode = "USER"; // Default to USER for safety
    if (devModeActive) {
        uiMode = "ADMIN"; // Dev mode gets full access
    } else if (licenseRole === "ADMIN") {
        uiMode = "ADMIN";
    } else {
        uiMode = "USER"; // USER or unknown -> USER mode
    }

    $.global.PRLB_MODE = uiMode;

    if (typeof prlbLogger !== "undefined") {
        prlbLogger.log("LAUNCHER", "INFO", "UI Mode: " + $.global.PRLB_MODE + " (License Role: " + licenseRole + (devModeActive ? ", Dev Mode Active" : "") + ")");
    }

    var mainBin = new File(scriptDirPath + "/ProofLab.jsxbin");
    var mainJsx = new File(scriptDirPath + "/ProofLab.jsx");

    var mainFile = null;
    if (mainBin.exists) {
        mainFile = mainBin;
    } else if (mainJsx.exists) {
        mainFile = mainJsx;
        $.global.PRLB_PRODUCTION = false; // Using .jsx = dev mode
    } else {
        alert("Error: ProofLab script not found.\n\n" +
            "Tried:\n" +
            "  - " + mainBin.fsName + "\n" +
            "  - " + mainJsx.fsName + "\n\n" +
            "No JSXBIN files found. Please contact your administrator.");
        return;
    }

    try {
        $.evalFile(mainFile);
    } catch (e) {
        alert("Error loading ProofLab script: " + e.toString());
        return;
    }
})();
