const dotenv = require("dotenv");
const path = require("path");
const mariadb = require("mariadb");
const fs = require("fs");

const mode = process.env.NODE_ENV || "development";
dotenv.config({ path: path.resolve(process.cwd(), `.env.${mode}`) });

const { DB_HOST, DB_PORT, DB_USERNAME, DB_PASSWORD, DB_NAME } = process.env;

//do not accidentally seed prod pls
if (mode !== "development") {
    process.exit(0);
}

(async () => {
    const conn = await mariadb.createConnection({
        host: DB_HOST,
        port: DB_PORT,
        user: DB_USERNAME,
        password: DB_PASSWORD,
        database: DB_NAME,
        multipleStatements: true,
    });

    const seedFile = path.join(process.cwd(), "migrations", "seed.sql");
    const sql = fs.readFileSync(seedFile, "utf8");

    await conn.query(sql);
    await conn.end();

    console.log("Seeded");
})();