/**
 * Environment check script for the Daily Notification plugin
 * Validates that all required tools and dependencies are installed
 */

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

// Constants
const REQUIRED_NODE_VERSION = 14;
const REQUIRED_JAVA_VERSION = 11;

// Colors for output
const GREEN = '\x1b[32m';
const YELLOW = '\x1b[33m';
const RED = '\x1b[31m';
const RESET = '\x1b[0m';

// Logging functions
const log = {
    info: (msg) => console.log(`🔍 ${msg}`),
    success: (msg) => console.log(`${GREEN}✅ ${msg}${RESET}`),
    warn: (msg) => console.log(`${YELLOW}⚠️ ${msg}${RESET}`),
    error: (msg) => console.log(`${RED}❌ ${msg}${RESET}`)
};

// Check if a command exists
function commandExists(command) {
    try {
        execSync(`which ${command}`, { stdio: 'ignore' });
        return true;
    } catch {
        return false;
    }
}

// Get Node.js version
function checkNodeVersion() {
    const version = process.version.match(/^v(\d+)/)[1];
    if (version < REQUIRED_NODE_VERSION) {
        throw new Error(`Node.js version ${REQUIRED_NODE_VERSION} or higher is required`);
    }
    log.success(`Node.js: v${version}`);
}

// Check npm
function checkNpm() {
    if (!commandExists('npm')) {
        throw new Error('npm is not installed');
    }
    log.success('npm is installed');
}

// Get Java version
function checkJava() {
    try {
        const output = execSync('java -version 2>&1').toString();
        const version = output.match(/version "(\d+)/)[1];
        if (version < REQUIRED_JAVA_VERSION) {
            throw new Error(`Java version ${REQUIRED_JAVA_VERSION} or higher is required`);
        }
        log.success(`Java: version ${version}`);
    } catch (error) {
        throw new Error('Java is not installed or invalid version');
    }
}

// Check Android environment
function checkAndroid() {
    if (!process.env.ANDROID_HOME) {
        throw new Error('ANDROID_HOME environment variable is not set');
    }
    log.success('Android environment is properly configured');
}

// Main function
async function main() {
    log.info('Checking development environment...\n');
    let hasErrors = false;

    try {
        checkNodeVersion();
        checkNpm();
        checkJava();
        checkAndroid();
    } catch (error) {
        log.error(error.message);
        hasErrors = true;
    }

    // Check iOS requirements only on macOS
    if (os.platform() === 'darwin') {
        try {
            if (!commandExists('xcodebuild')) {
                log.error('Xcode is not installed');
                hasErrors = true;
            } else {
                log.success('Xcode is installed');
            }
        } catch (error) {
            log.error('Failed to check Xcode installation');
            hasErrors = true;
        }
    } else {
        log.warn('iOS development requires macOS');
    }

    console.log(''); // Empty line for readability

    // Exit with appropriate code
    if (hasErrors && os.platform() === 'darwin') {
        log.error('Environment check failed. Please fix the issues above.');
        process.exit(1);
    } else {
        log.success('Environment check completed successfully for the current platform.');
        process.exit(0);
    }
}

main().catch(error => {
    log.error('Unexpected error:', error);
    process.exit(1);
}); 