#!/usr/bin/env node
/**
 * Install the exact version from package.json to Windows and WSL
 * This ensures we get the version we just published, not a stale @latest
 */

import { execSync } from 'child_process';
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// Read version from package.json
const packagePath = join(__dirname, '..', 'package.json');
const packageJson = JSON.parse(readFileSync(packagePath, 'utf-8'));
const version = packageJson.version;

console.log(`\n📦 Installing @bobfrankston/msger@${version}...\n`);

try {
    // Clean and install on Windows
    console.log('🪟 Windows installation:');
    execSync('npm cache clean --force', { stdio: 'inherit' });
    execSync(`npm install -g @bobfrankston/msger@${version}`, { stdio: 'inherit' });

    // Clean and install on WSL
    console.log('\n🐧 WSL installation:');
    execSync('wsl npm cache clean --force', { stdio: 'inherit' });
    execSync(`wsl npm install -g @bobfrankston/msger@${version}`, { stdio: 'inherit' });

    console.log(`\n✅ Successfully installed version ${version} on Windows and WSL\n`);
} catch (error: any) {
    console.error(`\n❌ Installation failed: ${error.message}\n`);
    process.exit(1);
}
