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 firms = sequelize.define(
    'firms',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

name: {
        type: DataTypes.TEXT,
      
      

      },

legal_name: {
        type: DataTypes.TEXT,
      
      

      },

tax_id_number: {
        type: DataTypes.TEXT,
      
      

      },

country: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

contact_email: {
        type: DataTypes.TEXT,
      
      

      },

contact_phone: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

trial_ends_at: {
        type: DataTypes.DATE,
      
      

      },

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

  firms.associate = (db) => {


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







    db.firms.hasMany(db.merchants, {
      as: 'merchants_firm',
      foreignKey: {
          name: 'firmId',
      },
      constraints: false,
    });
















    db.firms.hasMany(db.messages, {
      as: 'messages_firm',
      foreignKey: {
          name: 'firmId',
      },
      constraints: false,
    });


    db.firms.hasMany(db.audit_events, {
      as: 'audit_events_firm',
      foreignKey: {
          name: 'firmId',
      },
      constraints: false,
    });



//end loop



    db.firms.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return firms;
};


