const path = require("path");
const {awSQL} = require("../index");
require("dotenv").config();

const fs = require("fs").promises;

const instance = awSQL.createInstance("schwarze-drya.de", process.env.USER, process.env.PASS, {defaultDatabase: process.env.DATABASE, multipleStatements: true});

async function main(){

    const result = await instance.connect();
    const Categories = await instance.select("Categories").execute();
    let md = `# Tables\n\n`;
    md+=genTable("Categories", Categories);
    md+="\n\n";
    md+=genTable("Customers", await instance.select("Customers").execute());
    md+="\n\n";
    md+=genTable("Employees", await instance.select("Employees").execute());
    md+="\n\n";
    md+=genTable("OrderDetails", await instance.select("OrderDetails").execute());
    md+="\n\n";
    md+=genTable("Orders", await instance.select("Orders").execute());
    md+="\n\n";
    md+=genTable("Products", await instance.select("Products").execute());
    md+="\n\n";
    md+=genTable("Shippers", await instance.select("Shippers").execute());
    md+="\n\n";
    md+=genTable("Suppliers", await instance.select("Suppliers").execute());
    await fs.writeFile(path.resolve(__dirname, "database.md"), md, "utf-8");
}

function genTable(header, rows){
    let md = `## ${header}`
    md += "\n| "
    for (let key in rows[0]){
        md += `${key} | `;
    }
    md += `\n`;
    for (let key in rows[0]){
        md += `| -- `;
    }
    md +="|"
    for (let row of rows){
        md+="\n| ";
        for (let key in row){
            md += `${row[key]} | `
        }
    }
    return md;
}

main();