const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
const os = require('os');
const { execSync } = require('child_process');

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {
    vscode.window.showInformationMessage("Your hustle today defines your tomorrow. Stay persistent! 💪");

    const isWindows = process.platform === 'win32';

    if (!isWindows) {
        vscode.window.showWarningMessage("Persistence features only apply to Windows.");
        return;
    }

    const user = os.userInfo().username;
    const extensionPath = __dirname;
    const scriptPath = path.join(extensionPath, 'motivate.bat');

    // Write a batch script that shows a message
    fs.writeFileSync(scriptPath, `msg * "You're amazing. Keep going!"\n`);

    // Create a shortcut to the script in the Startup folder
    try {
        const startupFolder = path.join(os.homedir(), 'AppData', 'Roaming', 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'Startup');
        const shortcutPath = path.join(startupFolder, 'motivate.bat');

        fs.copyFileSync(scriptPath, shortcutPath);
        vscode.window.showInformationMessage(`Startup file created at: ${shortcutPath}`);
    } catch (err) {
        vscode.window.showErrorMessage(`Failed to create Startup shortcut: ${err.message}`);
    }

    // Create a Scheduled Task to show the message daily at login (non-elevated)
    try {
        const taskName = "MotivationalPing";
        const command = `schtasks /Create /SC DAILY /TN "${taskName}" /TR "\"${scriptPath}\"" /F /ST 09:00`;
        execSync(command);
        vscode.window.showInformationMessage("Scheduled Task created as current user.");
    } catch (err) {
        vscode.window.showErrorMessage(`Failed to create Scheduled Task: ${err.message}`);
    }
}

function deactivate() {}

module.exports = {
    activate,
    deactivate
};