/**
 * Setup script for native dependencies
 * Configures native build environments and dependencies
 */

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

function setupAndroid() {
    console.log('🔧 Setting up Android environment...');

    // Create gradle wrapper if it doesn't exist
    const gradleWrapper = path.join('android', 'gradlew');
    if (!fs.existsSync(gradleWrapper)) {
        console.log('Creating Gradle wrapper...');
        execSync('cd android && gradle wrapper', { stdio: 'inherit' });
    }

    // Make gradle wrapper executable
    fs.chmodSync(gradleWrapper, '755');

    // Create local.properties if it doesn't exist
    const localProperties = path.join('android', 'local.properties');
    if (!fs.existsSync(localProperties)) {
        const androidHome = process.env.ANDROID_HOME;
        if (androidHome) {
            const content = `sdk.dir=${androidHome.replace(/\\/g, '\\\\')}\n`;
            fs.writeFileSync(localProperties, content);
            console.log('Created local.properties');
        }
    }

    // Sync Gradle
    console.log('Syncing Gradle...');
    execSync('cd android && ./gradlew --refresh-dependencies', { stdio: 'inherit' });
}

function setupIOS() {
    if (process.platform !== 'darwin') {
        console.warn('⚠️ Skipping iOS setup (macOS required)');
        return;
    }

    console.log('🔧 Setting up iOS environment...');

    // Install pods
    console.log('Installing CocoaPods dependencies...');
    execSync('cd ios && pod install', { stdio: 'inherit' });

    // Create Xcode project if it doesn't exist
    const xcodeProject = path.join('ios', 'DailyNotificationPlugin.xcodeproj');
    if (!fs.existsSync(xcodeProject)) {
        console.log('Creating Xcode project...');
        execSync('cd ios && pod setup', { stdio: 'inherit' });
    }
}

function main() {
    console.log('🚀 Setting up native development environment...\n');

    try {
        setupAndroid();
        setupIOS();
        console.log('\n✅ Native environment setup completed successfully!');
    } catch (error) {
        console.error('\n❌ Native environment setup failed:', error.message);
        process.exit(1);
    }
}

main(); 