import consola from "consola";
import { config as configDotenv } from 'dotenv';

configDotenv();
export const config = {
    internal_port: parseInt(process.env.APP_PORT) || 4010,
    development: process.env.NODE_ENV === "production",
    version: process.env.VERSION || require('../package.json').version,
    api_key: getApiKey(),
    app_url: process.env.APP_URL || "http://localhost:8080",
    mail_server: process.env.MAIL_SERVER,
    mail_port: Number(process.env.MAIL_PORT) || 25,
    mail_user: process.env.MAIL_USER,
    mail_password: process.env.MAIL_PASSWORD,
    mail_from: process.env.MAIL_FROM,
    privacy_url: process.env.PRIVACY_URL || "/privacy",
    imprint_url: process.env.IMPRINT_URL || "/imprint",
    copyright_owner: process.env.COPYRIGHT_OWNER || "LfK!",
    event_name: process.env.EVENT_NAME || "Testing 4 Kaya!",
    contact_mail: process.env.CONTACT_MAIL || process.env.MAIL_FROM,
}
let errors = 0
if (typeof config.internal_port !== "number") {
    errors++
}
if (typeof config.development !== "boolean") {
    errors++
}

function getApiKey(): string {
    const key = process.env.API_KEY;
    if (!key) {
        consola.info("No API key set - generating a random one...");
        let result = '';
        const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        const charactersLength = characters.length;
        for (var i = 0; i < 64; i++) {
            result += characters.charAt(Math.floor(Math.random() * charactersLength));
        }
        consola.info(`API KEY: ${result}`)
        return result;
    }
    if (key.length < 64) {
        consola.error(`API key is too short - minimum: 64, current: ${key.length}`)
        throw new Error("API_KEY too short.")
    }
    return key
}
export let e = errors