const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');

module.exports = function(sequelize, DataTypes) {
  const business_units = sequelize.define(
    'business_units',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

unit_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"property",


"retail",


"services",


"content",


"general"

        ],

      },

is_active: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  business_units.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity








    db.business_units.hasMany(db.assets, {
      as: 'assets_business_unit',
      foreignKey: {
          name: 'business_unitId',
      },
      constraints: false,
    });





    db.business_units.hasMany(db.products, {
      as: 'products_business_unit',
      foreignKey: {
          name: 'business_unitId',
      },
      constraints: false,
    });






    db.business_units.hasMany(db.sales_orders, {
      as: 'sales_orders_business_unit',
      foreignKey: {
          name: 'business_unitId',
      },
      constraints: false,
    });










    db.business_units.hasMany(db.content_items, {
      as: 'content_items_business_unit',
      foreignKey: {
          name: 'business_unitId',
      },
      constraints: false,
    });


    db.business_units.hasMany(db.seo_keywords, {
      as: 'seo_keywords_business_unit',
      foreignKey: {
          name: 'business_unitId',
      },
      constraints: false,
    });


    db.business_units.hasMany(db.content_campaigns, {
      as: 'content_campaigns_business_unit',
      foreignKey: {
          name: 'business_unitId',
      },
      constraints: false,
    });





//end loop



    db.business_units.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });




    db.business_units.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.business_units.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };



  return business_units;
};


