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

vendorCode: {
        type: DataTypes.TEXT,
      
      

      },

vendorName: {
        type: DataTypes.TEXT,
      
      

      },

vendorType: {
        type: DataTypes.ENUM,
      
      

        values: [

"SUPPLIER",


"SERVICE_PROVIDER",


"CONTRACTOR",


"GOVERNMENT"

        ],

      },

taxClassification: {
        type: DataTypes.ENUM,
      
      

        values: [

"VAT_REGISTERED",


"NON_VAT",


"GOVERNMENT"

        ],

      },

tinNumber: {
        type: DataTypes.TEXT,
      
      

      },

birRegAddress: {
        type: DataTypes.TEXT,
      
      

      },

contactPerson: {
        type: DataTypes.TEXT,
      
      

      },

contactNumber: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

paymentTermsDays: {
        type: DataTypes.INTEGER,
      
      

      },

wtaxAtcCode: {
        type: DataTypes.TEXT,
      
      

      },

wtaxRate: {
        type: DataTypes.DECIMAL,
      
      

      },

bankName: {
        type: DataTypes.TEXT,
      
      

      },

bankAccountNumber: {
        type: DataTypes.TEXT,
      
      

      },

bankAccountName: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  vendors.associate = (db) => {


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






























    db.vendors.hasMany(db.ap_invoices, {
      as: 'ap_invoices_vendor',
      foreignKey: {
          name: 'vendorId',
      },
      constraints: false,
    });
















//end loop



    db.vendors.belongsTo(db.accounts, {
      as: 'defaultAccount',
      foreignKey: {
        name: 'defaultAccountId',
      },
      constraints: false,
    });




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

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



  return vendors;
};


