const {Structure, awSQL} = require("../");
const fs = require("fs").promises;
const path = require("path");

const Customers = new Structure()
                    .int("CustomerID", 255, {auto_increment: true, primary: true})
                    .varchar("CustomerName", 255)
                    .varchar("ContactName", 255)
                    .varchar("Address", 255)
                    .varchar("City", 255)
                    .varchar("PostalCode", 255)
                    .varchar("Country")

const Categories = new Structure()
                    .int("CategoryID", 255, {auto_increment: true, primary: true})
                    .varchar("CategoryName", 255)
                    .text("Description")

const Employees = new Structure()
                    .int("EmployeeID", 255, {auto_increment: true, primary: true})
                    .varchar("LastName", 255)
                    .varchar("FirstName", 255)
                    .varchar("BirthDate", 255)
                    .varchar("Photo", 255)
                    .text("Notes")

const OrderDetails = new Structure()
                    .int("OrderDetailID", 255, {auto_increment: true, primary: true})
                    .int("OrderID", 255)
                    .int("ProductID", 255)
                    .int("Quantity", 255)

const Orders = new Structure()
                    .int("OrderID", 255, {auto_increment: true, primary: true})
                    .int("CustomerID", 255)
                    .int("EmployeeID", 255)
                    .varchar("OrderDate",255)
                    .int("ShipperID", 255)

const Products = new Structure()
                    .int("ProductID", 255, {auto_increment: true, primary: true})
                    .varchar("ProductName", 255)
                    .int("SupplierID", 255)
                    .int("CategoryID", 255)
                    .varchar("Unit", 255)
                    .decimal("Price", 10, 2)

const Shippers = new Structure()
                    .int("ShipperID", 255, {auto_increment: true, primary: true})
                    .varchar("ShipperName", 255)
                    .varchar("Phone", 255)

const Suppliers = new Structure()
                    .int("SupplierID", 255, {auto_increment: true, primary: true})
                    .varchar("SupplierName", 255)
                    .varchar("ContactName", 255)
                    .varchar("Address", 255)
                    .varchar("City", 255)
                    .varchar("PostalCode", 255)
                    .varchar("Country", 255)
                    .varchar("Phone", 255)

async function install(){
    const TABLES = {
        Customers: {
            struc: Customers,
            data: JSON.parse(await fs.readFile(path.resolve(__dirname, "installRows", "Customers.json")))
        },
        Categories: {
            struc: Categories,
            data: JSON.parse(await fs.readFile(path.resolve(__dirname, "installRows", "Categories.json")))
        },
        Employees: {
            struc: Employees,
            data: JSON.parse(await fs.readFile(path.resolve(__dirname, "installRows", "Employees.json")))
        },
        OrderDetails: {
            struc: OrderDetails,
            data: JSON.parse(await fs.readFile(path.resolve(__dirname, "installRows", "OrderDetails.json")))
        },
        Orders: {
            struc: Orders,
            data: JSON.parse(await fs.readFile(path.resolve(__dirname, "installRows", "Orders.json")))
        },
        Products: {
            struc: Products,
            data: JSON.parse(await fs.readFile(path.resolve(__dirname, "installRows", "Products.json")))
        },
        Shippers: {
            struc: Shippers,
            data: JSON.parse(await fs.readFile(path.resolve(__dirname, "installRows", "Shippers.json"))),
        },
        Suppliers: {
            struc: Suppliers,
            data: JSON.parse(await fs.readFile(path.resolve(__dirname, "installRows", "Suppliers.json")))
        }
    }

    const instance = awSQL.getInstance();

    const existingTables = await instance.getTables(process.env.DATABASE);

    for (let key in TABLES){
        if (existingTables.includes(key)){
            await instance.dropTable(key);
        }
        await instance.createTable(key).selectDatabase(process.env.DATABASE).structure(TABLES[key].struc).execute();
        await instance.insert(key).data(TABLES[key].data).execute();
    }
}
module.exports = install;