import fs from 'fs';
import * as readLine from 'node:readline/promises';
import { exec } from 'node:child_process';

const defaultDict = {
    "token": "Your bot token",
    "developmentToken": "Your development bot token",
    "managementGuildID": "Your management guild ID",
    "developmentGuildID": "Your development guild ID",
    "enableDevelopment": true,
    "owners": [],
    "verifySecret": "If using verify, enter a code here which matches your website if needed",
    "mongoUrl": "Your Mongo connection string, e.g. mongodb://127.0.0.1:27017",
    "baseUrl": "Your website where buttons such as Verify will link to, e.g. https://example.com",
    "pastebinApiKey": "An API key for pastebin (optional)",
    "pastebinUsername": "Your pastebin username (optional)",
    "pastebinPassword": "Your pastebin password (optional)",
}

const readline = readLine.createInterface({
    input: process.stdin,
    output: process.stdout,
});

async function getInput(prompt) {
    process.stdout.write(prompt);

    const answer = await readline.question(prompt);
    return answer.toString();
}


export default async function(walkthrough = false) {
    if (walkthrough) {
        console.log("\x1b[33m🛈  Entering walkthrough mode for any missing values.");
        console.log("   \x1b[2mIf you don't want to enter a value, just hit enter.\x1b[0m\n");

        // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > ");
        // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i";
        // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") {
        //     console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`);
        //     await exec(toUse);
        //     console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`);
        // } else {
        //     console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m");
        // }
    }

    let json;
    let out = true
    try {
        json = JSON.parse(fs.readFileSync('./src/config/main.json', 'utf8'));
    } catch (e) {
        console.log("\x1b[31m⚠ No main.json found, creating one.")
        console.log("  \x1b[2mYou can edit src/config/main.json directly using template written to the file.\x1b[0m\n")
        out = false
        json = {}
    }
    for (const key in defaultDict) {
        if (!json[key]) {
            if (walkthrough) {
                switch (key) {
                    case "enableDevelopment": {
                        json[key] = (await getInput("\x1b[36mEnable development mode? This redisters commands in a single server making it easier to test\x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > ") || "Y").toLowerCase() === "y"; break;
                    } case "owners": {
                        let chosen = "!";
                        let toWrite = []
                        while (chosen !== "") {
                            chosen = await getInput("\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > ");
                            if (chosen !== "") { toWrite.push(chosen); }
                        }
                        json[key] = toWrite; break;
                    } default: {
                        json[key] = await getInput(`\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > `);
                    }
                }
            }
            else { json[key] = defaultDict[key] }
        }
    }
    if (!json.mongoUrl.endsWith("/")) json.mongoUrl += "/";
    if (!json.baseUrl.endsWith("/")) json.baseUrl += "/";
    let hosts;
    try {
        hosts = fs.readFileSync('/etc/hosts', 'utf8').toString().split("\n");
    } catch (e) {
        return console.log("\x1b[31m⚠ No /etc/hosts found. Please ensure the file exists and is readable. (Windows is not supported, Mac and Linux users should not experience this error)")
    }
    let localhost = hosts.find(line => line.split(" ")[1] === "localhost");
    if (localhost) { localhost = localhost.split(" ")[0]; }
    else { localhost = "127.0.0.1" }
    json.mongoUrl = json.mongoUrl.replace("localhost", localhost);
    json.baseUrl = json.baseUrl.replace("localhost", localhost);

    fs.writeFileSync("./src/config/main.json", JSON.stringify(json, null, 4))

    if (walkthrough) {
        console.log("\x1b[32m✓ All properties added.\x1b[0m")
    }
    return out;
}
