import { MongoClient } from "mongodb";
import config from '../../../config.json'

const uri = config.MONGODB_URI;
const options = {
    tlsCertificateKeyFile: config.MONGODB_CERT
};

const mongo = new MongoClient(uri, options);
await mongo.connect();

export const disconnect = async () => {
    mongo.close();
}

export const getDB = async (dbName = config.MONGODB_DB) => {
    return mongo.db(dbName);
}

export const Users = async () => {
    const db = await getDB();
    return db.collection('users');
}

export const AccessTokens = async () => {
    const db = await getDB();
    return db.collection('accessTokens');
}

export const Galaxies = async () => {
    const db = await getDB();
    return db.collection('galaxies');
}

export const Sectors = async () => {
    const db = await getDB();
    return db.collection('sectors');
}

export const Systems = async () => {
    const db = await getDB();
    return db.collection('systems');
}

export const Planets = async () => {
    const db = await getDB();
    return db.collection('planets');
}

export const Buildings = async() => {
    const db = await getDB();
    return db.collection('buildings');
}

export const Research = async() => {
    const db = await getDB();
    return db.collection('research');
}

export const Resources = async() => {
    const db = await getDB();
    return db.collection('resources');
}

export const Ships = async() => {
    const db = await getDB();
    return db.collection('ships');
}

export const Defenses = async() => {
    const db = await getDB();
    return db.collection('defenses');
}

export const Fleet = async() => {
    const db = await getDB();
    return db.collection('fleet');
}

export const Structures = async() => {
    const db = await getDB();
    return db.collection('structures');
}

export const Lang = async (language = "en") => {
    const db = await getDB(`${config.MONGODB_DB}_${language}`);
    return [
        await db.collection('general'),
        await db.collection('buildings'),
        await db.collection('ships'),
        await db.collection('resources'),
        await db.collection('research'),
        await db.collection('structures'),
        await db.collection('defenses')
    ]
}

export const Mails = async() => {
    const db = await getDB();
    return db.collection('mails');
}