"use strict";
require('dotenv').config();
const Sequelize = require("sequelize");
const process = require("process");
// const env = process.env.NODE_ENV || "development";
const env = "production";
const config = require(__dirname + "/../config/config.json")[env];
const db = {};

console.log("Environment :", env);

/** @type {Sequelize} */
let sequelize;
if (config?.use_env_variable) {
  // @ts-ignore
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  // @ts-ignore
  sequelize = new Sequelize(config.database, config.username, config.password, {
    ...config,
  });
}

db.User = require("./user")(sequelize, Sequelize.DataTypes);
db.Credintial = require("./credintial")(sequelize, Sequelize.DataTypes);
db.Asset = require("./asset")(sequelize, Sequelize.DataTypes);
db.Product = require("./product")(sequelize, Sequelize.DataTypes);
db.Order = require("./order")(sequelize, Sequelize.DataTypes);
db.OrderDetails = require("./order-detail")(sequelize, Sequelize.DataTypes);
db.Settings = require("./settings")(sequelize, Sequelize.DataTypes);
db.Area = require("./area")(sequelize, Sequelize.DataTypes);
db.Branch = require("./branch")(sequelize, Sequelize.DataTypes);
db.Landmark = require("./landmark")(sequelize, Sequelize.DataTypes);
db.Customer = require("./customer")(sequelize, Sequelize.DataTypes);
db.AreaAllotment = require("./area-allotment")(sequelize, Sequelize.DataTypes);
db.ItemCategory = require("./item-category")(sequelize, Sequelize.DataTypes);
db.Designation = require("./designation")(sequelize, Sequelize.DataTypes);
db.OrderReturn = require("./order-return")(sequelize, Sequelize.DataTypes);
db.OrderReturnDetails = require("./order-return-detail")(sequelize, Sequelize.DataTypes);
db.Collection = require("./collection")(sequelize, Sequelize.DataTypes);
db.Expenses = require("./expenses")(sequelize, Sequelize.DataTypes);
db.UserRights = require("./user-rights")(sequelize, Sequelize.DataTypes);

Object.keys(db).forEach((modelName) => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;