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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

rate: {
        type: DataTypes.DECIMAL,
      
      

      },

scope: {
        type: DataTypes.ENUM,
      
      

        values: [

"sales",


"purchase",


"both"

        ],

      },

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

      },

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

  taxes.associate = (db) => {


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















    db.taxes.hasMany(db.products, {
      as: 'products_tax',
      foreignKey: {
          name: 'taxId',
      },
      constraints: false,
    });








    db.taxes.hasMany(db.sales_order_lines, {
      as: 'sales_order_lines_tax',
      foreignKey: {
          name: 'taxId',
      },
      constraints: false,
    });



    db.taxes.hasMany(db.purchase_order_lines, {
      as: 'purchase_order_lines_tax',
      foreignKey: {
          name: 'taxId',
      },
      constraints: false,
    });







    db.taxes.hasMany(db.invoice_lines, {
      as: 'invoice_lines_tax',
      foreignKey: {
          name: 'taxId',
      },
      constraints: false,
    });




//end loop



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




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

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



  return taxes;
};


