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

name: {
        type: DataTypes.TEXT,
      
      

      },

due_days: {
        type: DataTypes.INTEGER,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  payment_terms.associate = (db) => {


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










    db.payment_terms.hasMany(db.clients, {
      as: 'clients_payment_terms',
      foreignKey: {
          name: 'payment_termsId',
      },
      constraints: false,
    });







    db.payment_terms.hasMany(db.quotes, {
      as: 'quotes_payment_terms',
      foreignKey: {
          name: 'payment_termsId',
      },
      constraints: false,
    });



    db.payment_terms.hasMany(db.sales_orders, {
      as: 'sales_orders_payment_terms',
      foreignKey: {
          name: 'payment_termsId',
      },
      constraints: false,
    });





    db.payment_terms.hasMany(db.invoices, {
      as: 'invoices_payment_terms',
      foreignKey: {
          name: 'payment_termsId',
      },
      constraints: false,
    });

















//end loop



    db.payment_terms.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });




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

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



  return payment_terms;
};


