const path = require('path');
const fs = require('fs');

require('dotenv').config({ path: path.join(__dirname, '.env') });


//this runs on main app 
function convertBaseToString() {
    var base64 = process.env.MY_CONFIG;
    if (base64) {
        const decodedContents = Buffer.from(base64, 'base64').toString('utf-8');
    const configArray = decodedContents.split('\n');
    const configObject = {};

    configArray.forEach(pair => {
        if (pair.trim() !== '') {
            const [rawKey, value] = pair.split('=');
            const key = rawKey.trim().replace(/^'|'$/g, '');
            configObject[key] = value.trim();
        }
    });

    return configObject;
    }else{
        const envContents = fs.readFileSync('.env', 'utf-8');
        const base64String = Buffer.from(envContents, 'utf-8').toString('base64');
        const newEnvContents = `MY_CONFIG = ${base64String}`;
        fs.writeFileSync('.env', newEnvContents, 'utf-8');
        console.log("Convert String into Base64 is success");

        const parts = newEnvContents.split('=');

        if (parts.length >= 2) {
        const result = parts.slice(1).join('=');


        const decodedContents = Buffer.from(result, 'base64').toString('utf-8');
        const configArray = decodedContents.split('\n');
        const configObject = {};
    
        configArray.forEach(pair => {
            if (pair.trim() !== '') {
                const [rawKey, value] = pair.split('=');
                const key = rawKey.trim().replace(/^'|'$/g, '');
                configObject[key] = value.trim();
            }
        });
    
        return configObject;
        } else {
        console.log("Invalid input format");
        }
        
    }
}


//command line utility encode , decode and check     add with npm run convertEnvBase64
function envConverter(value) {
    var base64 = process.env.MY_CONFIG;

        if (value == 'encode') {
        const envContents = fs.readFileSync('.env', 'utf-8');
        const base64String = Buffer.from(envContents, 'utf-8').toString('base64');
        const newEnvContents = `MY_CONFIG = ${base64String}`;
        fs.writeFileSync('.env', newEnvContents, 'utf-8');
        console.log("Convert String into Base64 is success");
        }else if (value == 'decode') {
            const envContents = fs.readFileSync('.env', 'utf-8');
            const base64String = envContents.split('=')[1].trim();
            const originalContents = Buffer.from(base64String, 'base64').toString('utf-8');
            fs.writeFileSync('.env', originalContents, 'utf-8');
            console.log("Convert Base64 into String is success");
        }else if(value == 'check'){
           if (base64) {
            const decodedContents = Buffer.from(base64, 'base64').toString('utf-8');
            const configArray = decodedContents.split('\n');
            const configObject = {};
        
            configArray.forEach(pair => {
                if (pair.trim() !== '') {
                    const [rawKey, value] = pair.split('=');
                    const key = rawKey.trim().replace(/^'|'$/g, '');
                    configObject[key] = value.trim();
                }
            });
            console.log(configObject);
           }else{
            console.log("Already Decoded String");
           }
        }
    
    }

const args = process.argv.slice(2);
// console.log(args);
envConverter(args[0]);

module.exports = convertBaseToString;
